Skip to content
Tutorials Center
My learning

Topics

Lesson 19 of 39 ยท Async Patterns for Requests

Retries, Backoff and Resilience

Learn when to retry failed requests, how exponential backoff with jitter works, why idempotency matters, how to honor 429 Retry-After, and build a fetch wrapper.

Networks fail in small, temporary ways: a Wi-Fi hiccup, a server restarting, a load balancer returning a 503 for two seconds. A well-built page treats many of these as transient and tries again before bothering the user. But retrying carelessly can make things worse: it can hammer a server that is already struggling, or charge a customer twice.

This lesson covers which failures deserve a retry, how to space retries out with exponential backoff and jitter, why only some requests are safe to repeat, how to respect a server that says "slow down" with status 429, and how to put it all in one reusable wrapper. Error basics are in Handling Errors in fetch.

When should you retry?

Ask two questions: could trying again plausibly succeed? and is it safe to do twice?

Situation Retry? Why
Network failure (fetch rejects with TypeError) Yes Often temporary
500, 502, 503, 504 Yes Server or gateway trouble that may pass
429 Too Many Requests Yes, after waiting The server asked you to slow down
408 Request Timeout Yes The request itself may be fine
400, 401, 403, 404, 422 No Repeating the same request gives the same answer
User cancelled (AbortError) No The user does not want it
flowchart TD
    A["Send request"] --> B{"Result?"}
    B -- "2xx" --> OK["Done"]
    B -- "4xx (not 408/429)" --> FAIL["Fail now"]
    B -- "5xx, 429, network error" --> C{"Attempts left<br/>and safe to repeat?"}
    C -- "No" --> FAIL
    C -- "Yes" --> D["Wait (backoff + jitter)"]
    D --> A

Only transient failures on safe requests are retried, with a wait before every new attempt.

Idempotency: is it safe to repeat?

A request is idempotent when sending it several times has the same effect as sending it once. GET, PUT and DELETE are idempotent by definition: setting a name to "Ada" twice leaves the name "Ada". A plain POST that creates an order is not: if the first attempt reached the server but the response was lost, a retry creates a second order.

Retry reads freely. For writes, either keep to idempotent methods or send an idempotency key: a unique value, often in an Idempotency-Key header, that lets the server recognize "this is the same attempt as before" and return the original result. Whether a server supports that is part of its API documentation.

Exponential backoff with jitter

If the server is overloaded and 1,000 clients all retry after exactly one second, the server gets 1,000 requests at the same moment, again. Two ideas fix that:

  • Exponential backoff: double the wait after each failure (for example 500 ms, 1 s, 2 s, 4 s), up to a cap.
  • Jitter: add randomness so clients spread out. "Full jitter" picks a random delay between 0 and the backoff value.
JS Backoff delays with full jitter

Press Run (or Ctrl + Enter) to see the console output here.

No output.

The first loop shows the ceiling doubling until the cap. The second part shows that clients choosing at random end up with different waits. (We pass a small seeded generator instead of Math.random only so the demo prints the same numbers every time.)

Rate limits and Retry-After

A server that limits how fast you may call it answers 429 Too Many Requests. It often adds a Retry-After header with either a number of seconds or a date. When you have it, obey it instead of guessing: it is the server telling you exactly when to return. A 503 can carry the same header.

JavaScript
function retryAfterMs(response) {
  const value = response.headers.get("Retry-After");
  if (value === null) {
    return null;
  }
  const seconds = Number(value);
  if (!Number.isNaN(seconds)) {
    return seconds * 1000;
  }
  return Math.max(0, new Date(value).getTime() - Date.now());
}

Cross-origin pages can only read Retry-After if the server exposes it with Access-Control-Expose-Headers, otherwise headers.get() returns null and your backoff takes over (see Headers, Auth and CORS Basics).

A reusable fetch wrapper

Now we combine everything. fetchWithRetry() accepts the fetch function, so you can pass the real fetch in production and a fake one in tests (or in this demo). It also takes a sleep function: the demo's sleep only records the wait, so the whole example finishes instantly and prints the exact schedule.

JS fetchWithRetry against a flaky server

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Read the output line by line. The flaky GET waits twice and then succeeds. The rate-limited call waits exactly 2000 ms, as the header said. The POST returns the 503 right away because repeating it is unsafe, and the 404 is returned immediately because retrying cannot change it.

Always set an upper limit on attempts and total time. Pair retries with a timeout per attempt (see Cancelling Requests and Timeouts), and show the user progress or a manual Retry button once automatic attempts are used up.

Retrying is one half of resilience. The other half is deciding what the page shows while it fails: cached data, a clear message, or a button. Good clients also give up gracefully: after several failures in a row, stop sending requests for a while instead of piling on. That idea is known as a circuit breaker, and many large systems use it.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which response should NOT be retried automatically?

    A 404 will keep returning 404. Retries are for temporary conditions: network errors, 5xx gateway or server trouble, and 429 after waiting.
  2. 2. Why add jitter to exponential backoff?

    Without jitter, clients that failed together retry together. Random delays spread the load.
  3. 3. A POST that creates an order times out. Why is blindly retrying it risky?

    POST is not idempotent. Use an idempotency key if the API supports one, or ask the user before repeating.
  4. 4. A 429 response includes Retry-After: 5. What should the client do?

    Retry-After is the server's own instruction for when to come back, so it takes priority over a guessed backoff.

Summary

  • Retry only transient failures: network errors, 408, 429 and 5xx; never client errors like 400 or 404.
  • Repeat only idempotent requests, or use an idempotency key for writes.
  • Use exponential backoff with a cap and jitter; honor Retry-After on 429 and 503.
  • Limit attempts, add per-attempt timeouts and give users a manual retry.
  • Inject fetch and sleep into your wrapper so it can be tested without waiting.

Next: Caching and Polling.