Reintento con Backoff

Backoff exponencial · Jitter

Escrito por Darjus Kazakevic · revisado el

Retry only what is safe, wait longer each time, and add randomness.

Many failures are brief: a dropped connection, a restarting pod, a 503 during a deploy. Trying the call again often works. Retry with backoff waits before each new attempt, and the wait grows: 100 ms, 200 ms, 400 ms. Random jitter spreads the attempts, so clients do not all retry at the same moment.

Retries also add load at the worst time. When a service is overloaded, every client that tries three times sends up to three times the traffic. So a retry policy needs limits: which errors to retry, how many attempts, and a budget for the whole service.

En esencia — Retry only safe, brief failures, wait a growing random time between attempts, and cap the total, so retries cannot multiply an outage.

Cuándo recurrir a él

Recurre a él cuando

  • Brief failures: dropped connections, a 503 during a deploy, a 429
  • The call is idempotent, or it carries an idempotency key
  • A short wait is acceptable to the user or the job

Reconsidéralo cuando

  • Client errors like 400, 404 or 422: the next try fails the same way
  • Writes the server cannot recognise as repeats, such as a plain POST /charge
  • A dependency that is down for minutes: a circuit breaker should stop the calls

Conceptos clave

Safe to retry (Idempotency)
A call is safe to retry when doing it twice has the same effect as doing it once. GET, PUT and DELETE are idempotent by design. A POST needs an idempotency key, so the server can recognise the repeat.
Exponential backoff
Double the wait after each failed attempt, up to a cap. With a 100 ms base, the waits are 100, 200, 400 and 800 ms. The growing gap gives a struggling service time to recover.
Jitter (Full jitter)
Randomness added to each wait. Without it, a thousand clients that failed together also retry together, in waves. Full jitter picks a random wait between 0 and the exponential value, which spreads the load with few extra calls.
Retry budget (Retry throttling)
A limit on retries across all requests, not per request: for example, retries may add at most 10% to normal traffic. When the dependency is down, the budget runs out and calls fail fast instead of tripling the load.
Retry amplification (Retry storm)
Retries at several layers multiply. If the gateway, the API and the service each try 3 times, one user request can become 27 calls to the database (3 × 3 × 3). They all arrive during the incident.

Reglas de diseño

Retry at one layer, not at every layer
Three layers that each try three times turn one failed request into 27 calls at the bottom. Choose one layer to own retries, and let the other layers pass the error up.
Retry only idempotent calls and brief errors
Repeat a write only when the server can detect the repeat, for example with an idempotency key. Do not retry client errors such as 400 or 404: the next try fails the same way.
Add full jitter to every backoff
Wait a random time between 0 and the exponential cap. Plain exponential backoff keeps clients in step, so they hit the recovering service in waves.
Cap attempts, total time and retry rate
Use 2–3 attempts, keep the total inside the caller's deadline, and add a retry budget such as 10% of requests. When the budget is spent, fail fast.

Errores habituales

  • Retrying in the SDK, the service mesh and the code at once. During every incident, the failing service sees several times its normal traffic.
  • Backoff without jitter. Retries from many clients arrive in sharp, regular waves, and the service falls over each time it starts to recover.
  • Retrying a timed-out payment without an idempotency key. The customer is charged twice.
  • Ignoring Retry-After on a 429 or 503. The client retries sooner than the server asked and stays rate-limited longer.