Video in progress

What are a retry budget and a hedged request, and what makes them better than the plain "retry 3 times" policy?

A retry budget limits retries relative to total traffic, say to no more than 10%, so that when a downstream struggles, retries don't add to the load. A hedged request, instead of waiting for a slow response, sends a parallel request to another replica after a set time and brings p99 down.

📖 Answer

The "3 attempts per request" policy is harmless in normal times: errors are rare, so retries are rare. But when a downstream starts working at half capacity, every failed request turns into 3 and the load nearly doubles, exactly when the service is at its weakest. A retry budget behaves differently in that situation.

The retry budget idea: the client counts successful requests in the recent window and allows retries only up to, say, 10% of that volume. If errors are few, every error is retried. If errors are widespread, the budget runs out and the excess retries aren't sent at all. The load goes up at most 1.1 times, not 3 times. Envoy, Linkerd and gRPC's retry throttling work on this principle. In Java it isn't hard to write yourself with a token bucket:

// each successful request adds 0.1 token, each retry takes 1 token
if (!retryBudget.tryAcquire()) {
    throw originalException; // budget exhausted: we don't retry
}

A hedged request solves a different problem: tail latency. If one of the replicas is in a GC pause or on a busy node, the request waits 2 seconds, while another replica would have answered in 20 ms. Hedging: if no response arrives after the p95 time (say 50 ms), the request is sent to a second replica too, whichever answers first is used, and the other is canceled.

The conditions for hedging are strict:

  • only for idempotent requests, usually reads;
  • the share of hedged requests must be capped as well (say 5%), otherwise during a slowdown it also doubles the load;
  • it helps if the downstream supports cancellation, otherwise both requests run to the end.

This is how I usually use them: a retry budget on all service-to-service calls (often through the service mesh), and hedging only on very latency-sensitive read paths, such as search or reading from the price cache.