Retries, Backoff and Idempotency
Running a system in production depends on retries, fallbacks and human takeover. Retries look simple, but done wrong they amplify failures.
Core concepts
| Concept | Description |
|---|---|
| Exponential backoff | The n-th retry waits base × 2^n, with an upper cap |
| Jitter | Adds randomness to the wait so that many clients don't retry at the same moment |
| Retry budget | Limits the number of retries, or the share of total requests that are retries, to prevent retry storms |
| Idempotency | Performing an operation several times has the same effect as performing it once |
| Idempotency key | The client generates a unique ID per logical operation, and the server dedupes on it |
How it works
Why you must add jitter
The simulations in the AWS Architecture Blog post Exponential Backoff And Jitter (Marc Brooker) show that with exponential backoff alone, many clients still retry in clusters at the same moment; adding jitter cut the number of calls needed to finish the same work by more than half. The post compares several approaches and recommends "Full Jitter":
sleep = random_between(0, min(cap, base × 2^attempt))AWS Well-Architected also stresses that backoff, jitter and a cap must be used together: backoff without a cap lets the backlog keep growing; a cap without backoff still saturates the backend.
Which errors to retry
| Retry | Don't retry |
|---|---|
| Timeouts, connection errors, 429, 5xx, LLM service overloaded | 400 bad parameters, 401/403, failed business validation |
| Structured output that fails validation (regenerate with the error message, only a few times) | Content-safety refusals (change strategy or hand over to a human) |
On a 429, honour the retry-after header in the response first.
Why retries require idempotency
Retrying assumes "doing it again does no harm". When the network times out you don't know whether the other side already processed the request. If the operation has side effects (pushing to the ERP, charging, sending a message), a non-idempotent operation gets executed twice.
In practice
Classified retries with tenacity
from tenacity import retry, stop_after_attempt, wait_random_exponential, retry_if_exception
def is_retryable(e: Exception) -> bool:
status = getattr(e, "status_code", None)
return isinstance(e, (TimeoutError, ConnectionError)) or status in (408, 409, 429) or (status or 0) >= 500
@retry(
retry=retry_if_exception(is_retryable),
wait=wait_random_exponential(multiplier=1, max=30), # exponential backoff + random jitter, capped at 30 seconds
stop=stop_after_attempt(4),
reraise=True,
)
async def call_llm(req):
return await provider.generate(req)Three ways to implement idempotency
- Idempotency key + unique constraint:
INSERT ... ON CONFLICT (idempotency_key) DO NOTHING - Cache step results: save a step's output once it succeeds and return it directly on re-runs (see Task State Machine)
- Downstream idempotency key support: pass an idempotency key when calling external APIs (many payment APIs support an
Idempotency-Keyheader)
Common pitfalls
- Multiplying layers of retries: SDK 2 × business logic 3 × queue 3 = up to 36 requests. Retry in exactly one layer.
- Retrying non-retryable errors: wastes quota and drags out the time to failure.
- No overall time limit: the user waits 5 minutes before seeing a failure.
- Retries not logged: every retry should be logged for tracing and alerting (a rising retry rate is often a sign of an incoming outage).
Common interview questions
- Why add jitter to exponential backoff? What's the Full Jitter formula?
- Which errors should be retried and which shouldn't?
- How do retries relate to idempotency? How do you implement idempotency?
- What is a retry storm? How do you avoid it?
- What goes wrong when layers of retries stack up?
How it relates to my projects
- AI Document Review Pipeline W2: classified retries in the LLM Gateway; the deliver step uses an idempotency key to avoid pushing to the ERP twice.
Sources
- AWS Architecture Blog: Exponential Backoff And Jitter (Marc Brooker) — Full vs Equal Jitter, more than halving the number of calls
- Amazon Builders' Library: Timeouts, retries, and backoff with jitter
- AWS Well-Architected: REL05-BP03 Control and limit retry calls
- tenacity Docs —
wait_random_exponential,retry_if_exception - MDN: Retry-After
- Stripe Docs: Idempotent requests — an industry reference implementation of idempotency keys
