Cache Stampede
A hot key expires and every request stampedes the database to recompute it at once.
By the end of this lesson, prevent simultaneous recomputation when a popular cache entry expires.
A cache stampede (or dogpile) is a thundering herd aimed at your database. A popular key expires; suddenly thousands of concurrent requests all miss the cache and all try to recompute the same expensive value against the origin at once — often enough to take the origin down.
Expiry -> simultaneous misses
- When a hot key expires, many concurrent misses all recompute against the database, overloading it.
flowchart LR exp[Hot key expires] --> miss[N concurrent cache misses] miss --> db[(All recompute against DB)] db --> down[DB overload]
Defenses: single-flight / request coalescing (only one caller recomputes; others wait for the result), a short lock on the key during recompute, early/probabilistic recomputation (refresh before expiry so the key never fully lapses), and stale-while-revalidate (serve the old value while one worker refreshes).
Cache stampede
- Trigger
- A high-traffic cache key expires and many requests recompute it simultaneously.
- Symptom
- A sudden origin/database load spike on cache expiry.
- Blast radius
- The origin datastore and everything depending on it.
- Mitigation
- Single-flight recompute, locks, early/probabilistic refresh, stale-while-revalidate, TTL jitter.
What does 'single-flight' do during a stampede?
It lets only one caller recompute the value while the others wait for and share that single result, instead of all hitting the origin.