Dashboard
Courses
Problems
Badges
Theme
Sign inStart free
Courses/Reliability and Failure
Lesson 15 of 24

Caching under failure: stampedes and thundering herds

Keep caches from amplifying failures through stampedes, cold starts and synchronised expiry.

15 min read

The cache is load-bearing

A cache with a 95% hit rate means the database sees only 5% of reads. Lose the cache, or even part of it, and the database suddenly receives 20 times its usual read load. A cache that started as an optimisation has become a critical dependency. Either size the database to survive a cold cache, or design explicit protection for the moments when hit rate collapses: restarts, failovers, deploys that change key formats, and mass expiry.

Cache stampede

When a popular key expires, every request that misses at that moment goes to the database to rebuild it. For a key read 10 000 times per second with a 200 ms rebuild, that is up to 2 000 identical queries in flight at once, which can take the database down and keep the key from ever being rebuilt.

  • Request coalescing (single flight): one request rebuilds, the others wait for its result
  • Stale-while-revalidate: keep serving the old value while one background refresh runs
  • Probabilistic early refresh: refresh slightly before expiry, with a probability that rises as expiry approaches

Synchronised expiry and cold starts

If a batch job loads a million keys with the same one-hour TTL, they all expire together an hour later. Add jitter to TTLs, for example 60 minutes plus or minus 10%, so expiry spreads out. After a cache node restarts empty, warm it gradually: pre-load the hottest keys from a snapshot, or shift traffic to it a little at a time while the database is protected by a concurrency limit.

Serving stale on failure

When the source of truth is down, a slightly stale answer is often much better than an error. Keep expired entries around for a grace period and serve them when the database or upstream provider fails, marking the response as stale if the client cares. This turns a dependency outage into a freshness problem. The Feedback panel's failure tests reward designs where a cache can carry reads through a database failure.

Key takeaways

  • A high hit rate makes the cache a critical dependency; plan for it being cold
  • Coalesce misses and refresh in the background to prevent stampedes
  • Jitter TTLs and warm caches gradually after restarts
  • Serving stale data during an outage is usually better than serving errors

Sign in to track lessons and earn badges.

Surviving traffic spikesData integrity: checksums, idempotency and double entry

Challenge checkpoint

Course: 0/24 lessons · 0/10 challenges0%

Build it on the canvas, pass the review (70+) and mark it complete to count it toward the course badge.

  1. 5/10Design Flash-Sale TicketingCombine a waiting room, admission control, hot-key handling and seat holds to sell out without overselling or collapsing.MAJORHardPRO
All 10 challenges →