A popular item expires and thousands of requests hit the database at the same moment.
A cache stampede (also called a thundering herd or dogpile) happens when a popular cached item expires and thousands of requests all go to the database at the same moment to rebuild it.
Imagine a product page cached for 10 minutes. It is read 5,000 times a second. For 10 minutes, the database sees almost nothing. At minute 10 the entry expires. In the next fraction of a second, thousands of requests find the cache empty. Each one asks the database to build the same page.
We need to understand it because a cache stampede is a common way for a healthy system to fall over suddenly.
| Protection | How it works | Good and bad |
|---|---|---|
| Lock / single flight (request coalescing) | On a miss, one request takes a lock and rebuilds the value. The others wait for it, or share its result. | Database sees 1 query per key. Waiting requests add delay. A stuck lock needs a timeout. |
| TTL jitter | Add a small random extra time to each TTL (for example 10 minutes plus 0 to 60 seconds). | Keys stop expiring together. Does not help if ONE hot key expires. |
| Stale-while-revalidate | When the value expires, keep serving the old one. One request refreshes it in the background. | Users never wait. They may see slightly old data. |
| Early (probabilistic) refresh | Refresh a little before expiry. Each read has a small, growing chance of starting the refresh as the expiry gets close. | The value never really expires under load. Slightly more work than needed. |
6 hot keys are all filled at second 0 and stay fresh for 10 seconds. Rebuilding one takes the database 2 seconds. The database can handle about 200 queries per second.
A simplified model with a simulated clock: the numbers show the idea, not a real database.
lock:product:42 only if it does not exist, with a short expiry. The one that succeeds rebuilds. The others wait a few milliseconds and read the cache again.| Term | Meaning in easy words |
|---|---|
| Cache stampede | Many requests rebuild the same missing key at the same time |
| Thundering herd / dogpile | Other names for the same problem |
| TTL | Time to live: how long a cached value stays fresh |
| Jitter | A small random amount added so things do not happen together |
| Single flight / coalescing | Only one request does the work. Others share its result |
| Stale-while-revalidate | Serve the old value while a fresh one is fetched in the background |
| Early refresh | Refresh before the value expires |
| Hot key | A key with very high traffic |
| Load shedding | Refuse some requests on purpose to protect the system |
| Circuit breaker | Stops calls to a failing system for a while |
| Negative caching | Caching a βnot foundβ answer for a short time |
| Protection | We gain | We lose |
|---|---|---|
| Lock / single flight | One query per key | Some users wait; needs lock timeouts |
| TTL jitter | No mass expiry | Does not protect one single hot key |
| Stale-while-revalidate | Never wait, no spike | Briefly old data |
| Early refresh | Key never expires under load | A bit of wasted work |
| Load shedding | The DB survives anything | Some users get errors or defaults |