13. Caching at Scale
1
The picture to keep in your head2
13.1 What is a cache stampede, and how do you stop one?3
13.2 What is cache penetration?4
13.3 What is a cache avalanche?5
13.4 What is a hot key, and why can't sharding fix it?6
13.5 How do you spread a cache across many machines?7
13.6 What is cache warming?8
13.7 How does HTTP caching work in browsers and CDNs?9
13.8 How do you know your cache is healthy?10
Memory card and where the analogy breaks11
Practice: five levels12
Interview questions13
Solutions14
Sources15
Code lab13. Caching at Scale
13.6 What is cache warming?
3 min read+10 XP
At the wedding: You don't open the doors to 500 guests with an empty fridge. The night before, you stock it with the dishes you know everyone will want.
In the system: Cache warming means filling the cache with the data you expect to be hot before real traffic needs it, so the first wave of users doesn't all miss.
When to warm:
- Before a known event: the Big Sale starts at 8 pm; at 7:45, load the 200 sale products, their prices and the home page.
- After a cache restart or a new node: a brand-new node is empty and would send every request for its slots to the database.
- After a deploy that changes key formats (Chapter 12, section 12.9's versioned keys).
How:
- From a list: the top N keys from yesterday's access stats, or the sale's product list.
- By replaying traffic: send a sample of recent real requests through the new cache before switching users to it.
- Gradually: shift traffic to a new cache node a little at a time, so it fills while the old path still serves most users.
Warm with jittered TTLs (Chapter 12), or everything you warmed will expire together: an avalanche on a timer. And don't warm everything: warming keys nobody reads just evicts the ones they do.
Memory code: "Stock the fridge before the guests arrive."