Retries
We decide when to try again and how to keep retries from making a bad situation worse. A retry turns a transient blip into a success, and a careless retry policy turns a dependency’s bad minute into an outage. We will cover retryable errors and retry amplification, retry budgets, and hedging and honouring Retry-After.
#Section 13What to retry, and how retries multiply
Many failures are transient. A connection is reset, a server is restarting, a load balancer drops a request during a deploy, a provider returns 503 for a second. Trying again a moment later succeeds. Unit VIII introduced retries for background jobs, and the same rules apply to synchronous calls, with the extra constraint that a user is waiting.
The first rule is to retry only what can succeed and only what is safe. Retryable errors are those that say nothing about the request itself. Connection refused or reset, DNS failures, 429 with Retry-After, 502, 503 and 504. Non-retryable errors describe the request, 400 bad request, 401 and 403, 404, 409 conflict and 422 validation errors, and will fail the same way every time. Timeouts are retryable only if the operation is idempotent or carries an idempotency key, Unit V, because the first attempt may have succeeded.
The second rule is about layers. Suppose a user’s checkout passes through the gateway, the order service and the payment service, and each layer retries failed calls up to 3 times, 4 attempts in all. One user click becomes up to 4 attempts at the gateway, each causing up to 4 at the order service, each causing up to 4 at the payment service, so 4 × 4 × 4 = 64 calls reach the payment provider. When the provider is struggling, that is exactly the load it does not need. This is retry amplification, and in deep call chains it turns a partial outage into a total one.
The fix is to retry at one layer only, usually the one closest to the user or the one with the most context, and have lower layers fail fast. A service that has already retried can signal downstream not to retry again, and some systems pass a header or gRPC metadata to say so. Google’s SRE book recommends a similar rule, that a backend which is overloaded returns an error that tells callers not to retry, rather than letting every layer try again.
Retries also need backoff and jitter, Unit VIII’s full jitter, so that many clients that failed together do not retry together. For synchronous calls the waits are short, tens to hundreds of milliseconds, and the total must fit the remaining deadline from Part IV.
#Section 14Retry budgets
Even at a single layer, retries add load in proportion to the failure rate. If the payment service fails 10% of calls and the client retries each failure up to 3 times, traffic rises by up to 30%. If the service fails completely, every call makes 4 attempts and traffic quadruples. The worse the dependency is doing, the harder its clients push, a positive feedback loop that can keep a recovering service down.
A retry budget caps retries as a fraction of normal traffic. Wren allows retries to add at most 10% to its successful calls. At 1,000 calls per second, at most 100 retries per second are allowed whatever the failure rate. When failures are rare, every failure gets its retries. When the dependency is down, almost no calls are retried, and the dependency sees close to its normal load instead of four times it.
The usual implementation is a token bucket, the algorithm from Part II used in a new place. Each successful call adds 0.1 token, each retry spends 1, and retries are allowed only while tokens remain. gRPC’s retry policy includes exactly this as retry throttling, with maxTokens and tokenRatio settings. Twitter’s Finagle library popularized retry budgets for RPC clients, and Envoy and Linkerd offer them for service meshes, Unit XI.
Budgets are per client, per dependency. A client calling the payment provider and the email provider keeps separate budgets, so a failing email provider does not stop payment retries. And budgets work with circuit breakers, Part VI. The breaker stops all calls when a dependency is clearly down, and the budget limits retries in the grey zone where it is partly working.
Retries also deserve visibility. Wren counts attempts per call and alerts when the retry rate passes a few percent, because a dependency that needs many retries to succeed is unhealthy even if users see no errors, and the extra latency of each retry is visible in the p99.
#Section 15Hedging and honouring Retry-After
Retries react to failure. Hedged requests react to slowness. Google’s paper “The Tail at Scale” by Jeffrey Dean and Luiz André Barroso in 2013 described sending a second copy of a request to another replica if the first has not answered within some time, typically the 95th percentile latency, and using whichever answer arrives first. Wren’s menu reads go to one of several replicas. With a p95 of 40 ms, a request still waiting at 40 ms gets a hedge to a second replica, which answers in 25 ms, so the user waits 65 ms instead of the slow replica’s 260 ms.
Hedging trades a little extra load for a much shorter tail. Since only requests slower than the p95 are hedged, it adds about 5% more requests. It is only safe for idempotent operations, reads and anything carrying an idempotency key, and the losing request should be cancelled to free its resources. Hedging also needs the same restraint as retries. When the whole service is slow, hedging every request doubles its load, so hedges should come from a budget too.
The other half of good retry behaviour is listening. A 429 or 503 with a Retry-After header tells the client exactly when to try again. A well-behaved client waits at least that long, plus jitter, before retrying, rather than applying its own shorter backoff. Wren’s clients for third-party APIs read Retry-After and the provider’s rate-limit headers, Part VIII, and its own API sends them, so clients that listen are spared needless refusals and the service is spared needless load.
The overall picture of a well-behaved synchronous call is short. It has a deadline, a connect and per-attempt timeout, at most a couple of retries for retryable errors with jittered backoff within the deadline, a retry budget, an idempotency key for anything that changes state, and respect for Retry-After. The next part adds the breaker that decides when not to call at all.