Tutorial

Retry with Backoff

Retry a transient failure with exponential backoff and jitter, cap the attempts, and classify which errors are worth retrying.

On this page

  1. Introduction
  2. Classify transient
  3. Exponential delay
  4. Add jitter
  5. Cap the budget
  6. Idempotent calls
  7. Test the split
  8. Sources

Introduction

This guide covers retry with exponential backoff and jitter, a recurring concern in the resilience scenario. Retrying immediately in a tight loop hammers the service and usually fails the same way. Exponential backoff spaces retries further apart, jitter stops a herd of clients from synchronizing, and a retry budget caps the attempts so a down service does not retry forever.

Classify transient

Retry only 5xx and network errors, not 4xx, because retrying a 404 will not make it succeed and wastes the budget.

The trade-off worth naming is that retry only 5xx and network errors, not 4xx, because retrying a 404 will not make it succeed and wastes the budget adds a measurable step to the hot path, so measure it before assuming it is free. A reasonable approach is to validate once at the edge, fail closed with a structured error, and log the rejection reason so a spike in bad input is visible on a dashboard rather than silent. Pair the implementation with a test that asserts a valid payload passes and at least one representative violation is rejected with the expected message; this is the cheapest way to keep the guarantee honest as the schema evolves.

function shouldRetry(err) {
  return err.code === 'ECONNRESET' || (err.status >= 500 && err.status < 600);
}

Exponential delay

Wait a base delay times 2 to the attempt power, so retries space out and give the service time to recover instead of hammering it.

The trade-off worth naming is that wait a base delay times 2 to the attempt power, so retries space out and give the service time to recover instead of hammering it adds a measurable step to the hot path, so measure it before assuming it is free. A reasonable approach is to validate once at the edge, fail closed with a structured error, and log the rejection reason so a spike in bad input is visible on a dashboard rather than silent. Pair the implementation with a test that asserts a valid payload passes and at least one representative violation is rejected with the expected message; this is the cheapest way to keep the guarantee honest as the schema evolves.

async function retry(fn, tries = 4, base = 200) {
  for (let i = 0; i < tries; i++) {
    try { return await fn(); }
    catch (e) { if (!shouldRetry(e) || i === tries - 1) throw e; }
    await new Promise(r => setTimeout(r, base * 2 ** i * (0.5 + Math.random())));
  }
}

Add jitter

Multiply the delay by a random factor so a retry storm does not synchronize on the same slot, which is the thundering herd.

The trade-off worth naming is that multiply the delay by a random factor so a retry storm does not synchronize on the same slot, which is the thundering herd adds a measurable step to the hot path, so measure it before assuming it is free. A reasonable approach is to validate once at the edge, fail closed with a structured error, and log the rejection reason so a spike in bad input is visible on a dashboard rather than silent. Pair the implementation with a test that asserts a valid payload passes and at least one representative violation is rejected with the expected message; this is the cheapest way to keep the guarantee honest as the schema evolves.

Cap the budget

Give up after a small number of tries so a down service does not retry forever, and let the caller decide the fallback.

The trade-off worth naming is that give up after a small number of tries so a down service does not retry forever, and let the caller decide the fallback adds a measurable step to the hot path, so measure it before assuming it is free. A reasonable approach is to validate once at the edge, fail closed with a structured error, and log the rejection reason so a spike in bad input is visible on a dashboard rather than silent. Pair the implementation with a test that asserts a valid payload passes and at least one representative violation is rejected with the expected message; this is the cheapest way to keep the guarantee honest as the schema evolves.

Idempotent calls

Retry only idempotent operations, or make the write idempotent with a request id, so a retry that lands twice does not create two resources.

The trade-off worth naming is that retry only idempotent operations, or make the write idempotent with a request id, so a retry that lands twice does not create two resources adds a measurable step to the hot path, so measure it before assuming it is free. A reasonable approach is to validate once at the edge, fail closed with a structured error, and log the rejection reason so a spike in bad input is visible on a dashboard rather than silent. Pair the implementation with a test that asserts a valid payload passes and at least one representative violation is rejected with the expected message; this is the cheapest way to keep the guarantee honest as the schema evolves.

Test the split

Assert a 503 retries up to the budget and a 404 fails immediately, and that the delays grow, so the policy is correct.

The trade-off worth naming is that assert a 503 retries up to the budget and a 404 fails immediately, and that the delays grow, so the policy is correct adds a measurable step to the hot path, so measure it before assuming it is free. A reasonable approach is to validate once at the edge, fail closed with a structured error, and log the rejection reason so a spike in bad input is visible on a dashboard rather than silent. Pair the implementation with a test that asserts a valid payload passes and at least one representative violation is rejected with the expected message; this is the cheapest way to keep the guarantee honest as the schema evolves.

Sources

Sources: Retry pattern, Exponential backoff.

FAQ

Why jitter on backoff?
Without jitter, every client that failed at the same time retries on the same exponential slot, which hammers the server again; jitter spreads them out.
How many retries?
Three to five for a user-facing call, more for a background job; the right number is the one where the total wait plus the failure path stays inside your latency budget.

Related Examples