A warm-up job writes 100 keys at t = 0, each with TTL 60 s. Every key is read every second and reloads exactly once when it expires. The DB can take 15 loads a second. What happens?
The average is fine. The peak is not. Same write time plus same TTL means the same expiry instant, so the whole minute's reloads land in one second. Averages hide synchronized load.
A TTL is a timer, and timers that start together end together. Keys written in one burst (a warm-up job, a deploy, a cold restart) all expire in the same second. Each key costs one reload, but they all land at once. Jitter fixes that: give each key a slightly different TTL so the reloads spread out.
A missing row has the opposite problem. Cache-aside only caches values, so a key with no row misses every time. Negative caching stores “not found” too, with a short TTL.
▶ SIMULATION
0 / 12
TRAFFIC
100 keys, each read every second
user:999 (missing): 50 req/s
jitter 0% · TTL 60–60 s
CACHE
expiring in next 10 s: 0
expiry spread: —
user:999: not cached
DATABASE
loads in the last second 0 / 15
peak 0/s
key reloads 0 · user:999 lookups 0
clockt = 0 s
loads/s, last 10 s—
✓ DB loads in every second ≤ 15
SCHEDULE
01[APP]config: TTL 60 s minus JITTER %, negative cache on
02[JOB]t = 0 s: warm-up job writes every key with its TTL
03[CLOCK]run traffic to t = 10 s
04[CLOCK]run traffic to t = 20 s
05[CLOCK]run traffic to t = 30 s
06[CLOCK]run traffic to t = 40 s
07[CLOCK]run traffic to t = 50 s
08[CLOCK]run traffic to t = 60 s (60 s TTL ends)
09[CLOCK]run traffic to t = 70 s
10[CLOCK]run traffic to t = 80 s
11[CLOCK]run traffic to t = 90 s
12[CLOCK]report
Start of jitter = slider scenario. 12 steps.
cache.set(key, value, 60_000); // every key written now expires at now + 60 s
const v = cache.get(key) ?? db.get(key); // a missing row returns null...
if (v !== null) cache.set(key, v, 60_000); // ...is never cached, so every request asks the DB
const ttl = (base) => Math.round(base * (1 - 0.3 * Math.random()));
cache.set(key, value, ttl(60_000)); // expiries spread over 18 s
const hit = cache.get(key);
if (hit !== undefined) return hit; // includes a cached null
const v = db.get(key);
cache.set(key, v, v === null ? 5_000 : ttl(60_000)); // short TTL for "not found"
return v;
Watch the DATABASE peak and the invariant strip. JITTER = SLIDER at 0% puts 101 loads into second 60. HOT MISSING KEY sends 50 lookups a second for user:999 from the first second. FIX: JITTER + NEGATIVE CACHE keeps every second under the cap of 15.
TUNE ITNOT YET
Set KEYS to 200. Then raise JITTER % until no second of the run asks the DB for more than 15 loads.
GOAL: 200 keys and peak DB loads ≤ 15 per second
FIX ITNOT YET
Two fixes. ttlFor(base, rng) must spread TTLs so keys set together do not expire together. getOrNegative must cache a missing row (the DB returns null) for a short time, short enough that a row created later shows up within 30 s.
IN PRODUCTION
Any cache that is filled in bulk: warm-up jobs, deploys that restart Redis, CDN or DNS records published together. Negative caching shows up in DNS (NXDOMAIN answers are cached, RFC 2308), in CDNs caching 404s, and in application caches storing a NOT_FOUND sentinel. Symptom: DB spikes at regular intervals, or steady DB load from IDs that do not exist.
IN INTERVIEWS
“Our DB spikes every hour on the hour. Why?” Spot the synchronized expiry, propose jittered TTLs (and staggering the warm-up), then add: “also check misses for nonexistent keys,” and explain negative caching with a short TTL or a delete on insert.
RECALL CARDSTap to flip. They join your review deck when you mark the lesson complete.