Design a Distributed Cache
Understanding the Problem
⚡ What is a distributed cache? A distributed cache keeps frequently read data in RAM across a fleet of servers, so applications can fetch it in well under a millisecond instead of querying a slower database. It holds a copy, not the original: anything in it can be evicted or lost, and the application falls back to the source of truth. Memcached and Redis are the usual examples, and large sites run clusters of hundreds or thousands of them.
This question comes in a few shapes: "design memcached," "implement an LRU cache, now make it distributed," or "design an in-memory database." They share the same core. You need a fast single-node store with eviction, a way to spread keys across nodes, and a plan for what happens when a node disappears. The in-memory database version adds one more demand, durability, and we'll handle it as its own deep dive.
Functional Requirements
Core Requirements
- Users should be able to
seta value under a key with an optional TTL, andgetordeleteit. - The cache should evict entries automatically when memory is full, keeping the most useful ones (LRU or similar).
- Users should be able to read and write many keys in one call (
mget), and do atomic updates like increment and compare-and-set.
Below the line (out of scope)
- Rich data types like sorted sets and streams. We'll treat values as opaque bytes.
- Queries over values, and transactions across keys.
- Cross-region replication of the cache.
Non-Functional Requirements
Ask your interviewer about the workload the cache sits in front of. For our design:
Core Requirements
- The system should hold a working set of 10 TB, with values averaging 1 KB and capped at 1 MB.
- The system should serve 5M reads/sec and 500k writes/sec at peak, with p99 latency under 1 ms inside the data center.
- The system should keep the hit rate high (99%+) and stay up when nodes fail, without pushing a flood of misses onto the database.
- The system should scale out by adding nodes, with only a small fraction of keys moving.
- Cached data may be briefly stale, but it should converge to the database within seconds of a write.
Below the line (out of scope)
- Strong consistency with the database. A cache is a copy, and we'll be explicit about how stale it can get.
- Authentication and multi-tenancy.
Now the math. With servers that give about 200 GB of RAM to the cache, 10 TB takes about 50 nodes. Traffic per node is then around 110k operations a second, and at 1 KB each, about 110 MB/s of network. A tuned memcached node handles several times that, so RAM sets the node count, not CPU.
The more important number is behind the cache. Say the database can serve 50k reads a second. At 5M reads a second, a 99% hit rate leaves 50k misses a second, which is exactly what the database can take. A 98% hit rate doubles that. And if one of 50 nodes dies, the 2% of keys it held all miss at once, adding 100k reads a second to a database that was already at its limit.
This is the insight to say out loud: at this scale, the cache isn't an optimization, it's load-bearing. The database can't survive without it, so a cache node failure is a database outage unless we design for it.