Design a distributed in-memory key-value cache
Role: Software Engineer
Category: System Design
Difficulty: hard
Interview Round: Other
Design a distributed cache: an in-memory key-value store that sits between application servers and a primary database and answers most reads so they never reach the database. Clients need three operations: store a value under a key (optionally with a time-to-live), read a value by its key, and delete a key. Entries expire when their TTL passes, and when a node runs out of memory the cache evicts old entries on its own.
Explain how a request finds the node that holds its key, how the cluster grows, and what happens when nodes fail or a few keys receive most of the traffic.
```hint Mapping keys to nodes
Count how many keys change owner under `hash(key) % N` when N goes from 10 to 11. A scheme where only a small slice of keys moves will matter for both scaling and failures.
```
```hint Where a node spends its time
With thousands of open connections on one node, think about the cost of one thread per connection and locks around a shared hash table. What would let a node serve many sockets without that cost?
```
```hint One key, one node
Even a perfect spread of keys puts every request for a single popular key on the same node. Think about how to serve that key from more than one place, and what that does to updates.
```
### Constraints & Assumptions
- Reads and writes should complete in under a millisecond at the cache node.
- The cluster should scale roughly linearly to tens of millions of requests per second and terabytes of cached data.
- A node failure must not take the cluster down, and data loss during node crashes or topology changes should be small.
- The workload is read-heavy.
### Clarifying Questions to Ask
- Does the application manage the cache itself (read the database on a miss and fill the cache), or should the cache read and write through to the database?
- After a write, may a read briefly return the old value, and how much recently written data can be lost if a node crashes?
- What are typical key and value sizes, and what is the ratio of reads to writes?
- Is LRU eviction acceptable as a default, or does the access pattern call for a different policy?
- Is this a single-region deployment?
### What a Strong Answer Covers
- Single-node data structures that give constant-time get, set and delete, plus LRU eviction and TTL expiry without scanning all of memory.
- A partitioning and routing scheme that moves only a small share of keys when nodes join or leave, balances load across nodes of different sizes, and avoids an extra network hop where possible.
- How one node serves many concurrent connections at sub-millisecond latency, and the limits of that model.
- Replication, failure detection and failover, with the consistency and data-loss trade-offs stated plainly.
- Handling of hot keys and expiry stampedes that would overload one node or the database.
### Follow-up Questions
- When you add a node, how do you move its share of keys without a burst of misses hitting the database?
- When would you choose cache-aside over write-through, and how do you keep the cache and database consistent under concurrent writers?
- How would the cache recover its contents after the whole cluster loses power?
- A network partition isolates a master with a few clients while its replica is promoted on the other side. What happens to the writes the old master accepts, and how do you limit the damage?
Overview: Design a distributed in-memory key-value cache that sits in front of a primary database and supports get, set with a TTL, delete, and eviction at sub-millisecond latency. It tests sharding and request routing, replication and failover, how one node handles heavy concurrency, and how to cope with hot keys.