Caching Strategies
Caching strategies cheat sheet: cache-aside, read-through, write-through, write-behind and refresh-ahead compared, eviction policies, where caches live, Redis commands, HTTP cache headers, and how to handle stampedes, penetration and avalanches.
Cheat sheet · Offered as a template
In this diagram
- Caching Strategies
- Keep a copy of the answer close to where it is asked. The patterns, the eviction policies, and the ways it goes wrong.
- ① Why cache?
- Reads are far more common than writes
- Recomputing or refetching is slow or costly
- Slightly stale data is acceptable
- The trade: speed for freshness and memory
- ② Cache-aside, the default
- Request
- App
- Cache
- Database
- On a write: update the DB, then delete the key
- The app owns the logic. A cold or flushed cache only costs a slower first read.
- ③ Eviction policies
- ④ Caching patterns
- ⑤ Redis essentials
- redis-cli
- ⑥ Where caches live
- Browser
- Per user
- CDN
- Per region
- Reverse proxy
- Per site
- App memory
- Per process
- Redis
- Shared
- DB buffer pool
- Pages
- ⑦ HTTP caching
- Response headers
- ⑧ Three ways it goes wrong
- Stampede
- A hot key expires and every request rebuilds it at once
- Fix: a lock or single-flight, and jittered TTLs
- Penetration
- Lookups for keys that do not exist always reach the DB
- Fix: cache the miss, or a Bloom filter
- Avalanche
- Many keys expire together, or the cache goes down
- Fix: spread TTLs, replicas, a circuit breaker
- ⑨ Is it working?
- Hit ratio
- p99 read
- Hit ratio = hits ÷ (hits + misses). Below about 80%, look at key design and TTLs first.
- Also watch