Caching and Performance Engineering · Caching Fundamentals
Cache Invalidation and Stampede Protection
Keep caches correct and protect the database when hot keys expire.
Invalidation options: TTL expiry, explicit deletion after writes, versioned keys (bump a version to invalidate a whole group), and event-driven invalidation (consume change events/CDC). Hot keys that expire can cause a stampede of simultaneous recomputations; prevent it with request coalescing (single-flight), short locks, probabilistic early refresh, or serving stale data while revalidating in the background.
Versioned keys: catalog:v{N}:product:{id} → INCR catalog_version to invalidate all
Single-flight: SET lock:product:42 1 NX EX 10 → only the lock holder recomputes
Early refresh (XFetch): recompute if now - (ttl_remaining * beta * log(rand())) > expiry
Stale-while-revalidate: return stale value immediately, refresh asynchronously
Common pitfalls
- Invalidating before the database commit (another request re-caches old data).
- Locks without TTLs (stuck forever if the holder dies).
- Synchronized TTLs across many keys.
Key points
- TTL, delete, version, events
- Coalesce concurrent misses
- Serve stale while revalidating
Further reading: Cache stampede
Practice questions
What is a cache stampede?
Answer
When a popular key expires, many concurrent requests miss and all recompute it from the database at once, potentially overloading it.
How do versioned cache keys help invalidation?
Answer
Incrementing a version number changes every key that includes it, invalidating a whole group of entries at once without scanning or deleting them.
Open in the app to mark lessons complete, take notes and track your progress.