Scrivio Interview library
DebuggingIntermediate6 min read · updated 13 November 2026

A search endpoint fans out every request to 20 index shards in parallel and waits for all of them. Each shard answers in 8 ms at p50 and 120 ms at p99, yet the endpoint's p50 is already near 120 ms and its p99 is over 400 ms.

Explain why the endpoint is so much slower than a typical shard, and what you can do about it.

A request that waits for all 20 shards is as slow as the slowest one. The chance that all 20 answer under their own p99 is 0.99^20, about 82 percent, so about 18 percent of requests wait for at least one shard's tail, and the endpoint's median is set by roughly each shard's 97th percentile. With 100 shards, about 63 percent of requests would hit a tail. We fix it by removing the causes of slow shard responses (GC pauses, compaction, cold caches, noisy neighbours), by tail-tolerant techniques such as hedged requests, timeouts with partial results and fewer, larger shards, and by measuring per-shard latency to find which shard or host drives the tail.

Why this question is asked

Fan-out, sending one request to many servers in parallel and combining their answers, is how search, feeds and many analytics systems work. It has a surprising property: a system built from fast parts becomes slow as a whole, because rare slow responses become common when we wait for many of them. This is often called the tail at scale. Interviewers ask about it because it explains real incidents where every individual service looks healthy and the user experience is still poor. In this answer, we will compute why the endpoint is slow, see how it worsens with more shards, and go through the fixes, starting with the cause and then the techniques that tolerate the tail.

1A Quick Recap: Percentiles and Tails

A percentile describes the latency distribution. The p50, or median, is the latency half of requests beat. The p99 is the latency 99 percent of requests beat; the remaining 1 percent, the tail, is slower. For each shard here, p50 is 8 ms and p99 is 120 ms. The large gap between them tells us the shards are usually fast but occasionally very slow, which is typical when something like a garbage collection pause or a disk flush stalls a request.

For a single call, a slow 1 percent may be acceptable. The problem appears when a request makes many calls and must wait for all of them.

2Why Waiting for All Shards Is So Slow

The endpoint sends the query to 20 shards in parallel and returns only when every shard has answered. Its latency is therefore the maximum of 20 shard latencies. For the endpoint to be fast, every one of the 20 must be fast.

If each shard independently answers under its p99 99 percent of the time, the probability that all 20 do is 0.99 to the power 20.

Java
double allFast20 = Math.pow(0.99, 20); // 0.818: 82% of requests see no shard tail
double anySlow20 = 1 - allFast20; // 0.182: 18% wait for at least one tail
double anySlow100 = 1 - Math.pow(0.99, 100); // 0.634: with 100 shards, 63% hit a tail
// the endpoint's median is the shard percentile q where q^20 = 0.5
double q = Math.pow(0.5, 1.0 / 20); // 0.966: endpoint p50 = shard p96.6
How fan-out turns the shard tail into the normal case
Shards waited forRequests hitting a shard p99 tailEndpoint p50 equals shard
11 percentp50
5about 5 percentabout p87
20about 18 percentabout p97
100about 63 percentabout p99.3

The last line explains the puzzle in the question. For the endpoint's median request, the slowest of the 20 shards sits at about the shard's 97th percentile. With a heavy tail like this one, the shard's 97th percentile is already close to its p99 of 120 ms, so the endpoint's p50 is near 120 ms even though a typical shard answers in 8 ms. The endpoint's p99, in turn, corresponds to a shard percentile of about 99.95, deep in the tail, which is why it exceeds 400 ms.

3Fix 1: Reduce the Tail at Its Source

The most effective fix is to make the shards' tails smaller, because every technique that tolerates a tail costs something. So we find out why a shard is occasionally 15 times slower than usual. Common causes are garbage collection pauses in the JVM, background work such as segment merging in Lucene-based indexes or compaction in LSM stores, cold caches after a restart or on rarely used data, a noisy neighbour process on the same host taking CPU or disk, and queueing when one shard holds a hot slice of data.

Each cause has a known fix: a low-pause collector such as ZGC or G1 with tuned regions, throttling or scheduling merges and compaction, warming caches before a node takes traffic, isolating the search processes on dedicated hosts or with CPU limits, and rebalancing hot data. We fix the largest cause first, which we can only know by measuring.

4Fix 2: Tail-Tolerant Techniques

