Design a distributed caching layer
depth · 45 min · senior/staff
Setting
A read-heavy product catalog service is overwhelming its primary database at peak — the same few thousand popular items are re-read constantly, while millions of long-tail items are read rarely. You're asked to put a caching layer in front of it.
The ask
Design the caching layer: how it's populated, how it stays consistent with the database, and how it behaves when it's cold, oversized, or a node fails.
Constraints & scale
- ~1M reads/sec at peak, ~2% of that volume are writes to the underlying data
- Working set of hot items fits in memory across a modest cluster, but the full catalog does not
- Cache must not serve data more than a few seconds stale after a write, and must never serve a different value for the same key to two callers hitting different cache nodes at the same moment
- A cold cache (fresh deploy, node restart) must not cause a "thundering herd" of requests hammering the database for the same popular keys simultaneously
- One cache node failing should degrade gracefully, not take down the read path
What a strong answer covers
Requirements → estimates (working set size, hit-rate target) → cache topology and eviction policy → consistency/invalidation strategy → high-level diagram → 1–2 deep dives → trade-offs. Likely probes: cache-aside vs. write-through, the thundering-herd defense, and how invalidation propagates across nodes.
A 45-minute plan
- 0-5 min · Requirements
- 5-10 min · Estimates/API
- 10-25 min · High-level
- 25-40 min · Deep dive
- 40-45 min · Wrap
What to cover, in order
- Requirements
- Estimates (working set size, hit-rate target)
- Cache topology and eviction policy
- Consistency/invalidation strategy
- High-level diagram
- 1–2 deep dives
- Trade-offs
Where the follow-up questions go
Pick one of these for your deep dive before the interviewer picks for you.
- Cache-aside vs. write-through
- The thundering-herd defense
- How invalidation propagates across nodes
Key trade-offs
Decisions a strong answer weighs, and what pushes each one either way.
- Cache-aside or write-through. Cache-aside fills on a miss and is simple and tolerant of cache loss, but a write followed by a stale read is possible. Write-through keeps the cache current, but writes cost more and a cache of mostly cold items fills with data nobody reads. With 2% writes and a long tail, think hard about whether paying on every write is worth it.
- Invalidate or update on write. Deleting the key forces the next reader to repopulate, which is safe and creates a miss. Writing the new value avoids the miss and risks a race where an older value lands after a newer one. Deleting is the safer default; versioned writes make updating safe.
- TTL as backstop or as the mechanism. A TTL alone bounds staleness only up to its length, and the constraint here is a few seconds. A short TTL on top of explicit invalidation gives a hard bound if an invalidation is lost. The price is more database load from expiry, so jitter the TTLs.
- Client-side or proxy routing. Smart clients that hash keys to nodes avoid a hop and need to agree on cluster membership. A proxy tier centralises that logic and adds latency and a component to scale. The right answer depends on how many client languages and teams you have.
- Consistent hashing and rebalancing. Placing keys on a ring moves a small share of keys when a node joins or leaves, but the moved keys start cold and hit the database. Replicating hot keys to more than one node spreads load and creates the question of how copies stay in step.
Common mistakes
What goes wrong most often on this problem, and why it hurts.
- Saying "TTL" and stopping. Time-based expiry alone cannot meet a few-seconds staleness bound after a write, and short TTLs on everything bring the database load back.
- No defence against many callers missing the same key. A popular key expiring or a node restarting sends thousands of identical queries to the database at once. Coalescing requests per key is the usual fix.
- Hashing keys with modulo. Any change in node count remaps almost every key and empties the cache at the worst possible time.
- Letting two nodes hold different values for one key without saying how. If both can serve it, you need a story for who is allowed to cache a key and how an invalidation reaches every holder.
- Treating a node failure as a cache outage. If a lost node sends all its traffic to the database, the cache has become a single point of failure. Plan for the degraded case, including load shedding.
Graded on
Requirements & scoping · Estimation · API & data model · High-level architecture · Deep dive · Scalability & performance · Reliability & consistency · Trade-offs · Operability · Communication & drive