vishal patel
Applied in productionIntermediateUpdated 2026-09-23

Caching Strategies

Cache-aside, read/write-through, write-behind; TTLs, invalidation, stampede protection and multi-layer caches (browser → CDN → app → DB).

rediscdnperformanceinvalidation

Where caches live

diagram

Patterns

PatternHowWhen
Cache-aside (lazy)App reads the cache; on miss, reads the DB and fills the cacheDefault for read-heavy data
Read-throughCache library loads from the DB on missSame idea, less app code
Write-throughWrite the cache and DB togetherData read right after it's written
Write-behindWrite the cache; flush to the DB asynchronouslyVery write-heavy, loss-tolerant (counters)
Refresh-aheadRefresh hot keys before they expirePredictable hot sets

The hard part: invalidation

  • TTL as a safety net, always, even with explicit invalidation.
  • Event-driven invalidation: EntryPublished → purge the CDN path and delete the Redis key.
  • Versioned keys: entry:42:v17. New versions never collide, and old ones expire naturally.
  • Delete, don't update, the cache on write. This avoids races that write stale data back.

Failure modes

  • Stampede / thundering herd: a hot key expires and 10k requests hit the DB. Fix with a request-coalescing lock, early probabilistic refresh, or stale-while-revalidate.
  • Cache penetration: repeated requests for keys that don't exist. Cache negative results briefly, or use a Bloom filter.
  • Hot key: replicate it or use a local in-process cache.
  • Plan for the cache being down: the DB must survive at least degraded traffic.
Where I've used it

Headless CMS delivery is a caching problem first: published content sits behind a CDN, and every publish, unpublish or release deploy has to trigger precise purges. Getting purge scope right (by entry, content type, locale and environment) matters as much as the cache itself.

Cheatsheet

The whole topic on one page. Click to open full screen.

Sources & further learning

Videos, courses, docs and books I recommend for this topic.

Related topics