← Home ← Codex ← DEBT
Browse by Category
+ added · updated 7d
← Back to glossary

Redis Patterns (Pub/Sub, Sorted Sets, Lua)

Performance PHP 5.0+ Intermediate
debt(d7/e5/b5/t5)
d7 Detectability Operational debt — how invisible misuse is to your safety net

Closest to 'only careful code review or runtime testing' (d7), since redis-insight and laravel-debugbar can surface N+1 Redis calls or missing TTLs at runtime, but no automated linter flags misuse patterns like KEYS * or missing pipelines.

e5 Effort Remediation debt — work required to fix once spotted

Closest to 'touches multiple files / significant refactor' (e5), as switching from per-call requests to pipelines, adding TTLs, or replacing KEYS with SCAN typically touches several cache/queue modules, not just one line.

b5 Burden Structural debt — long-term weight of choosing wrong

Closest to 'persistent productivity tax' (b5), because Redis usage spans web/cli/queue-worker contexts and patterns chosen (data structures, persistence config) affect many work streams across the codebase.

t5 Trap Cognitive debt — how counter-intuitive correct behaviour is

Closest to 'notable trap most devs eventually learn' (t5), grounded in the misconception that Redis is just a faster Memcached — devs miss its rich data structures and footguns like KEYS * blocking or missing TTLs causing eviction.

About DEBT scoring →

Also Known As

Redis usage patterns Redis cache Redis data structures

TL;DR

Beyond key-value caching, Redis enables queues, leaderboards, rate limiting, pub/sub messaging, and distributed locks via its rich data structures.

Explanation

Redis data structures enable sophisticated patterns: Sorted Sets (ZADD/ZRANGE) for leaderboards and priority queues; Lists (LPUSH/BRPOP) for FIFO job queues; Sets for unique visitor counting; Hashes for session storage; INCR/EXPIRE for atomic rate limiting counters; Pub/Sub for real-time notifications; Lua scripts for atomic multi-step operations without round-trips; and SET key value NX EX for distributed locks (Redlock for multi-node). Redis Streams (XADD/XREAD) provide durable message queues with consumer groups, persistent delivery, and acknowledgement. PHP clients: Predis (pure PHP) and phpredis (C extension, faster).

Common Misconception

Redis is just a faster alternative to Memcached for caching. Redis supports rich data structures (sorted sets, streams, pub/sub, Lua scripting) enabling rate limiting, leaderboards, distributed locks, and message queues — far beyond simple key-value caching.

Why It Matters

Redis is an in-memory data structure store — beyond simple key-value caching it supports atomic operations, pub/sub, sorted sets, and streams that solve common distributed systems problems elegantly.

Common Mistakes

  • Using Redis as a primary database without persistence configured — data is lost on restart without RDB/AOF.
  • Not setting TTLs on cached keys — Redis fills memory and starts evicting keys unpredictably.
  • Using KEYS * in production — it blocks Redis while scanning all keys; use SCAN instead.
  • Not using pipelines for multiple sequential Redis commands — each round trip adds latency.

Code Examples

✗ Vulnerable
// N sequential Redis calls — N round trips:
foreach ($userIds as $id) {
    $name = $redis->get("user:$id:name"); // 1 round trip per user
}

// Pipeline — 1 round trip for all:
$pipe = $redis->pipeline();
foreach ($userIds as $id) { $pipe->get("user:$id:name"); }
$names = $pipe->execute();
✓ Fixed
// String — simple cache
$redis->set('user:42:name', 'Alice', ['ex' => 300]);

// Hash — object storage without serialisation
$redis->hSet('user:42', 'name', 'Alice');
$redis->hSet('user:42', 'email', 'alice@example.com');
$name = $redis->hGet('user:42', 'name');

// Sorted set — leaderboard
$redis->zAdd('leaderboard', 1500, 'alice');
$top10 = $redis->zRevRange('leaderboard', 0, 9, true); // with scores

// List — simple queue
$redis->lPush('email_queue', json_encode($message));
$job = $redis->rPop('email_queue');

// Atomic counter — rate limiting
$redis->incr('api_hits:' . date('YmdH') . ':' . $ip);

Added 15 Mar 2026
Edited 22 Mar 2026
Views 62
Rate this term
No ratings yet
🤖 AI Guestbook educational data only
| |
Last 30 days
0 pings T 0 pings W 1 ping T 0 pings F 0 pings S 1 ping S 0 pings M 0 pings T 0 pings W 1 ping T 2 pings F 5 pings S 7 pings S 1 ping M 2 pings T 0 pings W 1 ping T 0 pings F 0 pings S 0 pings S 0 pings M 0 pings T 0 pings W 0 pings T 0 pings F 0 pings S 0 pings S 0 pings M 0 pings T 0 pings W
No pings yet today
No pings yesterday
Scrapy 16 Amazonbot 10 Perplexity 9 Ahrefs 3 SEMrush 3 Google 3 Unknown AI 2 Qwen 2 Claude 1 Bing 1
crawler 49 crawler_json 1
DEV INTEL Tools & Severity
🟡 Medium ⚙ Fix effort: Medium
⚡ Quick Fix
Use Redis for sessions, cache, queues, and rate limiting — pipeline multiple commands to reduce round trips; use connection pooling via phpredis or predis
📦 Applies To
PHP 5.0+ web cli queue-worker laravel symfony
🔗 Prerequisites
🔍 Detection Hints
Multiple individual Redis calls in a loop that could be pipelined or use MGET/MSET
Auto-detectable: ✗ No laravel-debugbar redis-insight
⚠ Related Problems
🤖 AI Agent
Confidence: Medium False Positives: Medium ✗ Manual fix Fix: Medium Context: File Tests: Update


✓ schema.org compliant