Sort failures before you retry them
Not every failure deserves a retry.
| Failure | Retry? | Why |
|---|---|---|
| Invalid request: bad parameters, prompt too long | No, mark errored | it fails the same way every time |
| Worker died, lease lapsed, transient server error | Yes, with backoff | usually transient, and another worker will do |
| Preempted, or no capacity | Requeue with a delay, without counting an attempt | it's capacity, not the request |
| On a worker that crashed, twice | No, quarantine it | probably a poison request |
A retry helps only when the next try can go differently. So decide which kind of failure you have first.
Back off, by a random time up to a cap
Ten requests fail together and back off up to 8 s. Without jitter all 10 retry at second 8. With full jitter they spread out, at most 2 in any second.
Back off with capped exponential delays and jitter. In capped exponential backoff, each retry waits a constant factor longer than the last, up to a maximum.
The AWS Architecture Blog post that made this standard simulated it. Retries backed off in step still arrive in clusters, as in the first row. So its full jitter version sleeps a random time between zero and the capped backoff:
sleep = random_between(0, min(cap, base * 2 ** attempt))
random_between stands for any uniform random draw, such as Python's random.uniform. The name isn't the point.
| Attempt (base 1 s, cap 60 s) | 0 | 1 | 2 | 3 | 4 | 5 | 6 and later |
|---|---|---|---|---|---|---|---|
| Longest sleep | 1 s | 2 s | 4 s | 8 s | 16 s | 32 s | 60 s |
| Average sleep | 0.5 s | 1 s | 2 s | 4 s | 8 s | 16 s | 30 s |
The post's verdict: the return on the extra complexity is huge, and jittered backoff should be a standard approach for remote clients.
Cap the attempts too. Google's SRE book uses a per-request retry budget of up to three attempts.
In batch the delay is nearly free, since a request with 20 hours left can wait a minute unnoticed. So back off generously.
In the editor, write next_step(kind, crashes, attempt): the decision for a failed request, from the table, and how long to sleep before it. draw is the random draw, random.uniform unless a test passes its own.