If you're asked in an interview how you'd report the latency of a trading system (or of any service, really), the answer the interviewer is hoping for isn't "the average", and I simulated a day of messages to show why. All the numbers below come from that one run.
The simulated day has 24 hours of 100,000 messages each, so 2.4 million in all. Most messages take about 2 µs, a small share of them hit something slow such as a cache miss or an interrupt, and one hour in the afternoon is busy and has a much heavier tail than the rest.
| Measure | Value |
|---|---|
| Mean | 2.12 µs |
| Median (p50) | 2.00 µs |
| p99 | 2.91 µs |
| p99.9 | 34.5 µs |
| Max | 413.7 µs |
The mean sits right next to the median and tells you almost nothing about the tail, while the p99.9 is more than ten times the p99, which is where the trouble actually is. In trading the slow moments tend to be the busy and volatile ones, and those are exactly the moments when being fast earns money. So I'd report percentiles, and I'd always give the count and the period alongside them, because "p99 of 2.91 µs over 2.4 million messages on Tuesday" means something to whoever reads it and "p99 of 2.91 µs" on its own really doesn't.
You can't average percentiles
Suppose you've kept a p99 for every hour and somebody asks you for the whole day. The mean of the 24 hourly p99s comes to 5.47 µs, whereas the day's real p99 is 2.91 µs, and the gap is mostly the busy hour (its own p99 was 65.35 µs) dragging the average up. As far as I know the only correct figure for the whole day is the one computed from all of the data together. In practice you'd keep a histogram for each hour with log-sized buckets, in the style of HdrHistogram, add the counters together and then read the percentile off the sum.
Coordinated omission
However, the third trap is probably the hardest one to spot, because it's hidden in the way most load tests get written (a client that sends a request and waits for the reply, records the time and only then sends the next one). To demonstrate it I ran a client like that against a simulated system that should receive a request every millisecond for one second, where each reply takes 0.05 ms and there's a single 100 ms stall halfway through.
The naive client simply waited through the stall and recorded a single 100 ms measurement before continuing, sending the requests it had missed late and timing each one from the moment it actually went out, so its p99 came out at 0.05 ms. However, if you time every request from when it was originally due (which is how a real user or a real exchange feed would experience it), you'll find 100 requests were delayed by more than 1 ms and the p99 is actually 90.05 ms, so I'd say the original test had probably concealed the stall almost completely.
The remedy I'd normally suggest is sending requests on a fixed schedule that doesn't wait for replies and measuring each request from when it should have gone out. If that isn't possible for some reason, I think you can at least correct the histogram afterwards for the samples you missed (HdrHistogram supports this directly).
I've written all of this up in considerably more detail for low-latency interviews, including what tick-to-trade actually measures and how you'd record histograms without slowing down the hot path, at https://academicquant.co.uk/qd-latency-percentiles?utm_source=devto&utm_medium=post&utm_campaign=devto-latency-percentiles-why
Top comments (0)