Lesson 5 / 32

Caching

Cut latency and database load with layered caching, cache-aside and write-through patterns, TTLs and eviction policies.

Where caches live

Browser, CDN, reverse proxy, application memory, and a shared store like Redis. Each layer absorbs reads so the database sees only the misses.

Write strategies

Cache-aside: app loads on miss, writes DB and invalidates. Write-through: write cache + DB together. Write-back: write cache now, DB later (fast, risky).

Always set a TTL

An expiry bounds staleness and stops a poisoned entry living forever. Add jitter to TTLs so keys don't all expire at once (thundering herd).

Quick check: Which eviction policy removes the entry unused for the longest time?

  • FIFO
  • Random
  • MRU (most recently used)
  • LRU
Answer

LRU — LRU (Least Recently Used) evicts the coldest entry by last access time.