Skip to content

Caching: recap and quiz

  1. 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).
  2. 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.
  3. 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.
  4. 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.
  5. 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.
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

Use these when you design one, and when you review someone else’s. Each question points back to the lesson that answers it.

  1. 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.
  2. 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)
  3. In which order do you write? Database first, then delete the cached copy. (Cache-aside)
  4. 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)
  5. Is any key likely to dominate? If one could, plan copies of it or a local cache. (Hot keys)
  6. 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)
  7. 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.
  8. What happens when the cache comes back empty? Plan how it refills, because the database faces the full load until it does.
  9. Did you test the failure? Run it, and run the same failure again after each change, as the failure lab does.

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.

Question 1 of 7

A service sends 2,000 requests per second to a cache with a hit ratio of 95%. Then the hit ratio falls to 90%. How many reads per second does the database serve after the fall?

Enter a number and choose its unit.

reads per second
Question 2 of 7

In cache-aside, a write has to change the database and also get rid of the cached copy of that row.

In which order should the application do the two, and why?

Choose one answer.

Question 3 of 7

Which of these statements about a TTL are true?

Choose every answer that applies.

Question 4 of 7

A deploy fills the cache with ten thousand entries at the same moment, each with a lifetime of 10 minutes. Exactly 10 minutes later the database overloads.

Which change addresses the cause?

Choose one answer.

Question 5 of 7

A cache has four shards. One key takes 40% of all requests, and the shard that owns it is saturated. You add four more shards.

What happens to the traffic of that key?

Choose one answer.

Question 6 of 7

A cache has a hit ratio of 98%. It fails, and the application fails open: every read goes to the database. By what factor does the database's read load increase?

Enter a number and choose its unit.

times
Question 7 of 7

A service's database was sized for the 5% of reads that miss the cache. The cache fails, and the application is set to fail closed.

Which statement is true?

Choose one answer.

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.