Designing a Distributed Cache
Difficulty: Intermediate Topics: Consistent Hashing, Eviction, Replication, Hot Keys Asked at: Amazon, Google, Meta, Uber · Prerequisites: Caching and Consistent Hashing
1. Understanding the Problem
A distributed cache is a pool of machines that hold hot data in RAM so your database does not have to answer the same question ten thousand times a second. You SET a key with a TTL, you GET it back in under a millisecond, and when memory fills up the cache throws away whatever it thinks you need least.
💡 TTL = time to live, the number of seconds an entry is allowed to exist before the cache deletes it on its own. It is the crudest form of invalidation and, until Deep Dive 4, the only one this design has.
The word that matters here is volatile. A cache is allowed to lose your data. It will lose your data - on eviction, on a node crash, on a TTL firing, on a failover that drops the last few writes. Everything in this design follows from accepting that. If you need the data to survive, you are designing a key-value store instead, and the answer looks completely different: write-ahead logs, quorum writes, Merkle-tree repair. None of that appears here.
Real examples: Redis Cluster, Memcached (Facebook runs it in front of their MySQL tier), Netflix EVCache, Amazon ElastiCache, Hazelcast.
2. Naive First Cut
flowchart LR
APP["App Server"]
CACHE["Redis<br/>single node"]
DB[("Origin DB<br/>Postgres")]
APP -->|"1. GET key"| CACHE
APP -->|"2. On miss query DB"| DB
APP -->|"3. SET key"| CACHE
One Redis box. Every app server talks to it. Cache miss falls through to Postgres and the app writes the value back.
Why this breaks:
- Memory ceiling - the working set is 500 GB.
💡 The working set is the slice of your data that is actually being asked for right now, not the whole database. A cache only needs to hold this slice, which is why 500 GB of cache can front many terabytes of Postgres. The biggest sensible cache instance gives you maybe 128 GB of usable RAM. You cannot hold the data. - Single point of failure - that box reboots and your hit rate goes to zero instantly. Every one of the 900K reads/sec lands on Postgres at the same moment. Postgres dies in about two seconds.
- No eviction story - fill 128 GB with no eviction policy configured and Redis starts rejecting writes with out-of-memory (OOM) errors, or the kernel OOM-killer takes the whole process.
- One hot key ruins everything - a single celebrity key at 100K ops/sec saturates the one thread serving it, and latency for every other key on that box goes with it.
💡 A hot key is one key that attracts a wildly disproportionate share of requests - the celebrity profile, the front-page product, the global config everyone reads. It is the one problem on this page that adding machines cannot solve, because a key hashes to exactly one place. Learn more → - Stale forever - nothing invalidates a key when the row behind it changes. Users see last Tuesday’s price.
The rest of the doc evolves this into a shardable, survivable cache tier.
3. Prior Art We’re Drawing From
- Redis Cluster - 16384 fixed hash slots, slots assigned to primaries, gossip over a separate cluster bus for membership and failure detection, replicas promoted automatically. Contributes the slot-indirection trick: clients hash to slots, not nodes, so resharding moves slots rather than recomputing key ownership. (Redis cluster specification)
- Facebook’s memcache tier - the NSDI 2013 paper Scaling Memcache at Facebook documents leases to kill stampedes, a “gutter” pool to absorb traffic for a dead server, and batching to cut round trips. Contributes the single most useful framing of a cache as a lossy, best-effort layer whose semantics are deliberately weak. (Scaling Memcache at Facebook, NSDI 2013)
- Netflix EVCache - memcached-based, deliberately named for Ephemeral, Volatile Cache. Writes fan out to every availability zone so a zone loss costs you nothing, and reads stay zone-local. Contributes replicate-for-availability rather than replicate-for-durability. (Netflix EVCache)
- Caffeine (W-TinyLFU) - a Java near-cache whose admission policy uses a frequency sketch to decide whether a new entry even deserves to displace an existing one. Contributes the idea that admission matters as much as eviction. (Caffeine, and the paper TinyLFU: A Highly Efficient Cache Admission Policy)
- Memcached - the simpler, multithreaded counterpoint. Slab allocator, no replication, no persistence, lower per-key overhead. Contributes the reminder that a cache does not need features to be excellent at being a cache. (memcached.org)
4. Functional Requirements
Core (Top 3)
- GET and SET with a TTL - read a key in single-digit milliseconds at p99, write a key with an optional expiry after which it disappears on its own
💡 p99 = the time that 99% of requests come in under, so the slowest 1 in 100 is the one being bounded. Averages hide these; a cache with a 0.3 ms average and a 200 ms p99 is a cache that stalls somebody’s page load every hundred requests. Learn more → - Scale past one machine’s memory - the working set must spread across many nodes, and adding a node must not invalidate the cache
- Survive losing a node - one machine dying costs you a slice of the cache, not the whole thing, and the cluster keeps serving in the meantime
Discussion
Newest first