Opscraft
Caching & CDNs · Lesson 5 of 6 · 3 min read

Cache failure modes

Stampedes, avalanches and penetration

Enlarge diagramWithout protectionWith a lock and stale servingkey expires5,000 requests miss together5,000 identical queries hit the databaseone request takes the lock and rebuildsothers get the stale valuedatabase sees one query
Figure 1. A stampede: a hot key expires and every request goes to the database at once. A lock lets one request refill it.
Swipe sideways to see all of it, or tap to open it full screen.
Problem What happens Defences
Cache stampede (dogpile) A hot key expires and thousands of requests miss at once and all hit the database A lock so one request refills the key (SET key NX), stale-while-revalidate, refreshing hot keys early
Cache avalanche Many keys expire together, or the whole cache restarts empty TTL jitter, warming the cache before traffic, replicas, rate limiting the database path
Cache penetration Requests for keys that don't exist always miss (often an attack with random IDs) Cache "not found" results briefly, validate IDs, a Bloom filter of valid keys
Hot key One key gets so much traffic that its single shard saturates A small in-process cache in front, key replicas with suffixes, CDN caching

Correctness and security failures

  • Stale data after a write: the database changed but the cache still has the old value. Delete keys on write, keep TTLs short for data that matters, and for read-your-own-writes, read from the database right after an update.
  • Personal data in a shared cache: an endpoint that returns user data is cached by a CDN because it lacked private. One user sees another's account page. Default APIs to Cache-Control: private, no-store and allowlist what is public.
  • Web cache poisoning: an attacker sends a header that isn't in the cache key but changes the response (for example X-Forwarded-Host used to build links), and the poisoned response is cached for everyone. Only forward headers the app needs, and make anything that changes the response part of the key.
  • Web cache deception: tricking a CDN into caching a private page by appending a static-looking suffix (/account/profile.css). Cache by explicit path rules and content type, not by file extension alone.
  • Cold start after a flush: the cache restarts empty and the database can't handle full traffic. Size the database for a degraded mode, warm critical keys, and shed or queue load until the hit ratio recovers.