A tale by claude-code · run run_ckt_9f2a · 10 Jul 2026
Track down the checkout slowdown
Checkout p95 latency had tripled to 2.9s since the v4.2 deploy. The cause was an N+1: each cart line fired its own address lookup, so a 40-item cart ran 41 queries. Batching the lookups into a single query cut checkout to 340ms and dropped queries per checkout from 41 to 2. No public API changed.
merged to main · 2026-07-10T09:41:00Z
The tale
Confirmed the regression
Pulled 14 days of checkout p95 from Grafana. Latency was flat around 850ms until the morning of the 8th, then tripled and stayed there. The step lines up exactly with the v4.2 deploy — nothing else shipped that day.
citationGrafana · checkout latency dashboard#
“p95 request duration for POST /checkout rose from 0.85s to 2.90s on 2026-07-08.”
Bisected to the deploy
Only checkout regressed — catalogue and cart p95 were untouched, so this was code, not the database or a noisy neighbour. Narrowed v4.2 to the three commits that touched the checkout path and ranked them by how much query work they could add.
Found the N+1
Commit 7be220 added a delivery estimate per cart line, and each estimate calls addressFor(lineId) — one query per line. Turned on query logging for a single 40-item checkout: 41 queries, 40 of them the same address lookup with a different id.
$LOG_QUERIES=1 bun run replay fixtures/checkout-40-items.json | grep -c SELECT#
41 SELECT * FROM addresses WHERE line_id = $1 -- ×40 (one per cart line) SELECT * FROM carts WHERE id = $1 -- ×1
Batched the lookups
Replaced the per-line addressFor call with one addressesFor(lineIds) that fetches every address in a single query, then maps them back by line id in memory. The public checkout response is byte-for-byte identical — only the query count changed.
diff2 files, +18 −7#
Proved it on real traffic
Replayed the same 40-item checkout, then a 500-request sample captured from production. Query count per checkout dropped from 41 to 2, and p95 came back down to 340ms — below where it sat before the regression.
What it read
Provenance
- run withclaude-code 2.1 · AI-assisted
- published bythe taleseal team — a hand-written example
- reviewed byPriya Menon, staff engineer · Read the diff and the load-test output, re-ran the replay locally, merged. (2026-07-10)
- full sessionraw transcript