Even after the sources are reduced, some tail remains, so we design the endpoint to tolerate it. The best-known technique is the hedged request. Each shard has a replica. The endpoint sends the query to one copy, and if it has not answered within a delay such as the shard's p95, say 40 ms, it sends the same query to the other copy and uses whichever answers first. Because the second copy is sent only for the slowest 5 percent, the extra load is about 5 percent, but most tail responses are cut short.

Step throughA hedged request to one shard
EndpointShard A copy 1Shard A copy 2
query
no answer after 40 ms (p95)
same query, hedged
result at 49 ms
cancel
would have answered at 130 ms

6 messages in order. Play them, or step through them one at a time.

Only requests slower than the p95 delay are duplicated, so the extra load is about 5 percent.

Two further techniques depend on the product. If a search can show good results without every shard, a timeout that returns partial results after, say, 100 ms, marked as possibly incomplete, caps the latency outright. And fewer, larger shards reduce the fan-out itself: 10 shards instead of 20 halves the number of chances to hit a tail, as long as each shard can still answer quickly.

5Measuring to Find What Drives the Tail

Finally, we instrument the fan-out. For each request, we record each shard's latency and which shard was the slowest. Aggregating that tells us whether the tail is spread evenly, which points to a common cause like GC, or concentrated on particular shards or hosts, which points to hot data or a bad machine. We also record how long the endpoint waited for the slowest shard after the second slowest had answered; a large gap shows that a single straggler is costing us. Distributed tracing with OpenTelemetry makes this straightforward, and per-shard latency histograms in the metrics system make it visible over time.

6Follow-up questions

What does hedging cost, and how do you keep it from doubling load?

Each hedge is an extra request, so the cost is the fraction of requests that get hedged. Sending the hedge only after the p95 delay limits that to about 5 percent extra load. We also cancel the slower copy once one answers, cap the rate of hedges, for example at 10 percent of traffic, so a general slowdown does not trigger hedges everywhere and double the load exactly when the system is struggling, and send the hedge to a different host or zone so it does not hit the same cause of slowness.

Why not just retry on timeout?

A retry starts only after the timeout, so the request has already been slow for the full timeout. A hedge is sent earlier, at the p95 delay, while the first copy is still running, so it shortens the tail rather than adding to it.

Is the independence assumption realistic?

Not entirely. If all shards pause together, for example after a deploy, the tails are correlated and the formula underestimates how often all are slow at once. But the general conclusion, that fan-out amplifies tails, holds either way.

7Conclusion

In this answer, we saw that a fan-out endpoint is as slow as its slowest shard: with 20 shards, about 18 percent of requests hit a shard's p99 tail, and the endpoint's median corresponds to roughly each shard's 97th percentile, which explains a p50 near 120 ms. With 100 shards the tail becomes the normal case. We reduce the tail at its source, tolerate what remains with hedged requests, partial results and fewer shards, and measure per-shard latency to find what drives it.

What to say in the interviewAbout 90 seconds aloud

Open with

The endpoint waits for all twenty shards, so it is as slow as the slowest one. If each shard is under its p99 ninety-nine percent of the time, the chance that all twenty are is 0.99 to the twentieth, about eighty-two percent, so roughly one request in five waits for at least one shard's tail.

Then, if they want more

In fact the endpoint's median corresponds to about each shard's ninety-seventh percentile, and with a heavy tail like 8 milliseconds at p50 and 120 at p99, that is already near 120. With a hundred shards it would be sixty-three percent of requests hitting a tail, so the shard p99 becomes the endpoint's typical latency.

To fix it, I would first attack the source: find out why a shard is sometimes fifteen times slower, whether GC pauses, segment merging or compaction, cold caches, or noisy neighbours, and fix the biggest cause.

Then I would tolerate what remains: hedged requests, where after the p95 delay we send the same query to the replica and take the first answer, costing about five percent extra load; timeouts with partial results if the product allows; and fewer, larger shards to reduce fan-out.

And I would measure per-shard latency and which shard was slowest per request, so we know whether it is one bad host or a general cause. To keep hedging safe, I cap the hedge rate and cancel the loser.

About this answer

Last updated
13 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the official documentation for the version you use. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "Explain why the endpoint is so much slower than a typical shard, and what you can do...": https://scrivio-interview-library.netlify.app/architecture/sd-fundamentals/8b53b6b78f/