Retries with backoff and jitter
Retry transient failures without turning a blip into a self-inflicted outage.
15 min readRetry the right failures
Only transient failures are worth retrying: timeouts, connection resets, 503 Service Unavailable and 429 Too Many Requests. Permanent failures such as 400 Bad Request, a validation error or a 404 for a deleted record will fail identically every time, so retrying them only burns capacity and delays the messages behind them. Classify errors explicitly in each consumer, send permanent ones straight to the dead-letter queue with the reason, and retry the rest.
Exponential backoff
Retrying immediately hits a struggling dependency while it is down. Exponential backoff waits longer after each failure: base x 2^attempt, capped. With a 1-second base you wait 1, 2, 4, 8, 16 seconds, up to a cap of, say, 5 minutes. Stop after a maximum number of attempts or an overall deadline that matches the business need: a password-reset email is useless after an hour, a webhook may be worth retrying for three days. If the server sends a Retry-After header, honour it.
Add jitter
Backoff alone is not enough. When a dependency blips, thousands of clients fail at the same moment, compute the same delays and retry in synchronised waves that knock it over again. Jitter spreads them out. With full jitter the delay is a random value between zero and the backoff ceiling: random(0, min(cap, base x 2^attempt)). The total retry load arrives as a smooth trickle instead of spikes, and the dependency recovers faster.
Retry amplification
Retries multiply across layers. If the client, the API and the service each try three times, one user action can cause 3 x 3 x 3 = 27 calls to the struggling database.
- Retry at one layer, usually the one closest to the failure or the queue consumer
- Use a retry budget: allow retries for at most about 10% of requests, so retries cannot multiply load during an outage
- Pair retries with a circuit breaker that stops calling a dependency that is clearly down
Key takeaways
- Retry transient errors only; send permanent errors to the dead-letter queue
- Exponential backoff with a cap and a deadline gives dependencies room to recover
- Full jitter breaks up synchronised retry storms
- Retry at one layer and budget retries to avoid amplification
Sign in to track lessons and earn badges.