A thundering herd is a crowd of requests that all wake up and go for the same thing at once. The version that takes sites down is the cache stampede: a cache has been hiding most reads from the database, the cached entries run out together, and every miss goes to the database in the same moment. The database was sized for the trickle the cache let through, not for the flood.
The experiment
A gift catalog serves about 900 requests a second, growing to about 2,340 over two minutes. Nearly all of it is reads, and 97% of those can be cached. Fourteen small app servers read through a 2 GB cache to a small SQL database. Before the incident the cache answers 83% of reads, so the database only sees the misses.
Every page in the cache was written in the same busy minute with the same five-minute TTL. At 1:10 in the run, all of them expire together. We ran it five ways with the same seed, changing one setting each time.
Share of requests failing each second around the expiry
- As shipped
- One refill per key
For three seconds, about half of all requests failed. The worst second lost 54.2%, and the p95 peaked at 6.4 seconds. Then it was over: under 1% errors by 1:16 and none by 1:19. Short, but every page view in those seconds was a failure.
| Change | Errors, 20 s after expiry | Worst second | Simulated cost / month |
|---|---|---|---|
| None (as shipped) | 7.9% | 54.2% | $875 |
| TTL raised to 1 hour | 6.5% | 53.6% | $875 |
| Jitter expiry | 0% | 0% | $875 |
| One refill per key | 0% | 0% | $875 |
| Medium database | 0% | 0% | $1,092 |
Why it happens
Two things have to line up. First, many entries expire in the same instant, which happens whenever they were written together with the same TTL: a deploy that warms the cache, a restock email, a cache restart. Second, every miss goes to the database on its own. If a thousand requests miss the same gift page, that is a thousand identical database reads.
The cruel part is the refill. The cache only fills back up as fast as the database answers, and the database is the thing that is drowning. In the run the hit rate fell from 83% to 13% in one second and took about fifteen seconds to get back to 80%, because the reads that would refill it were the ones failing.
Why a longer TTL did not fix it
A one-hour TTL raises the hit rate on a normal day (93% instead of 83% here). It does nothing about the moment the keys expire together. The run forces that moment to come, and when it did, the one-hour cache failed 6.5% of requests against 7.9%: barely different. A longer TTL changes when the stampede happens, not whether.
What fixes it
Jitter the expiry. Add a little randomness to each entry's TTL, so entries written together run out over a spread of time. The misses arrive as a drizzle the database can absorb. Errors: 0%.
Let one request refill each key. When many requests miss the same key, one goes to the database and the rest wait for its answer. This is often called request coalescing or single flight. A thousand misses on one page become one read. Errors: 0%, and the p95 stayed around 50 ms.
Size the database for a cold cache. A medium database survived the full stampede with no errors. It also added $217 a month, every month, to cover a few seconds of trouble. It is a real answer, and the most expensive one.
The first two cost nothing in this model, which is why they are worth knowing. The third is what you reach for when you cannot change the cache.
About these numbers
They describe InfraSim's simulation, which models flows of requests per tick rather than individual requests, with a fixed seed so every run repeats exactly. Costs are the game's simulated monthly figures, not any provider's prices.
Break it yourself
This is Advent of Outages door 2, "All at Once": the same catalog and the same expiry, free in your browser with no account. The door opens on 2 December and stays open after.
In the InfraSim app, Everything Expires at Once (chapter 13, War Stories) is the same failure at a bigger scale, and Dependency Down (chapter 5, Reliability) takes the cache away entirely for 55 seconds.
New to InfraSim? The first five campaign levels play in your browser right now. No install, no account.