System design lab · Episode 17

Cache problems: the stampede and the stale price

A cache makes reads fast, until two things happen. When a popular copy expires, everyone misses at the same moment and they all run to the database together. And when the data changes, the cache keeps giving the old answer until its copy expires. Follow the lessons next to the picture.

  1. Episode 1Load balancer
  2. Episode 2Cache
  3. Episode 3Rate limiter
  4. Episode 4Message queue
  5. Episode 5Database index
  6. Episode 6Replication and consensus
  7. Episode 7Circuit breaker
  8. Episode 8Backpressure
  9. Episode 9Retries and jitter
  10. Episode 10Consistent hashing
  11. Episode 11Partitioned log
  12. Episode 12Write-ahead log
  13. Episode 13Quorums
  14. Episode 14Unique IDs
  15. Episode 15The last ticket
  16. Episode 16Saga
  17. Episode 17Cache problems
  18. Episode 18Bloom filter
READERS100CACHE100REAL PRICE
Balls on the left: answers to readers (red: an old price). Balls on the right: database queriesReaders at the left, the cache in the middle, the database at the right

Controls

When the copy expires
When the price changes
The editor
Time

Want the details?

Two problems every cache has

A cache keeps a copy so most reads skip the slow database. Two things go wrong. A stampede (also called a thundering herd or dog-pile): a very popular copy expires, and in that moment hundreds of requests miss together and all ask the database. And stale data: the database changes, but the cache keeps giving the old copy until it expires.

How to read the bench
Part on the benchWhat it means in a real system
ReadersUsers or servers asking for one popular item.
Price card on the cacheThe cached copy. Red: older than the database.
Row of cellsTime left until the copy expires (its TTL).
Balls at the databaseQueries running; the dial counts them.
Red balls to the readersAnswers with an old price.
The numbers
  • 20 reads a second, a 5-second copy, a 0.8-second query; the database slows down above 6 queries at once.
  • In 20 seconds: every reader asks, 50 queries and up to 17 at once; one fetch, 4 queries; refresh early, 5 queries and no waiting after the first load.
  • Price change at 6 s with the cache left alone: 97 old answers; with delete on write: none.
In real systems
  • Single flight: in one process, keep a map of fetches in flight; across many servers, take a short lock in Redis (SET key NX PX 3000) and let others wait or serve the old copy.
  • Early refresh: refresh before expiry, with some randomness (“probabilistic early expiration”), so thousands of keys do not expire in the same second. Add jitter to TTLs for the same reason.
  • Serve stale while revalidating: CDNs and browsers can give the old copy and refresh it in the background (the HTTP header stale-while-revalidate).
  • Write strategies: cache-aside (the app reads the cache, falls back to the database, and deletes the copy on writes) is the most common. Write-through writes the cache and the database together. Write-back writes only the cache and saves to the database later: fast, but a crash can lose writes.
  • Delete, don't set: on a write, delete the copy rather than writing the new value into it. Two writes racing can leave the older value in the cache for a whole TTL.
Words to know
TTL
Time to live: how long a copy may be used.
Cache Stampede
Many requests missing the same expired key at once and all hitting the database.
Single Flight
Only one request fetches a missing key; the others wait for its answer.
Stale Data
A cached copy that is older than the source.
Cache Invalidation
Removing or updating copies when the source changes.