Skip to main content
PreparationZone Logo
PrepZone

Distributed Cache Deep Dive

Redis clusters, eviction policies, hot keys and cache stampede prevention at scale.

Pass checkpoint below

Read these first

StreamHub's Redis cluster holds stream metadata, session tokens, and rate-limit counters for every API pod. When one shard goes hot during a viral stream, the difference between a well-tuned cluster and a naive single-node Redis is the difference between 5 ms and timeout errors.

Redis cluster architecture

Hash ring0 … 2^32-1
Node Akeys 0–25%
Node Bkeys 25–50%
Node Ckeys 50–75%
Node Dkeys 75–100%

New node E joins → only ~1/N keys move, not all keys.

Adding a node only remaps keys adjacent to it on the ring.

Redis Cluster essentials

  • 16384 hash slots partitioned across primary nodes; each key maps to one slot.
  • Primary + replica pairs per shard — replica promotes on primary failure.
  • Client-side routing: Redis clients follow MOVED/ASK redirects to the correct shard.
  • No cross-slot transactions unless keys share the same hash tag {user}:profile and {user}:settings.
Java
# ElastiCache Redis cluster (pseudo-config)
engine: redis-7
node_type: cache.r7g.xlarge
num_shards: 12
replicas_per_shard: 1
maxmemory_policy: allkeys-lru
cluster_mode: enabled

Eviction policies in practice

PolicyBehaviourWhen to use
allkeys-lruEvict any key by LRU when memory fullGeneral cache — StreamHub metadata
volatile-lruEvict only keys with TTL setMixed cache + session store
allkeys-lfuEvict least frequently used keysStable hot sets (leaderboards)
noevictionReturn errors when memory fullRate limit counters — never silently drop
  • allkeys-lru

    BehaviourEvict any key by LRU when memory full
    When to useGeneral cache — StreamHub metadata
  • volatile-lru

    BehaviourEvict only keys with TTL set
    When to useMixed cache + session store
  • allkeys-lfu

    BehaviourEvict least frequently used keys
    When to useStable hot sets (leaderboards)
  • noeviction

    BehaviourReturn errors when memory full
    When to useRate limit counters — never silently drop

The rest of this guide is part of Pro

Free covers every fresher guide in full. Pro unlocks Senior, Staff and Architect guides across every track.

Still to come · 3 sections

  • Hot key problem
  • Cache stampede prevention
  • Monitoring and operations

Test yourself

Answer these before moving on — recall is what makes it stick.