Retries
medium 30 minA retry repeats a failed call in the hope that the problem was short.
Video lesson · Chapter 1 of 5
The coffee order with a missing reply
Retries in 85 seconds
1:25Reel coming soon
Theory
Read or listen first. About 2 min.
What it is
A retry is another attempt at a call that failed. It turns many short failures, such as a lost packet, a restart or a moment of overload, into nothing worse than a small delay.
A retry policy answers six questions: which failures to repeat, how often, how long to wait in between, how much randomness to add, when to stop, and what makes a repeat safe.
Why it exists
Short failures are normal in a network. If 1 call in 100 fails for a reason that is gone a moment later, one retry leaves 1 in 10,000.
A retry is also selfish: the client takes a second share of the server. When the server fails because it is overloaded, retries are more of what hurt it. That is why limits are as much a part of retrying as the retry.
A short history
1971
ALOHAnet goes into operation in Hawaii. A station that gets no acknowledgement waits a random time and sends again.
1989
RFC 1122 requires internet hosts to back off their TCP retransmissions exponentially.
1997
RFC 2068, the first HTTP/1.1, defines idempotent methods and describes when a client may repeat a request.
2012
RFC 6585 adds status 429 Too Many Requests, which may carry Retry-After.
2015
Marc Brooker compares kinds of jitter on the AWS Architecture Blog. Backoff without jitter is the clear loser.
2019
The AWS Builders' Library publishes its article on timeouts, retries and backoff with jitter.
2021
Metastable Failures in Distributed Systems, a paper at the HotOS workshop, studies overloads that keep themselves alive. Retries are a typical cause.
Words to know
- Transient failure
- One that goes away by itself. The only kind worth a retry.
- Backoff
- Waiting longer before each further attempt.
- Jitter
- A random part of the wait, so that clients spread out.
- Cap
- The longest single wait.
- Thundering herd
- Many clients arriving at the same moment.
- Retry budget
- A limit on how much load retries may add, for example 10%.
- Idempotent
- Repeating it has the same effect as doing it once.
- Retry amplification
- Retries in several layers multiply.
- Dead letter queue
- Where a job goes when its attempts are used up, for a person to look at.
How it works
Step through the idea, or press Play.
May I?. Temporary failure, safe to repeat, time and budget left
Understand it
Start here
The narrow gate
Imagine four friends who run to a playground. Its gate is so narrow that only one can pass. What they do after bumping into each other decides how long it takes.
The gate never became wider. Only the times of arrival changed.
Decide
Retry or not
Usually worth a retry
- A timeout or a dropped connection, if a repeat is safe
- Connection refused, for example during a restart
- 502, 503 and 504
- A database deadlock or a serialization failure: run the whole transaction again
- 429, after the time in Retry-After
Not worth it
- Most 4xx: the same request is wrong the same way
- 401 and 403. With an expired token, get a new one once, then repeat
- Validation errors
- Business rules, such as not enough money
- Anything unsafe to repeat without a key
Status codes
| Answer | Retry? | Note |
|---|---|---|
| 400, 404, 409, 422 | No | 409 may need fresh data first |
| 401, 403 | No | Renew the token once on 401 |
| 408, 429 | Yes, later | Honour Retry-After |
| 500 | Perhaps | It may be a bug that fails every time. Once, if safe |
| 501, 505 | No | It will not exist in two seconds either |
| 502, 503, 504 | Yes, with backoff | 504: the work may have been done |
| Connection refused | Yes | The request was not processed |
| Read timeout, reset | Only if safe | Unknown result. See timeouts |
Four questions before a retry
One call with two retries
Waiting
Four ways to wait
| Strategy | Waits with base 1 s | Formula | For |
|---|---|---|---|
| Immediate | 0, 0, 0 s | 0 | Once, for a rare glitch or a lost race for a lock. Never against overload |
| Fixed | 1, 1, 1 s | base | Simple jobs with few clients |
| Linear | 1, 2, 3, 4 s | base x attempt | Gentle growth |
| Exponential | 1, 2, 4, 8, 16 s | base x 2^(attempt - 1) | Network and API calls |
| Exponential with jitter | random below 1, 2, 4, 8 s | random(0, exponential) | Many clients: nearly always |
How long does that take?
What retries buy
Jitter
Why randomness matters more than the wait
Clients that fail together were put in step by the failure. If all of them wait the same time, they stay in step. Jitter breaks the step.
| Kind | Wait | Character |
|---|---|---|
| None | delay | Waves of the same height, further and further apart |
| Add-on (old code) | delay + random(0, delay / 2) | Mild. The second wait is 2 to 3 s, so the wave is only smeared |
| Equal | delay / 2 + random(0, delay / 2) | Keeps a minimum wait |
| Full | random(0, delay) | Widest spread, little work. A good default |
| Decorrelated | min(cap, random(base, previous x 3)) | Depends on the last wait, not on the attempt number |
delay is min(cap, base x 2^(attempt - 1)). The AWS article compares them: full and decorrelated are close, equal is the weakest of the jittered ones, no jitter is the clear loser.Our own experiment (the code tab)
| 100 clients, 10 served per time slot | Calls in total | Finished after |
|---|---|---|
| Immediate | 550 | 10 slots |
| Fixed | 550 | 19 slots |
| Linear | 550 | 55 slots |
| Exponential, no jitter | 550 | 161 slots |
| Full jitter | 360 | 21 slots |
| Equal jitter | 398 | 29 slots |
| Decorrelated jitter | 274 | 18 slots |
Safety
Repeating must be harmless
The answer that got lost
Harmless to repeat
- GET and other reads
- PUT of a whole resource
- DELETE by id (the second may answer 404)
- Set value = X
- Upsert with a unique key
Not harmless by itself
- POST that creates something
- Increment a counter
- Append to a list
- Transfer money
- Send an e-mail or a notification
POST /api/paymentsIdempotency-Key: pay_abc123_1234567890The server stores key -> result.A request with a known key returns the stored result.HTTP agrees: a client should not repeat a request with a non-idempotent method automatically, unless it knows that the repeat is safe.
Danger
When retries make it worse
Expected attempts per request
And layers multiply
How retries keep an overload alive
Limits
Retry in one place, with a budget
One layer retries
A retry budget as a token bucket
- 1
Earn
Every normal call earns a part of a token, for example 0.1.
- 2
Spend
Every retry costs one token. No token, no retry.
- 3
Keep a few
A small reserve allows retries at low traffic.
- 4
Effect
In good times there are always tokens. In an outage retries add about 10%, instead of 300% with four attempts.
The Google SRE book describes both limits together: at most three attempts per request, and a client-wide ratio of retries below 10%. Service meshes offer such budgets too, for example Envoy.
Background work
Retrying without a waiting user
Minutes, not milliseconds
Here the waits can be 1 minute, 5 minutes, an hour. After the last attempt the job goes to a dead letter queue for a person. Webhooks use the same pattern for delivery.
Best practice
Six rules
Set a maximum
Never retry for ever. Two or three attempts when a person waits, 3 to 5 in a job, more only with long delays.
Backoff with jitter
Exponential, capped, random.
Only transient errors
List what may be repeated. Everything else is not.
Make it safe
Idempotency keys for writes.
Cap and deadline
A cap of 30 to 60 s for jobs. For people the deadline of the request is the cap.
Log and count
Attempts per request, retry ratio, successes after a retry. A rising ratio is an early warning.
Summary
The whole topic in one minute.
Retry only temporary failures of calls that are safe to repeat. Wait a random time below a limit that doubles and has a cap. Stop after a few attempts, at the deadline, or when the budget is empty.
Retry in one layer. Obey Retry-After. A retry that makes an outage longer is worse than the error it wanted to hide.
- Temporary, safe, time left, budget left: four yeses before a retry.
- wait = random(0, min(cap, base x 2^(attempt - 1))).
- Jitter helps more than longer waits.
- Retry in one layer, at most 10% extra.
- Same key for every attempt of one write.