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
Memory card and where the analogy breaks
4 min read+10 XP
The memory card (tear this page out)
| # | Question | Memory code |
|---|---|---|
| 12.1 | What is a cache? | CFO: Copies, Faster, Often |
| 12.1 | Why do we need one? | Wait and Weight |
| 12.1 | What does it store? | No label, no lookup (key-value) |
| 12.1 | How does a request flow? | Fridge first, shop second, stock on the way back |
| 12.1 | Hit rate? | Count the hits; the misses set the speed |
| 12.1 | Is it the real data? | Lose the copy, keep the truth |
| 12.2 | Why is the database slow? | Really Patient Folks Shop: Receive, Plan, Find, Send |
| 12.2 | Why is a cache faster? | Close and Simple |
| 12.2 | Why not cache everything? | Little, Forgetful, Clueless |
| 12.2 | Why not a faster database? | Don't cook faster. Cook once. |
| 12.3 | Where does it live? | Pocket or Pantry |
| 12.4 | Read strategies? | Cache-aside: you go to the shop. Read-through: the fridge goes for you. |
| 12.5 | Write strategies? | Through: both now. Back: fridge now, shop later. Around: shop only, toss the carton. |
| 12.6 | What if data changes? | Date it and delete it |
| 12.6 | Jitter? | Don't let every carton go off on the same morning |
| 12.7 | What if it's full? | Last touched, first tossed |
| 12.8 | Safe invalidation? | Change the shop first, then toss the carton |
| 12.9 | When to cache? | Cache what can REST. Skip the three Us. |
| 12.9 | What does it cost? | Every cache gathers MOSS |
| 12.9 | Cache keys? | Put everything that changes the answer in the name, and nothing else |
The whole chapter in one line: Fridge first, date it, delete it, and never trust the fridge with the truth.
Where the analogy breaks
- Your fridge holds the actual milk; a cache holds a copy. Emptying the fridge loses milk. Emptying a cache loses nothing.
- A fridge serves one family. A cache serves millions of requests at once.
- Milk spoils with time. Data only goes stale when the source changes. That's why deleting on change matters more than the timer.
Quick check
Five questions on this chapter. Pick an answer, then check it.
quiz