Skip to content
Ameen Abozaid
MID-040

Cache consistency

  • 5 Lessons
  • 2 h practice (estimate)
On this page 8 topics

Overview

Topics

  • Stampedes
  • stale data
  • invalidation races
  • hot keys

Learn first

2 h of practice (estimate)

Learning outcomes

  1. Explain and prevent cache stampedes (locking, request coalescing, early refresh).
  2. Identify invalidation races that leave stale data and fix them (delete-after-write, versioning).
  3. Detect and mitigate hot keys.
  4. State an explicit staleness tolerance for each cached item.

Lessons

  1. 01Stampedes
  2. 02Invalidation races
  3. 03Hot keys
  4. 04Staleness budgets
  5. 05Demo: a stampede after cache expiry, then the fix

Lab and exercises

Lab

MID-040-LAB1 · Flash sale

Outcomes: 1–4 · Time: ~2 h Simulate a flash sale: 2000 requests per second on one product whose cache entry expires during the test. Measure database queries per second. Add stampede protection (lock or coalescing plus TTL jitter) and a local short-lived cache for the hot key. Write the staleness budget for the product. Acceptance: database load stays bounded (state the measured maximum queries per second before and after).

Exercises (assessment: race-condition analysis)

IDOutcomeTask
MID-040-EX0111000 readers hit an expired key; each query takes 50 ms. Estimate database load without and with a single-flight lock.
MID-040-EX022Draw the interleaving that leaves a stale price after "database update then cache delete", and fix it with versioning.
MID-040-EX032Explain why "delete cache, then update database" is worse than the reverse.
MID-040-EX043Propose a mitigation for a hot product key across 20 instances.
MID-040-EX054Write a staleness budget for price, stock display, category tree, and product description.
Show solutions
  • EX01 Without: up to 1000 identical queries in a 50 ms window. With a lock: 1 query; others wait about 50 ms or get the stale value.
  • EX02 Reader misses and reads old row; writer commits and deletes; reader writes old value. With versions: the cache write is "put if version > cached version", or the stored version is compared with the row version on read; the old value is rejected or short-lived.
  • EX03 A reader between the delete and the update reloads the old row and caches it; it stays stale until TTL.
  • EX04 Local Caffeine L1 with 1-2 s TTL, plus coalescing and jitter; optionally key replicas.
  • EX05 Example: price 1 s (event + short TTL), stock display 30 s (checkout verifies), category tree 1 h, description 10 min.

Outcome 3 is also covered by the lab (hot key).

Quiz

Outcomes: O1 stampedes · O2 invalidation races · O3 hot keys · O4 staleness budgets.

A self-check for you: nothing is graded, saved or sent anywhere. Choose an answer, then check it. You can try again as often as you like.

  1. 1A cache stampede happens when:O1
  2. 2Which does NOT reduce stampedes?O1
  3. 3A lock around cache rebuilds should also have:O1
  4. 4A stale value stays in the cache after a delete because:O2
  5. 5Which order is safer?O2
  6. 6Versioning fixes races by:O2
  7. 7A hot key is:O3
  8. 8A good mitigation for a hot key:O3
  9. 9A staleness budget is:O4
  10. 10For data with zero staleness tolerance, such as a payment total:O4

Practical part

Race-condition analysis (MID-040-EX02, EX03) plus lab MID-040-LAB1. Pass: quiz at least 8/10, correct race diagram and fix, and a lab with bounded database load measured.

Common mistakes

Identical TTLs on everything; ignoring races because "it's rare".

Assessment

Race-condition analysis.

Career relevance

Large-scale performance (SRA-007).

Type to search levels, phases, modules and lessons.