Skip to content

Lesson 4 of 9 · 24m

Retry Policy, Backoff, and Jitter

Build retry behavior that distinguishes transient from permanent failure, caps amplification, and avoids synchronized retry storms.

Retries are where good intentions turn into distributed denial-of-service attacks against your own dependencies. A database hiccups, every job fails, every worker retries at once, and the database never gets a quiet second to recover. Retry policy is not an annotation you sprinkle on a job. It is admission control for failure.

Classify the failure before retrying

The first question is not "how many retries?" It is "could this ever succeed unchanged?"

transient     connection reset, 502, timeout, lock contention, rate limit
permanent     validation error, unknown tenant, malformed payload, 403
ambiguous     timeout after request may have committed
dependency    downstream explicitly unavailable or shedding load

Permanent failures should not be retried. Ambiguous failures are why idempotency exists. Dependency failures need backoff and often a circuit breaker.

Use bounded exponential backoff with jitter

A practical delay schedule looks like this:

import random

def retry_delay_seconds(attempt):
    base = 2 ** min(attempt, 8)
    ceiling = min(base, 300)
    return random.uniform(0, ceiling)

Full jitter spreads retries across the window. Without jitter, thousands of workers wake on the same second because they failed in the same second. Add a maximum attempt count and a maximum total age. Infinite retry is just a slow poison queue.

Respect upstream retry metadata

When a dependency says when to come back, believe it:

HTTP/1.1 429 Too Many Requests
Retry-After: 17

A retry scheduler should use the later of local backoff and Retry-After. Likewise, database serialization failures deserve short randomized retries, while a hard validation failure should go straight to dead-letter handling.

Count retries in effect amplification

Every retry can repeat work inside the job as well as the job itself. If a job sends three emails and fails on the third, a naive retry sends the first two again. Idempotency keys and progress checkpoints reduce amplification.

attempt 1: charge ok -> email ok -> write export failed
attempt 2: charge duplicate? -> email duplicate? -> write export

The right design stores durable progress per job or performs the whole operation against idempotent endpoints.

Make retry policy observable

Log attempt number, error class, delay, and retryability. If ValidationError suddenly dominates, you have a deploy or producer bug, not a transient infrastructure problem.

Field notes

  • Ten retries at one second apart is often worse than three retries spread across five minutes. Fast retries mostly measure how quickly you can hammer a sick service.
  • Timeout failures are ambiguous by definition. Treat them as maybe-committed unless the downstream operation is provably idempotent.
  • Do not retry inside a transaction that holds database locks. Backoff should happen outside the transaction boundary.

Was this lesson helpful?

Sign in to rate