Skip to content
Ameen Abozaid
Planned lessonMID-040-L05

Demo: a stampede after cache expiry, then the fix

  • Lesson 5 of 5
  • Mid-level
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

  1. Cache product:7 with a 10 s TTL; run a load test of 200 concurrent readers.
  2. Count database queries at expiry: a spike to about 200 identical queries.
  3. Fix 1: a per-key lock (one loader; the rest wait or serve the stale value) or Caffeine LoadingCache; show 1 query.
  4. Fix 2: add random jitter of ±20% to TTLs and early refresh.
  5. 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.
  6. 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.

Lab and exercises Quiz

Marking a lesson complete is optional and you can undo it any time. Your progress stays in this browser.

Type to search levels, phases, modules and lessons.