Networks drop packets, services restart and databases fail over. A single retry hides most of these blips from users. But retries also multiply traffic: if every layer retries three times, one failing request can become dozens, and a struggling service gets hammered exactly when it needs relief.
Retry only what is safe
| Error | Retry? |
|---|---|
| Connection refused / reset, timeouts | Yes, for idempotent requests |
| 502, 503, 504 | Yes, with backoff (respect Retry-After) |
| 429 Too Many Requests | Yes, after Retry-After |
| 500 Internal Server Error | Sometimes; often a bug that will fail again |
| 400, 401, 403, 404, 422 | No: retrying will not change the answer |
And only retry idempotent operations: GETs, PUTs that set a value, or POSTs protected by an idempotency key. Retrying a non-idempotent payment can charge twice.
Exponential backoff with jitter
Wait longer after each failure, and randomize the wait so many clients do not retry in lockstep:
async function withRetry<T>(fn: () => Promise<T>, attempts = 4, baseMs = 100, capMs = 5000): Promise<T> {
for (let i = 0; ; i++) {
try {
return await fn();
} catch (err) {
if (i >= attempts - 1 || !isRetryable(err)) throw err;
const exp = Math.min(capMs, baseMs * 2 ** i);
const delay = Math.random() * exp; // "full jitter"
await new Promise((r) => setTimeout(r, delay));
}
}
}
Without jitter, a thousand clients that failed at the same moment retry at the same moment, recreating the spike that caused the failure.
Retry budgets
Limit retries as a share of traffic, not just per request. For example, allow retries up to 10% of requests per minute; beyond that, fail fast. This caps the extra load retries can add during an outage.
Retry at one layer
If the browser, the API gateway, the backend and the database driver all retry three times, a failure at the bottom becomes 3⁴ = 81 attempts. Decide which layer retries (usually the one closest to the failure that knows the operation is safe) and turn retries off elsewhere.
Timeouts come first
A retry is only useful if the first attempt gives up in reasonable time. Set connect and read timeouts on every call; see timeouts in distributed systems. The total time across retries must fit within the caller's own timeout.
Pair with a circuit breaker
When a dependency is clearly down, stop calling it for a while instead of retrying every request. A circuit breaker fails fast during outages and probes for recovery.
Observing retries
Retries hide failures from users, which is the point, but you still want to see them:
- Record retries as span events or attributes (
retry.attempt), or as separate client spans, so traces show them. - Chart the retry rate per dependency; a rising retry rate is an early warning before errors reach users.
- Alert when the retry budget is exhausted.
In VitalRix APM, each attempt appears as a client span in the trace, so a request that succeeded on the third try shows two failed calls and their timing. Rising error rates per outgoing host show which dependency is struggling. Related: ETIMEDOUT and ECONNRESET.
A worked example
A checkout service called a fraud-scoring API with three immediate retries and no timeout beyond the default 30 seconds. When the fraud API slowed down, each checkout request waited up to 2 minutes across retries, worker threads filled, and checkout failed entirely. The fix: a 2-second timeout, two retries with exponential backoff and full jitter, a retry budget of 10%, and a circuit breaker that skips the call and falls back to a rules-based score when the API is clearly down. The next fraud-API incident degraded scoring for 20 minutes, with no checkout failures.
Retries in common clients
- AWS SDKs retry with backoff by default; tune max attempts rather than adding your own loop around them.
- gRPC supports retry policies in service config.
- Service meshes can retry at the proxy; make sure the application does not also retry.
- Browser fetch has no retries; add them only for idempotent calls and user-visible recovery.
Checklist
- Timeouts on every call.
- Retry only idempotent operations and retryable errors.
- Exponential backoff with jitter, capped.
- Two or three attempts at most.
- Retries at one layer only.
- A retry budget, and a circuit breaker for hard failures.
- Retries visible in traces and metrics.
How many times should I retry?
Two or three attempts with backoff is enough for transient failures. More rarely helps and adds load during outages.
What is jitter?
Random variation added to the wait time so many clients do not retry at the same moment.
Should I retry 500 errors?
Cautiously. Many 500s are bugs that fail every time. Retry 502, 503 and 504, which are usually transient.
Try VitalRix free
Uptime monitoring, APM, logs, real user monitoring and status pages in one place. No credit card needed.