Skip to content
Goal: Deep understanding✓ Sources verified 2026-11-24Exponential BackoffJittertenacityIdempotency Key

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 ​

ConceptDescription
Exponential backoffThe n-th retry waits base × 2^n, with an upper cap
JitterAdds randomness to the wait so that many clients don't retry at the same moment
Retry budgetLimits the number of retries, or the share of total requests that are retries, to prevent retry storms
IdempotencyPerforming an operation several times has the same effect as performing it once
Idempotency keyThe 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 ​

RetryDon't retry
Timeouts, connection errors, 429, 5xx, LLM service overloaded400 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 ​

python
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 ​

  1. Idempotency key + unique constraint: INSERT ... ON CONFLICT (idempotency_key) DO NOTHING
  2. Cache step results: save a step's output once it succeeds and return it directly on re-runs (see Task State Machine)
  3. Downstream idempotency key support: pass an idempotency key when calling external APIs (many payment APIs support an Idempotency-Key header)

Common pitfalls ​

  1. Multiplying layers of retries: SDK 2 × business logic 3 × queue 3 = up to 36 requests. Retry in exactly one layer.
  2. Retrying non-retryable errors: wastes quota and drags out the time to failure.
  3. No overall time limit: the user waits 5 minutes before seeing a failure.
  4. 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 ​

  1. Why add jitter to exponential backoff? What's the Full Jitter formula?
  2. Which errors should be retried and which shouldn't?
  3. How do retries relate to idempotency? How do you implement idempotency?
  4. What is a retry storm? How do you avoid it?
  5. 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 ​

Vin Amory · AI development × content creation · Technical notes cite official docs in each page’s sources section