Topic 088 / Advanced
Cache stampede (thundering herd) and hot keys
Chapter 6.2 · Distributed Caching and CDNs
What and why
Two related failure modes appear once a distributed cache (→ 87) is under real load, neither handled by plain cache-aside (→ 83). Stampede: a single popular key expires (or a cold cache starts), and many concurrent requests all miss at once, all recomputing the same value against the database simultaneously. Hot key: one key receives disproportionate traffic, overloading the single node or shard holding it even though the cluster overall has spare capacity. Builds on: → 87 (Redis and distributed caching).
Diagram
| Problem | Symptom | Defense |
|---|---|---|
| Stampede | N requests miss the same expired key at once; N queries fire together | request coalescing (one query, N waiters), probabilistic early refresh, stale-while-revalidate |
| Hot key | one key, one shard/node overloaded while others sit idle | local near-cache, key replication, key splitting |
How it works
- A stampede begins the moment a popular key's TTL (→ 85) expires or a cache is cold; every concurrent reader sees a miss independently, because none of them knows another reader already started refilling.
- Request coalescing makes only the first miss trigger the real database query; other concurrent misses for the same key wait on that one in-flight result instead of issuing their own.
- Probabilistic early refresh has each reader, as a TTL approaches, refresh with a small and increasing probability rather than waiting for a hard expiry — spreading refreshes out instead of synchronizing them on the deadline.
- Stale-while-revalidate serves the just-expired value immediately (accepting one more TTL's worth of staleness) while exactly one background refresh updates it — readers never wait on the database.
- A hot key is a distribution problem, not a staleness one: a near-cache keeps a local copy inside each application instance so most reads never leave the process; key splitting breaks one hot logical key into several physical ones, combined on read, trading read-side aggregation for spread write load.
Practical example
(numbers illustrative). A key is read 1,000 times/second; its TTL expires, and recomputing the value takes ~20 ms. Without coalescing, roughly 1,000 × 0.02 = 20 concurrent requests miss and query the database in that instant — a 20× spike at one moment. With request coalescing, exactly 1 query fires and the other 19 wait on its result — a 20× reduction in database load for that instant.
When to use it
- Use stampede defenses when: a key's read rate × recompute time exceeds a handful of concurrent misses — the practical example's arithmetic.
- Use hot-key defenses when: cluster-wide utilization looks fine but one shard or node is saturated.
- Avoid adding coalescing or near-cache complexity to keys measurement shows aren't actually hot.
- Failure scenarios: a cache flush (deploy, restart, a full clear) turning every key cold at once — a stampede across the whole keyspace, not just one key; stale-while- revalidate silently masking a real upstream outage behind "stale but fine" longer than acceptable; key splitting paid for on keys that are never actually read together.
- Simpler alternative: a slightly longer TTL plus jitter (randomizing each key's exact expiry by a few percent) — cheap, no new mechanism, and already keeps most keys from expiring in the same instant.
Practice
(advanced — scenario and lab, Redis, 20–30 min).
- Expire a key, then fire 50 concurrent reads against it; count the resulting database queries with no defense, then add request coalescing and recount.
- Judge three scenarios — a celebrity's profile page, a per-user settings page, a global feature-flag value — for whether stampede defense, hot-key defense, both, or neither is warranted, and defend each choice.
- Add jitter (± a few percent) to a batch of keys' TTLs; show expirations no longer land in the same instant.
Done when the coalesced run produces exactly one database query regardless of concurrent reader count.
Shared lab setupKey takeaway
Stampede is a timing problem (many misses at once); hot key is a distribution problem (one key, one shard) — they share a cause (popularity) but need different defenses. Done when you can: compute a key's concurrent-miss exposure from its read rate and recompute time, and name the matching defense. Used next in → 96 (rate limiting, which can also protect a hot key), → 99 (fan-out on write, where a celebrity account is the canonical hot key); Projects 2 and 13.
References
None — this topic makes no external claims.
Source directory