Keep hot data close and cheap to read.
Where to cache
Browser, CDN, application memory, a distributed cache like Redis/Memcached, and the database’s own buffer. The closer to the user, the faster, but the harder to invalidate.
Patterns
• Cache-aside: app checks cache, on miss loads from DB and fills cache.
• Write-through: write to cache and DB together.
• Write-back: write to cache, flush to DB later (fast, risk of loss).
Eviction and invalidation
Eviction policies: LRU, LFU, FIFO, TTL. ‘There are only two hard things: cache invalidation and naming things.’ Use TTLs and explicit invalidation on writes, and beware cache stampedes when a hot key expires.