Retries, Backoff & Jitter Under Load
Retries turn transient failures (a dropped connection, a pod restarting) into successes, which is why every client library has them. During a real outage they do the opposite: every failure becomes several more requests, sent to a service that is already failing. Idempotency and Retries explains when a retry is safe and how exponential backoff and jitter spread retries out. This lesson measures what that spreading means for recovery after an outage.
The model
A service handles 100 requests per tick (a tick is 100 ms) and normally receives 70 new requests per tick, so it has 30% spare capacity. At tick 100 a restart drops 1,500 queued requests at once, which is exactly the situation where clients fail together. Every failed request is retried up to 6 times; a request that arrives when the service is already at capacity fails (an overload rejection) and is retried too. Four retry policies:
| Policy | Delay before retry n (n = 0, 1, …) |
|---|---|
| no retries | — |
| immediate | 1 tick |
| exponential | 4 × 2^n ticks (0.4 s, 0.8 s, 1.6 s, …), capped at 64 |
| exponential + full jitter | random between 1 and 4 × 2^n ticks (seeded) |
import random
CAPACITY, NEW_PER_TICK, TICKS = 100, 70, 400
BURST_AT, BURST = 100, 1500 # a restart drops 1,500 queued requests at once
MAX_RETRIES = 6
def run(policy, seed=1):
rng = random.Random(seed)
pending = {BURST_AT: [0] * BURST}
peak = gave_up = served = retries = 0
last_overload = BURST_AT
for t in range(TICKS):
arrivals = [0] * NEW_PER_TICK + pending.pop(t, [])
load = len(arrivals)
if t > BURST_AT:
peak = max(peak, load) # the worst tick after the restart itself
ok = min(load, CAPACITY)
served += ok
if load > CAPACITY:
last_overload = t
for tries in arrivals[ok:]:
if policy == "no retries" or tries >= MAX_RETRIES:
gave_up += 1
continue
retries += 1
if policy == "immediate":
delay = 1
else:
delay = min(64, 4 * 2 ** tries)
if policy == "exponential + full jitter":
delay = rng.randint(1, delay)
pending.setdefault(t + delay, []).append(tries + 1)
print(f"{policy:<26} later peak {peak:>4}/tick overloaded until +{(last_overload - BURST_AT) / 10:4.1f} s "
f"retries sent {retries:>5} gave up {gave_up:>5}")
for policy in ("no retries", "immediate", "exponential", "exponential + full jitter"):
run(policy)no retries later peak 70/tick overloaded until + 0.0 s retries sent 0 gave up 1470
immediate later peak 1540/tick overloaded until + 0.6 s retries sent 8370 gave up 1290
exponential later peak 1540/tick overloaded until +18.8 s retries sent 8370 gave up 1290
exponential + full jitter later peak 561/tick overloaded until + 3.7 s retries sent 4924 gave up 2
How to read it:
- No retries: the service is fine immediately, but 1,470 requests are simply lost.
- Immediate retries send all failed requests back on the next tick, and the next, and the next. Each wave is far above capacity, so most fail again and use up all six attempts within 0.6 seconds: thousands of extra requests, and almost as many lost as with no retries at all.
- Exponential backoff without jitter only spaces the waves out. Clients that failed together retry together, so every wave still overloads the service; it stays overloaded for almost 19 seconds and loses just as many.
- Full jitter spreads each wave over its whole backoff interval. The spare 30 requests per tick absorb the retries a few at a time: the peak is a third of the others', the overload ends within 4 seconds, fewer retries are sent, and almost every request eventually succeeds.
Change BURST, CAPACITY or NEW_PER_TICK and run it again: with little spare capacity (try NEW_PER_TICK = 95)
even jittered retries struggle, which is where the next lesson's retry budgets come in.
A good retry policy
# the shape of a production retry loop (non-run: real code would call the network)
def call_with_retries(do_request, deadline, max_attempts=3, base=0.1, cap=2.0, rng=random.Random()):
for attempt in range(max_attempts):
try:
return do_request(timeout=min(1.0, deadline.remaining()))
except RetryableError: # 503, 429 with Retry-After, connection reset
if attempt == max_attempts - 1 or not retry_budget.allow():
raise
sleep = min(deadline.remaining(), rng.uniform(0, min(cap, base * 2 ** attempt)))
time.sleep(sleep)| Ingredient | Why |
|---|---|
| retry only retryable errors | a 400 or a validation error will fail again; a 503 or reset may not |
| retry only idempotent operations (or use idempotency keys) | otherwise a retry can charge a card twice |
| few attempts (2–3) | each attempt multiplies load during an incident |
| exponential backoff with full jitter | spreads retries, as the model shows |
honour Retry-After |
the server knows when it can take traffic again |
| stay within the deadline | a retry after the user gave up is wasted work (Timeouts & Deadlines) |
| a retry budget | caps retries as a share of traffic (next lesson) |
Test yourself
Why did immediate retries lose many requests even though they retry more?
- They used fewer attempts
- All accumulated retries arrive at once, overload the recovering service, fail again and exhaust their attempts
- The service ignores retries
- Random chance
What does jitter add to exponential backoff?
- Longer delays
- Randomness, so clients that failed at the same moment don't retry at the same moment
- Fewer attempts
- Idempotency
Which error should a client normally NOT retry?
- 503 Service Unavailable
- Connection reset
- 400 Bad Request
- 429 with Retry-After