Lesson 11 of 16
12. Caching: The Basics

Memory card and where the analogy breaks

4 min read+10 XP

The memory card (tear this page out)

#QuestionMemory code
12.1What is a cache?CFO: Copies, Faster, Often
12.1Why do we need one?Wait and Weight
12.1What does it store?No label, no lookup (key-value)
12.1How does a request flow?Fridge first, shop second, stock on the way back
12.1Hit rate?Count the hits; the misses set the speed
12.1Is it the real data?Lose the copy, keep the truth
12.2Why is the database slow?Really Patient Folks Shop: Receive, Plan, Find, Send
12.2Why is a cache faster?Close and Simple
12.2Why not cache everything?Little, Forgetful, Clueless
12.2Why not a faster database?Don't cook faster. Cook once.
12.3Where does it live?Pocket or Pantry
12.4Read strategies?Cache-aside: you go to the shop. Read-through: the fridge goes for you.
12.5Write strategies?Through: both now. Back: fridge now, shop later. Around: shop only, toss the carton.
12.6What if data changes?Date it and delete it
12.6Jitter?Don't let every carton go off on the same morning
12.7What if it's full?Last touched, first tossed
12.8Safe invalidation?Change the shop first, then toss the carton
12.9When to cache?Cache what can REST. Skip the three Us.
12.9What does it cost?Every cache gathers MOSS
12.9Cache 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