TTL, JITTER & NEGATIVE CACHING

A thousand keys set at once expire at once

L112 MINCACHING
PREDICT

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?

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
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

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 IT NOT 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 IT NOT 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.

Mental model:

a TTL turns when you wrote into when you reload, so a burst of writes becomes a burst of reloads one TTL later. Add jitter so the reloads spread out. And a lookup that finds nothing is a result too: cache the absence, briefly, or every miss for a missing key goes to the DB.

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 CARDS Tap to flip. They join your review deck when you mark the lesson complete.

USED IN

Chapters that build on this lesson.

NEXT: CDN & CACHE INVALIDATION →