Caching Basics
A cache is a copy of the truth that is allowed to be wrong for a while. Everything hard about caching comes from deciding how wrong, for how long, and who fixes it.
FundamentalsPerformance~10 min · 6 sections
Prerequisites: A database read path and the idea that memory is faster than disk.
Cover these firstDatabases 101
After this: Choose a caching pattern and name its staleness and stampede behavior before you are asked.
Suggested first pass: Read sections 1–5, answer each section in your own words, then use the remaining failure modes and exercises as the advanced pass.
Before choosing a strategy, notice that a request already passes through several caches.
- Browser cache. The client does not ask again. Free and instant, and you cannot clear it. Once a browser has cached something for a year, it is out of your control.
- CDN. Copies held near the user. Excellent for images, scripts, video, and any page identical for everyone.
- Application cache. A shared in-memory store such as Redis or Memcached between your servers and the database. This is what people mean by "add a cache".
- Local in-process cache. A map inside one server. Fastest and most dangerous, because each server has its own copy and they drift.
- Database cache. The database already keeps hot pages in memory. Some "we need Redis" conversations end when someone gives the database more RAM.
Next deep dive
Queues & Async Messaging →
~12 min