CAPSTONE: THE LATENCY CLIFF

Everything in this path, in one request that got 10x slower overnight

L320 MINSYSTEMS & LOW-LEVEL
PREDICT

p99 went from 6 ms to 58 ms overnight. Page faults (each a 2 ms disk read) add about 14 ms to the average request. GC pauses 36 ms every 0.8 s. The shared counter costs 100 ns per increment, 10 increments per request. Which do you fix first?

Latency cliffs come from thresholds, not gradual load. Three are in play here. When the heap plus the data working set outgrow RAM, a fraction of page touches become major faults at 2 ms each. When the live heap nears its maximum, GC runs more often and each pause gets longer, and once pauses touch more than 1% of requests they are the p99. A shared counter adds coherence traffic, but only nanoseconds of it.

Diagnose with numbers. Turn each stage into milliseconds added to the p99, then fix the biggest one first. Scoring faults by their average cost per request, as the FIX IT does, is a first approximation: it understates their tail. And fixes interact. A bigger heap takes RAM from the page cache, so on the 16 GB node, raising the heap to 8 GB alone pushes the p99 from 58 to 67 ms. The GC fix needs the RAM fix with it.

▶ SIMULATION
SERVICE (1000 req/s)
requests 0 / 10000 · t = 0.0 s
p50 — · p99 —
—
MEMORY (16 GB RAM)
heap 4 + working set 11 = 15 GB
page miss rate 0.0% · faults/req —
GC (live 2 / 4 GB)
pause 20 ms every 4.0 s
collections 0 · stalled requests 0.00%
counter: per-core shards, 1 ns per increment
page faults, mean per request— ms
GC stall, mean per request— ms (20 ms on 0.00% of requests)
counter, per request0.0000 ms
✓ p99 ≤ 25 ms
SCHEDULE
01[OPS]yesterday: 16 GB RAM, 4 GB heap (2 GB live), 11 GB working set
02[LOAD]requests 1–2000 (2 s)
03[LOAD]requests 2001–4000 (2 s)
04[LOAD]requests 4001–6000 (2 s)
05[LOAD]requests 6001–8000 (2 s)
06[LOAD]requests 8001–10000 (2 s)
07[OPS]diagnose: attribute the p99 stage by stage
Start of yesterday scenario. 7 steps.
Diagnose a latency cliff stage by stage, with numbers:

1. Memory   heap_max + working_set vs RAM.  If it does not fit, count major faults
             (vmstat si/so, /proc/vmstat pgmajfault) × cost per fault.
2. GC       pause length and how often.  pause / interval ≥ 1% means the pause
             IS your p99 (GC logs, jstat).
3. Locks /  contended lines and mutexes: ns per op × ops per request
   counters (perf c2c, lock profiler).  Usually microseconds.

Rank by milliseconds added to the p99. Fix the biggest first.
Re-measure under the same load after each fix.

Watch the three boxes and the invariant strip. YESTERDAY fits in RAM and stalls 0.5% of requests on GC: p99 6 ms. THE CLIFF adds 3 GB of data and 1.6 GB of live heap. The page miss rate goes from 0 to 14%, GC stalls about 6% of requests, and p99 goes to 58 ms. FIX RAM ONLY and FIX HEAP ONLY apply one fix each and re-measure: the first leaves the GC pauses in the p99, the second makes the cliff worse. FIX runs the same data, heap and traffic on a 32 GB node with an 8 GB heap and sharded counters. p99 is back to 6 ms.

BREAK IT NOT YET

Yesterday holds a 6 ms p99. Inject one overnight change somewhere in the run and make the p99 break the 25 ms SLO. Try all three: one of them is real and does not matter.

GOAL: the run finishes with p99 > 25 ms

FIX IT NOT YET

Write diagnose(m). It gets faultsPerReq, msPerFault, gcPauseMs, gcIntervalMs, counterOpsPerReq, nsPerCounterOp and returns the stages to fix, biggest p99 impact first, as names from 'page-faults', 'gc', 'counter'. Impact in ms: page faults = faults × ms per fault (the average, a first approximation that understates their tail); GC = the pause, but only if pause ÷ interval ≥ 1% (otherwise fewer than 1% of requests see it and the p99 does not); counter = ops × ns ÷ 1,000,000. Leave out any stage under 1 ms.

Mental model:

performance fails at thresholds: the working set fits or it does not, GC pauses reach more than 1% of requests or they do not. When latency falls off a cliff, find the threshold you crossed. Then rank by milliseconds at the p99, not by suspicion or by how recently something changed. Re-measure under the same load after every fix.

IN PRODUCTION

Databases whose index outgrows the buffer pool, JVM and Go services whose caches grow until the collector runs constantly, containers that start swapping or hit their cgroup memory limit, and the metrics library everyone blames first. The tools: vmstat and pgmajfault for faults, GC logs and jstat for pauses, perf c2c for contended lines.

IN INTERVIEWS

“Latency went up 10x overnight. Walk me through it.” Start from the percentile that broke, list what changed, and turn each candidate into milliseconds: faults × cost per fault, GC pause × how many requests it reaches, contention × operations per request. Say which threshold was crossed, fix the largest stage first, and verify under the same load.

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

USED IN

Chapters that build on this lesson.