Back to roadmap
Module 8 · Caching, Queues, Async WorkDay 07025 min

Cache Patterns

Cache-aside, write-through, write-behind — different shapes, different tradeoffs.

Day 070

Cache Patterns

App
service
Cache
cache
DB
datastore
Signal path
Cache-aside flow
App
service
flow
Cache
cache
Cache
cache
flow
DB
datastore
Memory hook

Cache Patterns: cache-aside, write-through, write-behind

Mental model

absorb bursts before they become outages

Design lens

DEL vs SET on write — DEL is safer; SET avoids miss.

Recall anchors
Cache-asideWrite-throughWrite-behind

Why it matters

Cache-aside is the most common: app reads cache, falls through to DB on miss, populates cache. Write-through writes via cache to DB. Write-behind queues writes for later.

Deep dive

Cache-aside is robust to cache failure but invites stale reads after writes.

Write-through synchronizes cache and DB but slows writes.

Write-behind risks data loss on crash; reserve for non-critical paths.

Demo / scenario

Cache user profile.

  1. Read: GET cache.user; if miss, DB; SET cache TTL=300.
  2. Write: UPDATE DB; DEL cache.user (or SET).
  3. Stale window between DB update and cache delete is brief.
  4. Use TTL as belt-and-suspenders.

Tradeoffs

  • DEL vs SET on write — DEL is safer; SET avoids miss.
  • TTL caps drift even if invalidation fails.
  • Race conditions between read miss and concurrent write — use SET NX.

Diagram

App
Cache
DB
miss → DB
write → DB; del cache
Cache-aside flow.

Mind map

Check yourself

Loading quiz…

Sources & further reading