12. Caching: The Basics
1
The picture to keep in your head2
12.1 What is a cache, and what are a hit, a miss and the hit rate?3
12.2 Why is a cache faster than the database?4
12.3 Where does a cache physically live?5
12.4 On a miss, who fetches the data?6
12.5 When the data changes, what gets written where?7
12.6 What happens when the real data changes, and why add jitter?8
12.7 What happens when the cache is full?9
12.8 How do you invalidate safely, and what's the race everyone hits?10
12.9 When should you cache, what should you never cache, and how do you name keys?11
Memory card and where the analogy breaks12
Practice: five levels13
Interview questions14
Solutions15
Sources16
Code lab12. Caching: The Basics
The picture to keep in your head
3 min read+10 XP
Every question below is answered twice: first in the kitchen, so you can picture it, then in the system, so you know exactly how it works. Each answer ends with a memory code to lock it in.
The picture to keep in your head
You keep milk in your fridge so you don't walk to the supermarket every time you want tea. The fridge is small, fast and right next to you. The supermarket has everything, but every trip is slow.
Our running example: an online grocery app. A user opens the product page for milk. The app needs the milk's name, price and photo.
| Kitchen | System |
|---|---|
| Supermarket | Database (the original, the "source of truth") |
| Fridge | Cache |
| You, making tea | The app, handling a user's request |
| A label on a box in the fridge | A key |
| Use-by date | TTL (time to live) |
Figure: the fridge and the supermarket, mapped to the cache and the database.