Measuring latency honestly: why p99 matters more than the average
The average is the wrong number
When a system reports an average latency of 8 microseconds, it sounds fast. But latency is never evenly spread. Most orders are quick and a few are very slow, because of a cache miss, a garbage collection pause, a context switch or a burst of market data. The average smooths those slow orders away, and the slow orders are exactly the ones that arrive late to a moving market.
That is why we report percentiles. The p50 tells you the typical case. The p99 tells you how bad one order in a hundred is. The p99.9 and the maximum tell you about the moments when the market is busiest, which are also the moments that matter most.
Measure the right path
A single number for the whole system is not enough to act on. We measure each stage of the hot path separately: decoding the market data message, updating the order book, computing the signal, running risk checks and encoding the order. Timestamps are taken with a low overhead clock, and the cost of taking the timestamp itself is measured and subtracted.
Measuring per stage turns a vague goal into specific work. If decode takes 40 percent of the budget, that is where the effort goes.
Test with real traffic shapes
Latency measured on a quiet system is a best case. Real markets arrive in bursts. We replay recorded market data at its original pace and at several times that pace, and we watch how the tail percentiles move. A system whose p99 doubles under a burst has a queue somewhere that needs attention.
Common causes of a long tail
- Memory allocation on the hot path
- Threads that are not pinned to dedicated cores
- Logging that writes synchronously
- Shared data structures with lock contention
- Power saving and frequency scaling left switched on
Set a budget and keep it
We agree a latency budget with the client for each stage, and the build fails when a change pushes a stage over its budget in the benchmark suite. That one rule prevents the slow drift that happens when every small feature adds a few hundred nanoseconds and nobody notices until the total has doubled.
Honest latency work is less about one clever trick and more about measuring carefully, every day, and treating the tail as seriously as the median.
