Definition
What it is
When a cache holds a hot key and that key expires, every request that arrives before the next fill misses the cache and hits the origin. If the key had 10,000 requests per minute, the origin now sees 10,000 simultaneous queries. Most databases cannot sustain this for even one second. Latency spikes; timeouts cascade; the system either recovers (slowly) or falls over.
The same pattern appears with connection pool resets, DNS TTL expiry, TLS cert rotation, service restarts (all workers reconnect at once), and cron jobs scheduled at :00:00.
Why it matters
A single hot key expiry can take down a production database. Teams often discover the problem in the worst possible way — during peak traffic. The fix is small (a lock) but only if you know it exists.
Prevention strategies
Strategy | Mechanism | Best for |
|---|---|---|
Lock | First request takes mutex, others wait | Critical hot keys |
Single-flight | Only one refresh; others get stale | Library-level |
Jitter TTL | Randomize expiry ±10% | Even distribution |
Cache warming | Pre-fill before expiry | Known hot keys |
Serve stale while refreshing | HTTP responses |
The thundering herd flow
sequenceDiagram participant C as Clients (N) participant Cache participant DB Note over Cache: T+0 key expires C->>Cache: GET Cache-->>C: MISS C->>DB: query (N times) Note over DB: CPU saturates DB-->>C: slow or timeout Note over Cache: One refill succeeds C->>Cache: GET Cache-->>C: HIT
How to prevent it
Killing the herd
Single-flight / mutex
In Redis:
SET lock:key 1 NX EX 10. Winner refills, others wait or serve stale.Jitter TTL
If TTL is 60s, use 60 ± 6s. Spreads expiry across 12 seconds.
Stale-while-revalidate
Serve stale for up to 5 minutes while refreshing. Users never wait.
Warm cache before expiry
Cron every 30s re-fills known hot keys before they expire.
Backpressure
Limit concurrent origin queries via a semaphore. Reject excess.
Never broadcast
Push invalidation sends "revalidate", not "evict". Let the cache refill on next read.
Common failures
Origin
The image comes from animal stampedes: a small disturbance triggers a mass simultaneous flight. In computing the term was used informally in the 1990s (thundering herd on file locks) and formalized in HTTP caching discussions (notably by the Squid project and Cloudflare). The W3C and IETF discuss it in cache-related RFCs (RFC 7234).
A thundering herd is when a large number of processes all try to acquire the same lock or hit the same resource at the same time.
— Beej's Guide to Unix IPC, 2024 (paraphrase)
Evidence
FAQ
- Thundering herd vs cache stampede?
Same problem, different name. "Cache stampede" emphasizes the cache; "thundering herd" emphasizes the simultaneous requests.
- When does it happen?
Cache expiry, service restart, DNS TTL, cert rotation, cron at :00.
- Does CDN prevent it?
Partly. The CDN absorbs the burst if it has the key cached; if not, it forwards everything.
- Can I just use a longer TTL?
Longer TTL reduces frequency, not the burst. Jitter + locks are the real fix.
Connections
- cache
- cache-invalidation
- stale-while-revalidate
- single-flight
- backpressure
AWS (2015). Exponential Backoff and Jitter. https://aws.amazon.com/blogs/architecture/exponential-backoff-and-jitter/
Cloudflare (2024). What is a thundering herd? https://www.cloudflare.com/learning/performance/glossary/what-is-a-thundering-herd/
Etymology
Origin: metaphor
thundering(adjective): from Old English þunor, "thunder"herd(noun): from Old English heord, "group of animals"
Semantic network
● broader ● narrower ● related