Roadmap › System Design — High-Level Design (HLD) from Basics to Case Studies › Module: Building Blocks
Lessons in this course (14)
5. Caching — where, strategies and pitfalls
Intermediate★ Must knowLesson 5 of 14
text
Browser cache → CDN → API gateway/output cache → application in-memory cache → distributed cache (Redis) → database buffer pool| Strategy | How | Good for |
|---|---|---|
| Cache-aside (lazy loading) | App checks cache; on miss loads DB and fills cache | Most read-heavy data |
| Read-through | Cache library loads from DB itself | Simpler app code |
| Write-through | Write to cache and DB together | Data read right after write |
| Write-behind | Write to cache, flush to DB asynchronously | High write throughput (risk of loss) |
| Refresh-ahead | Refresh popular keys before expiry | Predictable hot data |
Eviction policies: LRU (least recently used — most common), LFU, FIFO, TTL expiry.
Pitfalls and fixes:
| Problem | Fix |
|---|---|
| Stale data | Short TTL, delete key on update, versioned keys |
| Cache stampede (many requests miss at once and hit the DB) | Lock/single-flight per key (HybridCache does this), early refresh, jittered TTLs |
| Hot key (one key gets huge traffic) | Local in-memory copy, replicate the key, split it |
| Cache penetration (requests for keys that don't exist) | Cache "not found" briefly, Bloom filter |
| Cold start after restart | Warm up popular keys |