LOAD-BEARINGfree
High-level designTier 4 · unit 2 of 7 · 15 min read

Caching: four strategies, three failures

Caching is the highest-leverage optimisation in system design — Tier 0's latency table showed memory beating flash by a couple of hundred times and beating cross-region hops by thousands. It is also the source of the industry's most beloved bitter joke ("there are only two hard things in computer science: cache invalidation and naming things"). Both reputations are earned. Let's cover the four ways to run a cache, and the three ways caches take down the systems they were protecting.

The four strategies — who fills the cache, and when?

  • Cache-aside (lazy loading) — the app does everything. Check cache; on a miss, read the database, then put the result in the cache for next time. Simplest and by far the most common. Costs: every miss pays full database latency, and a cold cache (post-restart) means a painful stretch of misses.
  • Read-through — same behaviour, different owner: the cache itself fetches from the database on a miss. The load-and-store logic lives in one layer instead of being repeated through application code.
  • Write-through — every write goes to cache and database, synchronously, together. The cache is never stale — lovely — but every write now pays double latency.
  • Write-behind — write to the cache, acknowledge immediately, flush to the database asynchronously. Spectacular write speed, and a sharp edge: if the cache dies before flushing, those writes are gone. Acceptable for view counters. Career-ending for payments. Know which one you're building.

Eviction: the cache is full — who leaves?

LRU (least recently used — evict what's been untouched longest) is the default and usually right: recent past predicts near future. LFU (least frequently used) is better when a stable set of items is permanently hot and you don't want a burst of one-off requests flushing your VIPs out. And put a TTL on everything as a backstop — because someday, somewhere, an invalidation path will be forgotten, and a TTL turns that bug from "stale forever" into "stale for ten minutes."

The three failure modes — learn these cold

These have taken down more production systems than most categories of bug, and interviewers love them:

  • Thundering herd / stampede. One hot key expires — the homepage data, say. In the next 50 milliseconds, 10,000 requests all miss, and all 10,000 independently descend on the database to recompute the same value. The database, sized for a 95% cache hit rate, is suddenly serving everything, and it dies. Fixes: a per-key lock so exactly one request recomputes while the rest briefly wait or serve the stale copy; plus jittered TTLs — add randomness to expiry times so a thousand keys cached together don't all expire in the same second.
  • Cache penetration. Requests for keys that don't exist anywhere — not in cache, not in the database. Often an attacker cycling through made-up IDs. Every request misses, every miss hits the database, and the cache contributes nothing. Fixes: cache the negative ("this ID doesn't exist") with a short TTL, or keep a bloom filter of all real keys — remember it from Lesson 3.01: it answers "definitely not present" instantly, letting you bounce garbage before it costs anything.
  • Cache avalanche. The cache node itself dies or restarts empty. 100% of traffic — which the cache had been absorbing — lands on a database sized for 5% of it. The database collapses; now everything is down, and the empty cache can't even refill from a dead source. Fixes: replicate the cache (its availability now matters like a database's), coalesce duplicate in-flight requests, and put a circuit breaker in front of the database so it sheds load and survives rather than accepting all comers and dying (full treatment in Tier 9).

Invalidation: the famously hard part

When the underlying data changes, the cached copy is a lie. Options, honestly ranked: TTL — just let lies expire; bounded staleness, zero code paths to forget; the lazy answer and usually the right one. Explicit invalidation on write — precise, and you will eventually miss a write path (see: famous joke). Versioned keys — embed a version in the key (user:42:v8), bump the version on change; old entries are never deleted, just abandoned to expiry. Sidesteps deletion entirely and is quietly underrated.

The trap

Caching before measuring. A cache is only as good as its hit rate, and if you don't know yours, you don't know if the cache helps. A 40% hit rate means most requests pay the cache check and the database read — plus you own all the invalidation machinery. That can be genuinely worse than no cache and one good index. Measure the hit rate; below ~80%, interrogate the design.

The tradeoff sheet

Problem solved
Repeated identical reads landing on a datastore that is slower and dearer per lookup than memory, and the tail latency that arrives with it.
Rejected, and why
A read replica adds capacity but not speed — same engine, same milliseconds. A larger instance raises the ceiling without changing the cost per read. Both are simpler, and neither turns a 20 ms lookup into a 1 ms one.
The trade
Correctness. Every cached value is a claim about the past, so you are choosing a staleness window and taking on the invalidation that bounds it.
First to break at 10x
The hit rate, before the cache. The working set outgrows the memory you sized for it, hit rate falls, and the datastore takes miss traffic it was never provisioned to serve.
What it does to the bill
Memory is the priciest tier you rent per GB. A cache pays for itself when it removes database instances — not when it sits in front of one that stays exactly the same size.
Day-2 operations
Alert on hit rate and eviction rate, never on memory used. A cold tier after deploy is a thundering herd aimed at the database, so warm before cutover and never restart the whole tier at once.

The nuance

“Below about 80% hit rate, interrogate the design.”

True for a cache in front of a fast keyed lookup, where a miss costs a little and the machinery costs a lot. It is wrong for a cache in front of something genuinely expensive — a thirty-second aggregation, a paid API call, a model inference — where a 30% hit rate is excellent value. The threshold is about the ratio between miss cost and cache cost, not about the number 80.