Video in progress
One request calls 50 backends in parallel and the p99 latency is very bad. Why is that, and how do you reduce it?
Each backend: p50 = 10 ms, p99 = 200 ms
One request goes to 50 backends in parallel and waits for all of them.
Result: ~40% of requests ≈ 200 ms
In a fan-out the response waits for the slowest backend: even if each one is slow only 1% of the time (at p99), the chance that at least one of the 50 is slow is ~40%. Hedged requests, answering with a partial result, and reducing the fan-out degree help.
📖 Answer
The math: if the probability that a single backend answers fast is 0.99, then all 50 being fast is 0.99⁵⁰ ≈ 0.6. So ~40% of requests wait for at least one "slow" backend. That's why a large part of the overall distribution sits at about a single backend's p99, and the overall p99 is even worse. Search, feed assembly, scatter-gather queries: it's the same everywhere.
What you can do:
Hedged request. You send the request to one replica; if it hasn't answered by, say, the p95 time, you send it to a second replica as well and take whichever comes back first. The extra load is about 5%, but the tail shrinks sharply. Only for idempotent reads.
Tied request. Send to both replicas at once, and when one starts, send the other a "cancel" message. Less load, but more complex.
Partial results. In search, if 48 of the 50 shards answer, return the result at the deadline; the user won't notice the difference. Mark the response as "partial" and track it in a metric.
Reduce the sources of slowness. The tail is often not random: GC pauses, noisy neighbors, cold caches, background compaction. Reducing them on each backend (for example, a low-pause GC, limiting background jobs) affects the whole system.
Reduce the fan-out. If there are many small shards, make them bigger; or two-stage aggregation: not 50, but 7 intermediate aggregators, each collecting 7-8 shards.
Average latency doesn't show this problem at all. You have to watch each backend's p99/p999 and the overall p99 of the fan-out request separately.
Sign up to mark studied questions and save your progress.