Cache Strategies, Eviction and Invalidation
Choose between cache-aside, read-through, write-through, write-behind and write-around, pick eviction policies, and avoid stampedes and stale data.
Intermediate · about 3 hours · difficulty 3/5
Learn first: Caching Fundamentals and Cache Layers
"There are only two hard things in computer science: cache invalidation and naming things." The strategy you pick decides how stale data can get and what happens on failure.
Read and write strategies
- Cache-aside (lazy loading): app reads the cache; on a miss it reads the DB and fills the cache. On writes it updates the DB and deletes the cache key. The most common pattern.
- Read-through: the cache library itself loads from the DB on a miss.
- Write-through: write to cache and DB together. Cache is always fresh; writes are slower.
- Write-behind (write-back): write to cache, flush to DB later. Very fast writes; risk of data loss if the cache dies.
- Write-around: write only to the DB; the cache fills on the next read. Good when written data is rarely read soon.
Eviction policies
When memory is full: LRU (least recently used, the usual default), LFU (least frequently used, good for stable hot sets), FIFO, random, or TTL-based. Redis offers several maxmemory policies; choose one deliberately.
Classic failure modes
- Cache stampede / thundering herd: a hot key expires and thousands of requests hit the DB at once. Fix with request coalescing (one loader, others wait), a lock per key, or refreshing early with jittered TTLs.
- Cache penetration: repeated lookups for keys that do not exist. Cache the "not found" result briefly or use a Bloom filter.
- Cache avalanche: many keys expire together or the cache cluster dies. Add TTL jitter and protect the DB with rate limits.
- Inconsistency: delete-then-write races can leave stale values. Delete after the DB commit, keep TTLs as a safety net, or drive invalidation from the database change stream (CDC).
Pitfalls
- Updating the cache value on write instead of deleting it, and racing with concurrent readers.
- Same TTL for every key, so they all expire together.
You are ready to move on when you can pick a strategy for a product catalogue, a leaderboard and a user session store, and explain how you would prevent a stampede on a viral product page.
Key concepts
- Cache-aside
- Read-through and write-through
- Write-behind and write-around
- LRU and LFU eviction
- Cache stampede and request coalescing
- Cache penetration and avalanche
- TTL jitter
- CDC-driven invalidation
Mini project
Implement an LRU cache from scratch with O(1) get and put, then build a cache-aside wrapper around a slow function that coalesces concurrent misses for the same key. Show with a load test that the backend sees one call per expiry.
Check yourself
In cache-aside, why is it usually safer to delete the cache key after updating the database than to write the new value into the cache?
Free resources
- Caching patterns (AWS)
- Scaling Memcache at Facebook (USENIX)
- Redis key eviction (Redis)