Cache Invalidation Strategies Lab (Interactive)
Mutate the database from app code, ETL, and raw psql, then count the stale reads each strategy leaks before converging. Compare pure TTL, application eviction, CDC (Debezium + Kafka), and versioned keys against mutations that bypass application code.
Cache Invalidation Strategy Lab
Mutate the DB through app code, ETL, or raw psql — and count how many stale reads each strategy leaks.
Strategy
Mutate user:42 via…
› DB v1 and Redis v1 agree. Pick a strategy, then mutate through different writers.
How It Works Under the Hood
Phil Karlton’s famous quip is mechanical truth: when a row mutates, its copies in Redis, Memcached, and CDN edges become stale. Application-level DEL works only for writes that pass through application code — an emergency psql script or nightly ETL silently leaves the cache serving outdated prices. Pure TTL is self-healing but tolerates staleness up to its window. CDC tails the WAL (PostgreSQL/MySQL binlog) through Debezium and Kafka so every commit, from any writer, reliably triggers eviction within 10–50ms. Versioned keys (user:42:v pointer INCR) invalidate atomically with zero races.
Core Architectural Principles
- TTL is the universal safety net: every key expires eventually, bounding staleness even when active invalidation fails.
- CDC from the transaction log catches writes from rogue scripts and batch jobs that application eviction cannot see.
- Versioned cache keys swap an INCR pointer instead of deleting, eliminating write races without distributed locks.
For enterprise-scale designs, propose layered invalidation: short TTLs as the baseline, application DEL for the hot path, and CDC (Debezium + Kafka + idempotent workers) as the guarantee that no mutation from any writer is missed — Airbnb does exactly this for listing caches. Mention versioned keys for composite objects to avoid races, and always restate that TTLs remain mandatory even with active eviction.
Reliability rises from TTL-only to CDC-everything, paid for in staleness windows versus Kafka and Debezium infrastructure complexity.