Full answer
Requirements
Functional:
get,setwith TTL,delete, multi-get, and compare-and-set.- Evict entries when memory is full.
Non-functional:
- p99 latency around 1 ms inside a data centre.
- Scale out by adding nodes without remapping most keys.
- Survive a node failure with a short window of reduced hit ratio, not an outage. The cache is not the source of truth; losing some data is acceptable.
Capacity estimates
Assumptions: 1 TB of hot data; average value 1 KB; 1M operations per second at peak, 90% reads; servers with 64 GB RAM of which 50 GB holds data (the rest for the OS, connection buffers, fragmentation); one replica per primary.
- Primaries: 1 TB / 50 GB = 20 nodes; with one replica each, 40 nodes.
- Operations per primary: 1M / 20 = 50,000 per second; well within the range a tuned in-memory server can handle, leaving headroom.
- Network: 1M x 1 KB = about 1 GB/s across the cluster, about 50 MB/s per primary.
- Item count: 1 TB / 1 KB = 1 billion keys; per-key overhead (pointers, expiry, LRU fields) of about 50 to 100 bytes adds 50 to 100 GB, which must be included in sizing.
API
GET key -> value | miss
SET key value [EX seconds] [NX | XX]
DEL key
MGET k1 k2 k3 ... batched read
CAS key expected_version new_value
Data model on each node
- A hash table from key to entry:
value,expires_at,version, LRU or LFU metadata. - Memory allocated in size classes (slab allocation) to limit fragmentation.
- Expiry: lazily on access plus a background sampler that removes expired keys.
- Eviction: approximate LRU by sampling a few keys and evicting the oldest, avoiding a global linked list.
High-level design
flowchart LR
A[App server + cache client] -->|"hash of key, then slot, then node"| N1[Primary 1]
A --> N2[Primary 2]
A --> N3[Primary ...20]
N1 --> R1[Replica 1]
N2 --> R2[Replica 2]
CM[(Cluster map / config service)] -. updates .-> A
CM -. health, failover .-> N1Partitioning
Use 16,384 fixed hash slots (as Redis Cluster does): slot = CRC16(key) mod 16384, and assign slot ranges to nodes. Adding a node moves some slots, not individual keys by recomputation, and migration can proceed slot by slot. Consistent hashing with virtual nodes gives the same property and is common in client-side Memcached setups.
Clients cache the slot map and send requests straight to the right node (one network hop, no proxy). If a slot moved, the node replies with a redirect and the client refreshes its map. A proxy layer (Twemproxy or Envoy) is an alternative that keeps clients simple at the cost of an extra hop.
Replication and failover
Each primary streams writes asynchronously to its replica. A failure detector (gossip among nodes, or a separate coordinator) marks a primary down after missing heartbeats, promotes the replica and updates the cluster map. Asynchronous replication means the last few writes can be lost; for a cache that is acceptable. Require agreement from a majority of observers before failover to avoid split brain on a network blip.
Bottlenecks and trade-offs
- Hot keys: one key on one node can exceed its capacity. Use client-side local caching for a few seconds or replicate the key under several names.
- Big values: a 5 MB value blocks a single-threaded server and saturates the network; cap value size and store big objects elsewhere.
- Thundering herd after failover: a cold replacement causes misses that hit the database. Keep replicas warm (they already are, with replication) and add request coalescing in clients.
- Multi-get fan-out: an
MGETof 100 keys may touch 20 nodes; the slowest node decides latency. Group keys that are read together using hash tags ({user:77}:profile,{user:77}:settingsshare a slot). - Consistency: the cache can serve stale data after the database changes; invalidation is the application's job (TTL plus delete on write).
- Replication vs memory: a replica doubles memory cost. Some teams skip replicas for pure caches and accept a hit-ratio dip; that is fine only if the database can absorb the extra misses.
By level
Fresher: Explain splitting keys across several cache servers by hashing, LRU eviction and TTL, and what happens when one server fails. A fresher should say that a cache is not the source of truth.
Mid: Size the cluster, choose hash slots or consistent hashing, describe client routing, replication and failover, and per-node memory layout. Expect memory sizing that includes per-key overhead.
Senior: Cover slot migration, split-brain protection, hot keys, big values, cross-zone placement, warm-up after failure, cost of replicas versus database headroom, and operational tooling. Expect protection against split brain during failover.
Key points
- Partition with hash slots or consistent hashing; clients route with a cached map
- Size by memory including per-key overhead, then check ops per node
- 1 TB at 50 GB usable per node needs about 20 primaries at about 50k ops/s each
- Async replicas plus majority-based failure detection for fast failover
- Hot keys, big values and multi-get fan-out are the main bottlenecks
Common mistakes
- Ignoring per-key memory overhead and fragmentation in sizing
- Treating the cache as durable storage
- Using hash mod N and remapping most keys on scale-out
- Allowing very large values on a single-threaded engine
Follow-ups
- How would you migrate a slot to a new node without errors?
- What happens to in-flight writes during failover?
- How would you support a 2 TB working set next year?
Tips
- Size memory first, then operations, then network
- Explain how a client finds the right node
- State clearly that the cache is not the source of truth
Evaluation rubric
- Requirements with latency and failure behaviour
- Correct sizing math with overhead
- Partitioning, routing, replication and eviction designed coherently
- Bottlenecks and trade-offs covered