Chapter 11: Caching — Strategies, Invalidation, CDNs
Cache Layers
Client ──► CDN (static assets, edge cache)
──► API Gateway cache (response cache)
──► Application cache (Redis/Memcached)
──► Database cache (query cache, buffer pool)
──► OS cache (page cache)
──► Hardware cache (CPU L1/L2/L3)
Each layer catches requests before they hit the next (slower) layer.
Caching Strategies
| Strategy | Read | Write | Consistency |
|---|---|---|---|
| Cache-Aside | Check cache → miss → read DB → fill cache | Write DB, invalidate cache | Eventual (stale until invalidated) |
| Read-Through | Cache handles DB read on miss | Write DB, invalidate cache | Eventual |
| Write-Through | Always from cache | Write cache + DB synchronously | Strong |
| Write-Behind | Always from cache | Write cache, async flush to DB | Eventual (risk of data loss) |
CDN (Content Delivery Network)
Without CDN:
User (Tokyo) ──── 200ms RTT ────► Origin Server (Stockholm)
With CDN:
User (Tokyo) ── 5ms ──► CDN Edge (Tokyo) ── cache hit ──► response
│ cache miss
▼
Origin Server (Stockholm)
Cache at CDN: static files (images, CSS, JS), API responses (with short TTL), video chunks
Cache Invalidation
"There are only two hard things in computer science: cache invalidation and naming things." — Phil Karlton
- TTL (Time-To-Live): simplest. Accept staleness for TTL duration.
- Event-based: invalidate on write. Requires pub/sub infrastructure.
- Version keys:
user:123:v5— new version = new key = no stale data.
Cache Eviction Policies
- LRU (Least Recently Used): evict oldest-accessed. Most common.
- LFU (Least Frequently Used): evict least-accessed overall.
- TTL: evict when time expires.
Key Takeaways
- Cache at every layer: CDN, app (Redis), DB, OS
- Cache-aside is the most common pattern (app manages cache)
- CDN for static content and geographically distributed users
- TTL is simplest invalidation — accept some staleness
- Only cache hot, frequently-read, expensive-to-compute data