Caching: recap and quiz
The module in five sentences
Section titled “The module in five sentences”- Cache-aside. A cache keeps copies of answers close by, the database stays the truth, and the database only sees the misses, so its load is
R × (1 − h). - TTL and invalidation. A TTL bounds how long a stale copy can be served and deleting on write removes most staleness, so use both, and remember a slow reader can still put an old value back.
- Cache stampede. When a popular entry expires, every request rebuilds it at once. There are two causes, a crowd on one key and many keys expiring together, and a different mitigation for each.
- Hot keys. Shards spread keys, not requests, so one dominant key saturates the shard that owns it, and only copies of the key, or less traffic for it, help.
- When the cache fails. The cache carries load the database was not sized for, so losing it multiplies the database’s load by
1 / (1 − h), and every fix trades something.
How each one breaks
Section titled “How each one breaks”| Idea | What breaks | What you see | What helps |
|---|---|---|---|
| Hit ratio | It falls | The database’s load rises sharply, before errors appear | Watch database utilisation, not only errors |
| Invalidation | A copy is not updated or deleted | Stale reads for up to the TTL | Delete on write, with a TTL as the safety net |
| Expiry | Many entries expire at once | A burst of misses and a queue at the database | Coalescing, jitter, early refresh |
| Sharding | One key dominates | One shard saturated, the others quiet | Copies of the key, or a local cache |
| The cache itself | It is unreachable or empty | The database overloads, then errors | Capacity, load shedding, a standby, and a plan for the cold restart |
A checklist for designing a cache
Section titled “A checklist for designing a cache”Use these when you design one, and when you review someone else’s. Each question points back to the lesson that answers it.
- What exactly is cached, and how big is it? Count the objects, the size of each and the skew of the requests, and size the cache with the calculator. The skew is the number you must measure.
- How stale can each kind of data be? Choose a TTL for what the data can tolerate, delete on write where it cannot be stale at all, and keep the TTL as the safety net. (TTL and invalidation)
- In which order do you write? Database first, then delete the cached copy. (Cache-aside)
- What happens when many entries expire together? Is there a moment when a deploy, a restart or a batch job fills the cache all at once? Add jitter, and coalesce or refresh early for the hot ones. (Cache stampede)
- Is any key likely to dominate? If one could, plan copies of it or a local cache. (Hot keys)
- What does the application do when the cache is unreachable? Choose fail-open or fail-closed on purpose, and check that the database can take the load if you chose fail-open. (When the cache fails)
- How big is the database’s margin? Compute
1 / (1 − h)for your hit ratio. If the database cannot carry that, the cache is holding up the design. - What happens when the cache comes back empty? Plan how it refills, because the database faces the full load until it does.
- Did you test the failure? Run it, and run the same failure again after each change, as the failure lab does.
Check your understanding
Section titled “Check your understanding”Seven questions cover the whole module. Every option is explained, whether it is right or wrong.
Module quiz: caching
This quiz needs JavaScript to check your answers.
Where this goes next
Section titled “Where this goes next”The caching module used a cache in front of a database. The same ideas come back in later modules: a CDN is a cache at the edge of the network, a replica is a copy that lags behind its source, and a rate limiter protects a system from the same overload that a failed cache causes. When you meet them, ask the questions of this module first: where is the copy, how does it go stale, what happens when many copies expire at once, and what happens when it is gone.
Your progress
Built by GhaziAlibi.
© ShardCraft. All rights reserved.