Caching
Cache topology from client to database, the five patterns, invalidation strategies, stampedes and their cures, negative caching and cache penetration, Redis as the workhorse, consistency between cache and database and the dual-write problem, and materialised views as precomputation.
Topology and patterns
Every layer between the user and the database can cache: the client (HTTP caching, TanStack Query), the CDN, the application process, a distributed cache, and the database's own buffer pool. Each layer is a replica allowed to be stale (Distributed Systems part 11 covers the mechanics). This part is about choosing which layers a given piece of data may live in, and for how long.
Invalidation, stampedes and Redis
| invalidation strategy | staleness | cost |
|---|---|---|
| TTL only | up to the TTL | simplest; pick TTL per data type |
| delete on write | tiny window (the read-race) | every write path must know its keys |
versioned keys (user:7:v42) | none for readers of the new version | old versions expire unused |
| CDC-driven invalidation | replication lag | a consumer of the change stream deletes keys; robust, more moving parts |
- Request coalescing: one load per key per process (single-flight).
- A short lock in Redis (
SET key:lock NX PX 3000) so one process across the fleet rebuilds. - Probabilistic early expiry (XFetch): each reader refreshes slightly before expiry with a probability that rises as expiry nears.
- Stale-while-revalidate: serve the old value while one refresh runs.
Redis is the workhorse: in-memory data structures (strings, hashes, sorted sets, streams), optional persistence (RDB snapshots, AOF), eviction policies (allkeys-lru, volatile-ttl, allkeys-lfu), and Redis Cluster with 16,384 hash slots. Course 7 covers it in depth. Materialised views are caching inside the database: precomputed results refreshed on a schedule (REFRESH MATERIALIZED VIEW CONCURRENTLY in Postgres needs a unique index).
Consistency between cache and database
The question to answer per piece of data is what happens if a user sees a value that is N seconds old. A product description: nothing. A balance shown on a dashboard: confusion, so show its age. A balance used to approve a transfer: money lost, so never cache it. Write the answer down next to the cache key definition.