CACHE STAMPEDE

When a popular key expires, everyone races to recompute

L215 MINCACHING
PREDICT

user:42 expires at 1000 ms. Ten readers arrive evenly between 700 and 1300 ms. A DB load takes 150 ms, plus 50 ms for each load already running. No protection. How many DB loads does this one expiry cost?

A cached key expires at one instant. Every request that arrives before someone reloads it misses. In plain cache-aside each miss runs its own DB load. Those loads compete, so each one is slower, so the window stays open longer and more requests fall into it.

Three fixes keep the same expiry. Singleflight lets the first miss load and makes the rest wait for that load. Stale-while-revalidate keeps serving the old value while one background refresh runs. Early refresh lets a reader refresh before expiry, with a probability that rises as expiry approaches.

▶ SIMULATION
REQUESTS
arrived 0 / 10 · waiting 0
hit 0 · served stale 0
longest wait: 0 ms
CACHE user:42
generation: v1
expires at: 1000 ms
DATABASE
loads in flight 0
loads for the v1 expiry 0
total loads: 0
clockt = 700 ms · strategy: none
last served—
last event—
✓ DB loads per key per expiry ≤ 1✓ no request waits longer than one load (150 ms)
SCHEDULE
01[APP]every miss loads from the DB
02[CLOCK]advance to t = 700 ms
03[CLOCK]advance to t = 750 ms
04[CLOCK]advance to t = 800 ms
05[CLOCK]advance to t = 850 ms
06[CLOCK]advance to t = 900 ms
07[CLOCK]advance to t = 950 ms
08[CLOCK]advance to t = 1000 ms (user:42 expires)
09[CLOCK]advance to t = 1050 ms
10[CLOCK]advance to t = 1100 ms
11[CLOCK]advance to t = 1150 ms
12[CLOCK]advance to t = 1200 ms
13[CLOCK]advance to t = 1250 ms
14[CLOCK]advance to t = 1300 ms
15[CLOCK]advance to t = 1350 ms
16[CLOCK]advance to t = 1400 ms
17[APP]report
Start of no protection scenario. 17 steps.
function get(key) {
  const v = cache.get(key);
  if (v !== undefined) return v;
  const fresh = await db.load(key);   // every concurrent miss runs this
  cache.set(key, fresh, TTL);
  return fresh;
}

Watch the DATABASE box and the invariant strip. NO PROTECTION turns both invariants red after expiry. SINGLEFLIGHT and STALE-WHILE-REVALIDATE face the same traffic and the same expiry and keep loads for the v1 expiry at 1 at every REQUESTS value; stale-while-revalidate also keeps the longest wait at 0 ms. EARLY REFRESH (PROBABILISTIC) holds at the default but not at every count: move the slider and watch it let two loads through.

TUNE IT NOT YET

No protection. The DB absorbs 3 loads for one expiry without falling behind. Raise REQUESTS until one expiry costs more than that, and note the count where it first happens.

GOAL: one expiry costs more than 3 DB loads

FIX IT NOT YET

Add singleflight. Concurrent misses for the same key must share one loader call, and the next expiry must be able to load again. loader(key) returns a Task, not a Promise: call .then(fn) on it to get a new Task. Return the value on a hit and a Task on a miss. No async/await.

Mental model:

an expiry is a synchronization point: every concurrent reader learns about it at the same instant. Either make one of them do the work and the rest wait (singleflight), make none of them wait and accept stale data (stale-while-revalidate), or move the work before the instant (early refresh). Singleflight and stale-while-revalidate (with one refresher per key) bound the load; early refresh alone does not.

IN PRODUCTION

Hot keys in Redis or memcached: homepage queries, config blobs, a celebrity's profile. Go's singleflight package, stale-while-revalidate in HTTP Cache-Control, memcached leases, and the XFetch paper (“Optimal Probabilistic Cache Stampede Prevention”, VLDB 2015). Symptom: periodic DB spikes lined up with a TTL.

IN INTERVIEWS

“A popular key expires and the DB falls over. What happened and how do you fix it?” Explain the miss window and the feedback loop, then compare singleflight (bounded load, waiters), stale-while-revalidate (no waiting, stale reads) and early refresh (probabilistic). In a fleet, say that singleflight is per process, so N servers can still send N loads unless you add a distributed lock or lease.

RECALL CARDS Tap to flip. They join your review deck when you mark the lesson complete.

USED IN

Chapters that build on this lesson.

NEXT: WRITE-THROUGH vs WRITE-BACK →