A Small Book on Caching
Everything I know about caching, distilled into 90 pages. Patterns that work, traps that don’t, and 24 worked examples in TypeScript. The blog post that started this site, expanded into something worth paying for.
Table of contents
- Chapter 1: The Map with a TTL — Why every cache starts here, and why most should stay here longer than you think.
- Chapter 2: Invalidation is the hard part — Write-through, write-behind, and the event-driven approach that actually works at scale.
- Chapter 3: Cache stampedes — What happens when a hundred requests hit an empty cache at once, and three ways to prevent it.
- Chapter 4: Layered caches — L1/L2 patterns for apps that need both speed and consistency. When to use Redis vs. an in-process Map.
- Chapter 5: Cache keys are a design decision — Naming conventions, versioning strategies, and the one mistake that will corrupt your data.
- Chapter 6: Measuring cache effectiveness — Hit ratios, latency percentiles, and how to know when your cache is hurting more than helping.
- Chapter 7: Caching at the edge — CDN caching, stale-while-revalidate, and the surprisingly tricky world of Cache-Control headers.
- Chapter 8: When not to cache — The cases where adding a cache makes everything worse. Recognizing them before you ship.
Sample excerpt: Chapter 3
Imagine your cache key expires at 12:00:00. At 12:00:01, 200 requests arrive for the same resource. Each one sees a cache miss. Each one hits the database. The database, which was happily serving 10 queries per second, now gets 200 in one second. It slows down. The requests take longer. More requests pile up behind them. This is a cache stampede, and it has taken down more production systems than most engineers realize.
The fix is deceptively simple: only let one request through. Everyone else waits. The first request populates the cache, and the rest read from it. This is called “request coalescing” or “single-flight,” and implementing it correctly is the subject of this chapter.
What readers are saying
“This is the caching book I wish I had five years ago. Concise, practical, and full of code I actually copied into production.”
“Chapter 5 on cache keys alone saved me from a bug that would have taken days to track down. Worth every penny.”
“I’ve read the big distributed systems books. This isn’t trying to be one of those. It’s tighter, more focused, and immediately useful.”