notes.

Tail latency is a queueing problem, not a code problem

2026-10-24 · performance · 14 min

We spent three weeks optimising the hot path. p50 improved by 12%. p99 did not move, because p99 was never in the hot path.

Two weeks of wrong

The service had a p50 of 8 ms and a p99 of 340 ms. Profiling showed the median request spending almost all of its time in our own code, so that is where we optimised. After three weeks the median was 7 ms and the p99 was still 340 ms.

The mistake was treating the slow requests as slow versions of the fast ones. They were not. Sampling them showed two populations:

PopulationShareWhat it was doing
Fast97%Cache hit, no lock contention
Slow3%Waiting behind a writer holding the shard lock

The queue behind the lock

A single writer held the shard lock for 30–90 ms during compaction. Requests arriving in that window did not become slow requests — they became queued requests, and the queue is served in arrival order. A 90 ms hold with 40 requests per second means up to four requests waiting, each seeing 90 ms of pure wait on top of their own work.

That is why p50 optimisation did nothing: the fast path was already fast. The tail was the wait, and the wait was length of queue multiplied by hold time.

What actually worked

before: p50 8ms   p99 340ms   hold_p99 90ms   max_waiters 40
after:  p50 8ms   p99  41ms   hold_p99 11ms   max_waiters  8

The general shape

If your tail is worse than your median by more than an order of magnitude, look for a queue: a lock, a connection pool, a single-threaded stage, a retry loop. Measure its depth. The fix is almost never making the slow path faster — it is making the queue shorter or the occupancy briefer.


← back to archive