Latency budgets and the tail
Averages hide the requests users complain about.
Which latency to set the target on
Why set the latency target on the p99, not the average?
Sort 100 requests from fastest to slowest. The 50th is the p50, the median: half are faster. The 99th is the p99, the latency 99% of requests beat.
Say a user runs 20 searches in a session, each slow 1% of the time. The chance all 20 are fast is 0.99^20 = 0.82. So the chance of hitting at least one slow one is 1 - 0.82 = 18%. Set targets on p99, and budget against it.
A budget splits the p99 target across the stages
A budget starts from the end-to-end p99 target and gives each stage a share. Here a search has a 200 ms p99 target. The ranking stage costs per item, so its time is items times microseconds per item: 100 µs each here.
At 500 items, the ranking model takes 50 ms, and the request totals 155 ms: 45 to spare. At 1,000 items, ranking takes 100 ms, and the total is 205 ms, 5 over.
Drag the ranking model's items from 500 to 1,000 and watch the verdict flip. Then try to win the time back elsewhere.
Ranking model: 100 µs per item.
Interviewers like this exact squeeze, because the fix is a design choice, not a tuning knob:
- score fewer candidates with the heavy model
- make each one cheaper
- move work off the request path
A budget is a plan, not a measurement. Stage p99s don't add up exactly to the end-to-end p99, so measure the whole request too.
In the editor, write session_slow(n, p_fast): the chance that a session of n requests meets at least one slow one. Then write max_items(target_ms, us_per_item, rest_ms): the most items the ranking model can score when the other stages take rest_ms.
- 1
Write
session_slow(n, p_fast): the chance that a session of n requests hits at least one slow one. - 2
Write
max_items(target_ms, us_per_item, rest_ms): the most items the ranking stage can score inside the target.
Run your code to check it. Output shows here.