Timeouts, Retries and Backoff

How to retry failed calls safely without turning a small outage into a big one.

What is it?

Any call over a network can fail or hang. Three tools handle that:

- A timeout caps how long you wait for a response, so a stuck dependency can't hold your threads, connections and users forever. - A retry tries again after a failure, which fixes many brief, transient errors. - Backoff spaces retries out, usually doubling the wait each time (exponential backoff), with jitter (randomness) so clients don't all retry at the same instant.

Used carelessly, retries make outages worse: every client hammering a struggling service multiplies its load. That's a retry storm. Retry budgets and careful choice of which errors to retry keep them in check.

Explain like I'm 10

If a shop's phone line is busy, calling back every second just keeps the line jammed for everyone. Waiting a bit longer each time, at slightly random times, gives the shop room to recover. And if they said 'we don't sell that' (a permanent no), calling back won't help at all.

Examples

Retry with exponential backoff and full jitter

async function callWithRetry(operation, options = {}) {
  const maxAttempts = options.maxAttempts ?? 4;
  const baseDelayMs = options.baseDelayMs ?? 100;
  const maxDelayMs = options.maxDelayMs ?? 5000;

  for (let attempt = 1; ; attempt++) {
    try {
      return await operation({ timeoutMs: 2000 });
    } catch (error) {
      if (!isRetryable(error) || attempt >= maxAttempts) throw error;

      const cap = Math.min(maxDelayMs, baseDelayMs * 2 ** attempt);
      const delayMs = Math.random() * cap; // "full jitter"
      await new Promise((resolve) => setTimeout(resolve, delayMs));
    }
  }
}

function isRetryable(error) {
  if (error.isTimeout || error.isConnectionError) return true;
  return [429, 502, 503, 504].includes(error.status);
}

Each attempt has its own timeout. The wait ceiling doubles per attempt up to a cap, and the actual wait is random below it, which spreads retries out. Only transient errors are retried.

How it works

Timeouts come in layers: connect timeout, per-attempt timeout, and an overall deadline for the whole operation including retries. Set them from measured latency (for example, a bit above the dependency's p99), not defaults, which are often very long or infinite. Pass the remaining deadline downstream (gRPC does this natively) so deep services stop working on requests the caller has already abandoned.

Which errors are retryable?

- Usually yes: connection resets, timeouts, 502, 503, 504, and 429 (after the Retry-After the server sent). - Usually no: 400, 401, 403, 404, 409, 422. The same request will fail again. - Careful with 500 and timeouts on non-idempotent operations: the first attempt may have succeeded. Only retry if the operation is idempotent or carries an idempotency key.

Exponential backoff with jitter: without jitter, all clients that failed at the same moment retry at the same moments, causing synchronized spikes. "Full jitter" (a random wait between 0 and the exponential cap) spreads them evenly.

Retry storms and amplification: if each layer of a 4-deep call chain retries 3 times (4 attempts), one user request can become 4^4 = 256 calls at the bottom. Fix it by retrying at one layer only (usually closest to the failure or at the edge), and by using a retry budget: retries may add at most a fixed fraction (say 10%) on top of normal traffic. When the budget is spent, fail fast. Pair this with a circuit breaker that stops calls to a dependency that's clearly down.

Backoff with full jitter (base 100ms, cap 5s)
attempt 1 fails -> wait random(0, 200ms)
attempt 2 fails -> wait random(0, 400ms)
attempt 3 fails -> wait random(0, 800ms)
attempt 4 fails -> give up (or circuit breaker opens)

Retry amplification (4 attempts at every layer)
User -> API (x4) -> Orders (x4) -> Inventory (x4) -> DB (x4)
1 request ---------------------------------------> up to 256 DB calls

Why does it exist?

Most network failures are brief: a dropped packet, a restarting instance, a momentary overload. Retrying hides them from users. But failure is also contagious: slow dependencies exhaust callers' resources, and naive retries pile load onto services that are already struggling. Timeouts, backoff and budgets let you get the benefit of retries without the cascade.

When to use it

Set timeouts on every network call, always. Retry idempotent operations (or those with idempotency keys) on transient errors, with exponential backoff and jitter. Use a retry budget or circuit breaker anywhere many clients call a shared dependency.

When not to use it

Don't retry validation, auth or not-found errors. Don't retry non-idempotent writes (charging a card, sending a message) without an idempotency key. Don't retry at every layer of a deep call chain. And don't retry when the user is better served by a fast failure and a clear message, for example when the overall deadline is nearly spent.

Common mistakes

  • Relying on default timeouts, which in many HTTP clients are very long or unlimited.

  • Retrying immediately in a tight loop with no backoff, hammering the struggling service.

  • Using exponential backoff without jitter, so all clients retry in synchronized waves.

  • Retrying at every layer of a call chain, multiplying load during an outage.

  • Retrying a timed-out payment request that actually succeeded, charging the customer twice.

Practice exercises

  1. Easy:

    Classify each as retryable or not and explain: 503, 400, connection reset, 401, 429 with Retry-After: 30, 404.

  2. Medium:

    A service's p99 latency is 300ms and the user-facing deadline is 2s. Choose per-attempt timeouts, retry count and backoff, and justify them.

  3. Hard:

    Design a client library used by 200 services that prevents retry storms. Cover budgets, deadline propagation, idempotency and observability.

Interview questions

Why should every network call have a timeout?

Without one, a hung dependency holds the caller's threads, connections and memory indefinitely. Requests pile up until the caller fails too. A timeout turns a hang into a fast, handleable error.

What is exponential backoff?

Increasing the wait between retries multiplicatively, usually doubling, up to a maximum. It gives a failing service progressively more room to recover instead of a constant stream of retries.

Why add jitter?

Clients that failed together would otherwise retry together, creating synchronized load spikes. Randomizing each wait spreads retries over time; full jitter picks a random value between zero and the backoff cap.

Which HTTP errors are typically retryable?

Transient ones: 429 (respecting Retry-After), 502, 503, 504, plus connection errors and timeouts if the operation is idempotent. Client errors like 400, 401, 403, 404 and 422 will fail the same way again.

What's the danger in retrying a timed-out request?

A timeout doesn't mean the server didn't do the work, only that you didn't hear back. Retrying a non-idempotent operation can apply it twice. Use idempotency keys so the server recognizes the retry.

What is a retry storm?

When a dependency slows down, every client retries, multiplying traffic on the already-struggling service and preventing recovery. Retries at multiple layers multiply it further.

What is a retry budget?

A cap on retries as a fraction of normal requests, for example retries may be at most 10% of calls in a time window. During a widespread outage the budget runs out and clients fail fast instead of multiplying load.

What is deadline propagation?

Passing the remaining time budget along with each request so downstream services know when the caller will give up. They can skip or abort work that can't finish in time instead of wasting resources on an abandoned request.

How do retries and circuit breakers work together?

Retries handle short, isolated failures. A circuit breaker tracks the failure rate and, when it's high, stops calls for a cool-down period. Retries then fail fast instead of piling on, and the breaker probes later to see if the dependency recovered.

In a 5-service call chain, where should retries live?

Ideally at one layer, typically the one nearest the failing dependency or at the edge, not at every hop, because attempts multiply per layer. Combine with an end-to-end deadline so retries stop when the user's request can no longer succeed.