28/41
Part 5 of 7 · Failure handlingLearn and do

Sort failures before you retry them

Not every failure deserves a retry.

FailureRetry?Why
Invalid request: bad parameters, prompt too longNo, mark erroredit fails the same way every time
Worker died, lease lapsed, transient server errorYes, with backoffusually transient, and another worker will do
Preempted, or no capacityRequeue with a delay, without counting an attemptit's capacity, not the request
On a worker that crashed, twiceNo, quarantine itprobably 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

Retries in the busiest second
no jitter
10
full jitter
2

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)0123456 and later
Longest sleep1 s2 s4 s8 s16 s32 s60 s
Average sleep0.5 s1 s2 s4 s8 s16 s30 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.