Caching
The fastest query is the one you never make — but stale data and stampedes await.
By the end of this lesson, choose caching strategies and defend against invalidation and stampede failures.
A cache stores the result of expensive work close to where it's needed so you don't redo it. Caching is the single biggest lever for latency and scale — and the source of some of the nastiest bugs, because a cache is by definition a second copy of the truth that can drift.
Cache-aside read path
- In cache-aside: the app reads the cache; on a hit it returns; on a miss it reads the database, returns the value, and populates the cache.
flowchart LR app[App] -->|1 read| cache[(Cache)] cache -->|hit| app cache -->|miss| db[(Database)] db -->|2 value| app app -->|3 populate| cache
Strategies: cache-aside (app manages the cache, most common), read-through/write-through (cache sits inline and stays synced with the store), write-behind (buffer writes, flush later — fast but risks loss). Eviction (LRU/LFU) and TTLs bound size and staleness. The hard part is always invalidation: deciding when a cached value is no longer true.
This is where the abstraction starts leaking.
'There are only two hard things in computer science: cache invalidation and naming things.' A cache silently returns wrong answers when your invalidation logic misses an update. The failure isn't a crash — it's confidently serving stale truth.
Cache stampede
- Trigger
- A hot key expires and many requests miss simultaneously.
- Symptom
- A thundering herd of identical recomputations slams the origin.
- Blast radius
- The backing store and everything depending on it.
- Mitigation
- Request coalescing/single-flight, early/probabilistic refresh, jittered TTLs, locks.
Deep dive: Cache Stampede
Caching trades consistency for latency and load: you accept some staleness in exchange for large speedups and reduced origin pressure. Shorter TTLs reduce staleness but raise miss rate and stampede risk.
Why is invalidation the hard part of caching?
Because the cache is a second copy of the truth; if you fail to invalidate on an update, it silently serves stale data with no error.