Retries That Cannot Run Away: Backoff, Jitter and Where They Live

A rate-limited endpoint turns away ten thousand mobile clients in the same second. Each one waits a second and tries again, then two seconds, then four. The server now meets ten thousand simultaneous requests at every one of those moments. Retries are supposed to give a struggling dependency room to recover. Written carelessly, they become the load that keeps it down.

Waiting is not the same as spreading out

Exponential backoff lengthens the gap between attempts, which helps. It does nothing about timing. If a thousand callers all began failing at the same instant, they all compute the same schedule, and they stay in step for as long as the outage lasts.

The cure is randomness. If each caller picks its wait from a range instead of a fixed number, the herd thins into a stream. The book names this hazard the "thundering herd" and pairs backoff with jitter for exactly that reason.

A backoff function you can prove

Many hand-written versions add jitter at the end: compute the exponential delay, clamp it to the cap, then add a random bonus. The clamp is now a fiction, because the final wait can be larger than the cap. Putting the randomness inside the bound removes the problem by construction.

export function backoffMs(attempt: number, baseMs: number, capMs: number): number {
  const ceiling = Math.min(capMs, baseMs * 2 ** attempt); // attempt counts from 0
  return Math.floor(Math.random() * ceiling);              // 0 up to, never past, ceiling
}

export async function retrying<T>(call: () => Promise<T>, tries = 4): Promise<T> {
  for (let attempt = 0; ; attempt++) {
    try {
      return await call();
    } catch (err) {
      if (attempt + 1 >= tries || !isRetryable(err)) throw err;
      await new Promise((r) => setTimeout(r, backoffMs(attempt, 200, 5_000)));
    }
  }
}

The argument is short. Math.random() returns a value below 1, so the product is below ceiling, and ceiling is never above capMs. No input can push a wait past the cap, and a test can assert that over a few thousand random draws.

If a zero-millisecond wait bothers you, reserve a minimum: take half the ceiling as a floor and add a random amount up to the other half. The sum still cannot exceed the ceiling. The rule is simply that nothing is added after the clamp.

Who owns which half

The book splits this small feature across two layers. Deciding whether an error is transient (a timeout, a 503, a dropped connection) is a pure function of the error, so it belongs in Utils. The loop sleeps and calls out to the world, so it belongs in Resources. In the sketch above, isRetryable would come from L2 and retrying would live in L3. The same logic is easy to unit test in one place and hard to get wrong in the other.

Two further habits come from general practice rather than from LVD, and both are cheap:

  • Retry in one layer only. If the client, the repository and a gateway each make three attempts, one failed call becomes twenty-seven. Pick the lowest layer that can classify the error, and let everything above it fail fast.
  • Retry only what is safe to repeat. A read, or a write protected by an idempotency key, can be replayed. A bare "create order" cannot, and the loop should not guess.

When "try again" is the wrong answer

Backoff assumes the dependency will be back soon. When it will not, every caller still spends its full budget of waits before giving up. Protecting the rest of the system from a dependency that is down, slow or hogging shared capacity takes other tools, and the book places them in the same Resources-layer section as retry. You can read more about where that layer sits in the Resources layer overview, or in the patterns that keep external systems at arm's length.

The takeaway

Treat a retry loop as code with an invariant, not as a convenience: the wait is random, it grows, and it never exceeds the cap. Keep error classification pure in Utils, keep the sleeping in Resources, and retry at exactly one level.

What the book covers

  • The circuit breaker as a three-state machine, with a reference implementation
  • Bulkheads that give each external dependency its own pool
  • How call timeouts are tuned from observed latency
  • Shallow and deep health checks and what consumes them
  • An exercise that traces circuit breaker state transitions under a failing API