System design · primitive

Caching — strategies, invalidation, hot-key pain.

TL;DR

Caches collapse latency and protect origins. They also cause cache stampedes, stale reads, and hot-key outages. In an interview, name the strategy (read-through / write-through / write-back), the invalidation mechanism (TTL / event-driven), and the failure mode (stampede, hot key) out loud.

Strategies

Read-through: cache fills on miss. Write-through: writes go to cache and origin synchronously. Write-back: writes go to cache first, flushed to origin async — fastest, but risks data loss on crash.

Invalidation

TTL is simplest but produces staleness. Event-driven (cache-bust on write) is precise but requires a write pipeline. Versioned keys (key:v7) avoid invalidation entirely.

Failure modes

Cache stampede: N clients race to fill a missing key on expiry. Mitigate with probabilistic early refresh or a singleflight lock. Hot key: one key gets 10× traffic. Mitigate with request coalescing or key sharding.

Sizing

P95 hit rate is the number to target. A cache that hits 70% is often the right tradeoff; 95% requires huge memory or unfair LRU eviction. Narrate the target hit rate explicitly.

Frequently asked questions

What is a cache stampede?
When many requests hit a just-expired key simultaneously and all race to repopulate it. The origin is hammered. Mitigate with singleflight or probabilistic early refresh.
Read-through vs write-through vs write-back?
Read-through fills on miss. Write-through writes sync to both cache and origin. Write-back writes to cache first and flushes async — fastest but risks loss.
How do I invalidate a cache?
Three options: TTL (simple, stale), event-driven (precise, requires write pipeline), versioned keys (simplest correctness). Mention the tradeoff out loud in interviews.

Practice this live.

Book a 45-minute system design mock with a transcript. Included in the $19/mo subscription.