Demo: a stampede after cache expiry, then the fix
On This Lesson 3 topics
This lesson is planned. The outline below lists the topics it will cover. The full lesson has not been published yet.
In this lesson
reproduce a stampede and fix it. Prepares MID-040-LAB1.
Draft outline details
Step by step
- Cache
product:7with a 10 s TTL; run a load test of 200 concurrent readers. - Count database queries at expiry: a spike to about 200 identical queries.
- Fix 1: a per-key lock (one loader; the rest wait or serve the stale value) or Caffeine
LoadingCache; show 1 query. - Fix 2: add random jitter of ±20% to TTLs and early refresh.
- Planned mistake: the lock has no timeout and the loader fails; every request waits forever. Fix: lock timeout, and on timeout serve the stale value or fail fast.
- Show the database query count as a flat line during expiry.
Check your understanding
Why must the lock have a timeout?
Show answer
A failed loader would block all waiters.
Practice
Apply this lesson with the module's lab, exercises and quiz.
Marking a lesson complete is optional and you can undo it any time. Your progress stays in this browser.