Distributed Cache (Redis Cluster / Memcached)
Requirements: sub-millisecond get/set/delete with TTL, horizontally scalable to TBs, highly available, eviction when full.
app (client lib with cluster map) --hash(key)--> shard k (primary) --async repl--> replica(s)
16384 hash slots -> nodes (Redis Cluster) / consistent hashing ring
config service / gossip: slot map, failure detection, replica promotion
Deep dives
- Partitioning: consistent hashing or fixed hash slots; resharding moves slots, clients get
MOVEDredirects. - Replication & failover: async replicas, automatic promotion; possible loss of the last writes (cache = acceptable).
- Eviction: LRU/LFU approximations (sampled), TTL expiry (lazy on access + active sampling).
- Hot keys: client-side near-cache, key replication with random suffixes, request coalescing.
- Consistency with DB: cache-aside + delete-on-write; leases (Facebook's memcache paper) to prevent stale sets and thundering herds.
- Memory efficiency: slab allocator (Memcached) vs jemalloc + compact encodings (Redis).