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
StrategyHowGood for
Cache-aside (lazy loading)App checks cache; on miss loads DB and fills cacheMost read-heavy data
Read-throughCache library loads from DB itselfSimpler app code
Write-throughWrite to cache and DB togetherData read right after write
Write-behindWrite to cache, flush to DB asynchronouslyHigh write throughput (risk of loss)
Refresh-aheadRefresh popular keys before expiryPredictable hot data

Eviction policies: LRU (least recently used — most common), LFU, FIFO, TTL expiry.

Pitfalls and fixes:

ProblemFix
Stale dataShort 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 restartWarm up popular keys