Free course · 20 lessons · Advanced

Database Caching at Scale

Caching for high-traffic systems without stale or leaked data: cache-aside done right, the stale-set race, versioned keys, TTLs and jitter, caching with transactions and replicas, stampedes, penetration, avalanches, hot keys, event-driven invalidation, write-through and write-behind, two-tier caches, HTTP/CDN caching, Redis operations and failover — with real case studies, runnable simulations and code for every major stack.

Start the course

1. Cache-Aside Done Right

The read and write paths of the most common caching pattern — and the races that make caches serve stale data.

  1. Cache-Aside in Practice — The read path, the write path and the details (keys, TTLs, serialization, failures) that decide whether a cache helps or hurts.
  2. The Stale-Set Race — Why cache-aside can still cache an old value forever after a delete — and how leases, versions and short TTLs fix it.
  3. Versioned Keys and Generation Counters — Invalidate a whole family of cache entries — category pages, search results, per-user lists — with one increment.
  4. Choosing TTLs and Adding Jitter — Staleness budgets per data type, and why identical TTLs make thousands of keys expire at the same second.
  5. Caching with Transactions and Replicas — Invalidating before commit, filling from a lagging replica — two ways to cache old data on purpose-built systems.

2. Cache Failure Modes

Stampedes, penetration, avalanches, hot keys and big keys — what takes caches (and the databases behind them) down.

  1. Cache Stampedes (Dogpiles) — When a hot key expires, every request misses at once: request coalescing, locks, early refresh and stale-while-revalidate.
  2. Cache Penetration: Requests for Data That Doesn't Exist — Bots and bugs asking for missing keys bypass the cache every time — negative caching and Bloom filters stop them.
  3. Cache Avalanches and Cold Starts — When the whole cache disappears at once — restarts, failovers, flushes — the database must not take the full load.
  4. Hot Keys and Big Keys — One key receiving a huge share of traffic, or one value that is megabytes large — both overload a single cache node.

3. Cache Consistency Strategies

Invalidation pipelines, write-through and write-behind, two-tier caches, HTTP caching, and never leaking one user's data to another.

  1. Event-Driven Invalidation with Outbox and CDC — Move invalidation out of request handlers so every writer — including scripts and other services — keeps caches correct.
  2. Write-Through and Write-Behind — Updating the cache on every write, or the database later — and what you can lose.
  3. Two-Tier Caching: In-Process L1 + Redis L2 — Microsecond reads from local memory without serving stale data for long: invalidation fan-out with pub/sub.
  4. HTTP and CDN Caching Without Surprises — Cache-Control, ETags, stale-while-revalidate and purges — and the headers that keep private data out of shared caches.
  5. Caching Personalized and Authorized Data Safely — Cache keys that forget the user, tenant, locale or permissions turn a performance feature into a data leak.

4. Operating Caches in Production

Redis data structures and atomic scripts, memory and eviction, failover consistency, and observing caches.

  1. Redis Data Structures and Atomic Operations — Use INCR, hashes, sorted sets and Lua scripts instead of racy GET-then-SET sequences.
  2. Memory, Eviction Policies and Serialization — What happens when the cache fills up — and why the wrong maxmemory-policy turns a full cache into write errors.
  3. Cache Failover and Lost Invalidations — Asynchronous Redis replication means a failover can bring back values you already deleted.
  4. Observing and Testing Caches — Hit ratio per keyspace, latency, evictions, staleness — and tests that catch races before production does.

5. Capstone Case Studies

Design caching for a flash-sale product page, and investigate stale prices after a cache failover.

  1. Capstone: Caching a Flash-Sale Product Page — 500,000 shoppers open the same product page at 10:00:00. Design the caching so the database survives and prices stay right.
  2. Capstone: Postmortem — Stale Prices After a Cache Failover — Reconstruct an incident from timelines and code, find the root causes and write the remediation plan.