# What is a cache stampede?

> A cache stampede happens when many requests miss the same expired or missing cache entry at the same moment and all recompute it at once. The sudden burst of identical queries hits the database together, can overload it, and turns a brief expiry into a long slowdown or outage.

- Author: Zeeshan Adil (https://www.internalswithzee.com/about)
- Published: 2026-11-12
- Source: https://www.internalswithzee.com/concepts/cache-stampede

Also called: thundering herd, dogpile effect.

## Why it happens

A cache works because the next reader finds a copy and never touches the database. That only holds while a copy exists. The moment it expires, the next reader computes it again. If readers arrive one at a time, that is one query. If thousands arrive in the time that one query takes, every one of them also finds no copy, and every one sends its own query.

So the size of the burst is not the traffic over a minute. It is the traffic that arrives during one recompute. A page read 20,000 times a second and recomputed in 12 ms sends about 240 identical queries before the first answer is stored.

## How to recognize it

- Database load spikes at a regular interval equal to a cache TTL.
- The queries in the spike are nearly all the same query, for the same key.
- Latency rises for everything on that database, not only the cached page.
- After a deploy, restart or cache flush, many keys go cold together and the spike is wider.

## What fixes it (and what does not)

Jitter (a random TTL) helps when many different keys expire together, because it spreads their expiries out. It does nothing for one hot key: the key still expires once, and the same crowd is still waiting.

What does help:

1. Serve the old copy past its freshness limit while one refresh runs behind it (see [stale-while-revalidate](/concepts/stale-while-revalidate)).
2. Let concurrent requests for the same key share one in-flight query (see [request coalescing](/concepts/request-coalescing)).
3. Put a separate budget on refresh queries, so that when every cache is cold the database still sees a bounded rate and the rest get a fast error.

```
t=0   entry expires
t=1   200 requests miss, 200 queries start
t=2   database slows, none finish before the deadline, nothing refills the cache
```

## Measured

These numbers come from my [cache stampede challenge](/challenges/distributed-systems/a-popular-post-takes-down-the-database), a simulation (no real database) of 20 API processes sharing an 8-core PostgreSQL that can safely run 500 of these queries a second. With 20,000 reads a second for one post and a plain 30-second TTL, 100% of reads were served before the expiry and 0% after it. The database started 19,819 queries a second and finished none in time. At 5,000 reads a second the same expiry was a blip; at 10,000 it was an outage.

For one hot post, jitter did not change the count: 263 queries with no jitter, 257 with 20% jitter. For 10,000 posts cached together, the same jitter cut the worst second from 8,899 queries to 908.

## Related

- Challenge to run: https://www.internalswithzee.com/challenges/distributed-systems/a-popular-post-takes-down-the-database
- Related concept: https://www.internalswithzee.com/concepts/metastable-failure
- Related concept: https://www.internalswithzee.com/concepts/request-coalescing
- Related concept: https://www.internalswithzee.com/concepts/stale-while-revalidate
- Related concept: https://www.internalswithzee.com/concepts/hot-key

## Sources

- [AWS Builders' Library: Caching challenges and strategies](https://aws.amazon.com/builders-library/caching-challenges-and-strategies/)
- [Nishtala et al., Scaling Memcache at Facebook (NSDI 2013)](https://www.usenix.org/system/files/conference/nsdi13/nsdi13-final170_update.pdf)

