Exponential backoff and jitter is the standard way to retry a failed network call without hammering a service that is already struggling. Exponential backoff makes each wait longer than the last; jitter adds randomness so that thousands of clients that failed at the same moment do not all retry at the same moment too. This guide builds a small, dependency-free fetchWithRetry wrapper for Node.js 20+ and modern browsers, and explains which failures to retry, how to honour Retry-After, how to cap total time with AbortSignal, and when a retry is not safe at all.

From my own work: The retry bugs I have debugged in client projects were rarely about the formula. They were a loop that retried a
400forever, a paymentPOSTretried without an idempotency key, and a batch job where every worker retried on the same fixed one-second delay, so the API saw the original spike again every second. A random delay and a short list of retryable errors fixed all three.
Why exponential backoff and jitter work together
A transient failure, such as a dropped connection, a 503 during a deploy or a 429 from a rate limiter, usually goes away by itself. Retrying immediately mostly adds load at the worst moment. Exponential backoff grows the wait with each attempt: with a base of 250 ms the ceilings are 250 ms, 500 ms, 1 s, 2 s, 4 s and so on, up to a cap.
Backoff alone still leaves clients synchronized. If 500 clients fail at 12:00:00.000, they all retry at 12:00:00.250, then all at 12:00:00.750. Jitter breaks that pattern by picking a random delay. The AWS Architecture Blog compared several variants in simulation and found that adding jitter cut both the total work clients did and the time to finish (AWS: Exponential Backoff And Jitter). Google Cloud’s client libraries use the same idea of exponential backoff with randomness (Google Cloud: retry strategy).
| Strategy | Delay for attempt n | Behaviour |
|---|---|---|
| Fixed delay | base |
Simple, but every client retries in lockstep |
| Exponential, no jitter | min(cap, base * 2^n) |
Backs off, but clients stay synchronized |
| Full jitter | random(0, min(cap, base * 2^n)) |
Best default: spreads retries across the whole window |
| Equal jitter | half + random(0, half) where half is the exponential value / 2 |
Guarantees a minimum wait, slightly less spread |
| Decorrelated jitter | min(cap, random(base, previous * 3)) |
Grows from the previous delay; good for long-running loops |
Use full jitter unless you have a reason not to. It is one line of code and gives the widest spread.
Which errors to retry, and which never to retry
Retrying is only correct when the failure is likely to be temporary and repeating the request cannot cause harm. Split it into two questions.
Is the failure transient? Retry network errors (in fetch these reject with a TypeError), per-attempt timeouts, 408 Request Timeout, 429 Too Many Requests, 500, 502, 503 and 504. Do not retry other 4xx responses: a 400, 401, 403, 404 or 422 will fail the same way next time, and retrying only hides the bug.
Is the request safe to repeat? RFC 9110 defines GET, HEAD, OPTIONS, PUT and DELETE as idempotent: sending them twice has the same intended effect as sending them once (RFC 9110). POST and PATCH are not. A timeout on a POST /payments does not tell you whether the server charged the card. Retry those only when the API supports an idempotency key, as described below and in the idempotent API design guide.
Backoff, Retry-After and sleep helpers
Start with three small functions. Retry-After can hold either a number of seconds or an HTTP date, so the parser handles both (MDN: Retry-After). The sleep is abortable, so a cancelled request does not sit in a timer for twenty seconds.
// retry.js (ES module, Node.js 20+ or a modern browser)
export const RETRYABLE_STATUS = new Set([408, 429, 500, 502, 503, 504]);
export const IDEMPOTENT_METHODS = new Set(['GET', 'HEAD', 'OPTIONS', 'PUT', 'DELETE']);
// Full jitter: a random delay between 0 and the exponential ceiling
export function backoffDelay(attempt, { base = 250, cap = 20_000 } = {}) {
const ceiling = Math.min(cap, base * 2 ** attempt);
return Math.random() * ceiling;
}
// Retry-After is either delay-seconds ("120") or an HTTP date
export function parseRetryAfter(value, now = Date.now()) {
if (!value) return null;
const seconds = Number(value);
if (Number.isFinite(seconds)) return Math.max(0, seconds * 1000);
const at = Date.parse(value);
return Number.isNaN(at) ? null : Math.max(0, at - now);
}
// setTimeout as a promise that stops early when the signal aborts
export function sleep(ms, signal) {
return new Promise((resolve, reject) => {
if (signal?.aborted) return reject(signal.reason);
const timer = setTimeout(resolve, ms);
signal?.addEventListener('abort', () => {
clearTimeout(timer);
reject(signal.reason);
}, { once: true });
});
}
A fetchWithRetry wrapper with exponential backoff and jitter
The wrapper below keeps normal fetch semantics: it resolves with a Response (possibly a final 503) and rejects only for network errors, timeouts or cancellation. Each attempt gets its own timeout through AbortSignal.timeout(), and the whole operation has a deadline, combined with any signal the caller passes using AbortSignal.any() (MDN: AbortSignal.timeout()). AbortSignal.any() needs Node.js 20.3 or later.
// retry.js (continued)
export async function fetchWithRetry(url, options = {}, retry = {}) {
const {
retries = 4, // retries after the first attempt (5 tries total)
base = 250, // ms
cap = 20_000, // longest single wait, ms
attemptTimeout = 10_000, // per try, ms
deadline = 60_000, // whole operation, ms
retryNonIdempotent = false,
onRetry = () => {},
} = retry;
const method = (options.method || 'GET').toUpperCase();
const canRetry = IDEMPOTENT_METHODS.has(method) || retryNonIdempotent;
const total = AbortSignal.timeout(deadline);
const signal = options.signal ? AbortSignal.any([options.signal, total]) : total;
for (let attempt = 0; ; attempt++) {
let res = null;
let error = null;
try {
res = await fetch(url, {
...options,
signal: AbortSignal.any([signal, AbortSignal.timeout(attemptTimeout)]),
});
if (!RETRYABLE_STATUS.has(res.status)) return res; // success or a permanent error
} catch (err) {
if (signal.aborted) throw signal.reason; // caller cancelled or deadline passed
error = err; // network error or per-try timeout
}
if (!canRetry || attempt >= retries) {
if (res) return res;
throw error;
}
const hinted = res ? parseRetryAfter(res.headers.get('retry-after')) : null;
if (hinted !== null && hinted > cap) return res; // server wants a longer pause than we allow
const delay = hinted ?? backoffDelay(attempt, { base, cap });
onRetry({ attempt: attempt + 1, delay: Math.round(delay), status: res?.status, error });
await res?.body?.cancel(); // release the connection before waiting
await sleep(delay, signal); // rejects if the deadline passes while sleeping
}
}
A few details matter more than they look:
- Cancel the body. An unread response body keeps the connection busy.
res.body.cancel()frees it before the wait. - Honour the server first. When a
429or503carriesRetry-After, the server knows better than your formula. If it asks for longer than your cap, return the response and let the caller decide, rather than holding a request open for minutes. - Bodies must be replayable. Strings,
Buffer,Blob,FormDataandURLSearchParamscan be sent again. AReadableStreambody can be read only once, so do not retry streaming uploads with this wrapper. - Count the deadline, not just the attempts. Five attempts with ten-second timeouts and twenty-second waits could take well over a minute. The total signal puts a hard ceiling on that.
Using it looks like ordinary fetch:
import { fetchWithRetry } from './retry.js';
const res = await fetchWithRetry(
'https://api.example.com/v1/orders/42',
{ headers: { accept: 'application/json' } },
{
retries: 5,
onRetry: ({ attempt, delay, status, error }) =>
console.warn(`retry #${attempt} in ${delay} ms`, status ?? error?.name),
},
);
if (!res.ok) throw new Error(`orders API failed: ${res.status}`);
const order = await res.json();
Retrying POST requests safely with an idempotency key
For a non-idempotent call, generate one key per logical operation, send it on every attempt, and let the server return the stored result when it sees a key it has already processed. Many payment and messaging APIs accept an Idempotency-Key header for exactly this. The key must be created outside the retry loop; a new key per attempt defeats the purpose.
const idempotencyKey = crypto.randomUUID(); // once per order, not per attempt
const res = await fetchWithRetry('https://api.example.com/v1/orders', {
method: 'POST',
headers: {
'content-type': 'application/json',
'idempotency-key': idempotencyKey,
},
body: JSON.stringify({ sku: 'TSHIRT-M', qty: 2 }),
}, { retryNonIdempotent: true, retries: 3 });
If the API you call has no idempotency support, do not retry the POST automatically. Record the failure, then check the remote state (for example, look the order up by your own reference) before trying again.
A generic retry helper with decorrelated jitter
Not everything is fetch. Database reconnects, queue publishes and SDK calls benefit from the same pattern. This helper takes any async function and a shouldRetry predicate, and uses decorrelated jitter, which grows from the previous delay instead of the attempt number:
import { sleep } from './retry.js';
export async function retry(fn, {
retries = 5, base = 100, cap = 5_000, shouldRetry = () => true, signal,
} = {}) {
let delay = base;
for (let attempt = 0; ; attempt++) {
try {
return await fn(attempt);
} catch (err) {
if (attempt >= retries || !shouldRetry(err) || signal?.aborted) throw err;
delay = Math.min(cap, base + Math.random() * (delay * 3 - base)); // random(base, delay * 3)
await sleep(delay, signal);
}
}
}
// Example: retry only connection-level errors from a database driver
const rows = await retry(() => pool.query('SELECT 1'), {
shouldRetry: (err) => ['ECONNRESET', 'ECONNREFUSED', 'ETIMEDOUT'].includes(err.code),
});
Retry budgets, circuit breakers and layered retries
Retries multiply. If a browser retries three times, your API gateway retries three times, and your service retries its database three times, one user action can become dozens of database calls during an outage. Three rules keep that under control:
- Retry at one layer. Usually the layer closest to the failure, or the outermost caller, but not both.
- Use a retry budget. Allow retries only while they are a small share of total traffic in a sliding window; when the share is exceeded, fail fast. That turns a full outage into fast errors instead of a retry storm.
- Add a circuit breaker for hard-down dependencies. After a run of failures, stop calling for a cool-down period and return a fallback, then let a single probe request test recovery.
On the server side, send Retry-After with your 429 and 503 responses so well-behaved clients know how long to wait. The API rate limiting guide covers the limiter side, and the WordPress maintenance mode guide shows a 503 with Retry-After in Nginx.
Test the retry logic against a flaky local server
Do not wait for a production outage to find out whether the loop works. This tiny Node.js server fails two out of every three requests with a 503 and Retry-After: 1:
// flaky-server.mjs
import http from 'node:http';
let count = 0;
http.createServer((req, res) => {
count++;
if (count % 3 !== 0) {
res.writeHead(503, { 'retry-after': '1', 'content-type': 'text/plain' });
return res.end('busy');
}
res.writeHead(200, { 'content-type': 'application/json' });
res.end(JSON.stringify({ ok: true, count }));
}).listen(8080, () => console.log('flaky server on http://localhost:8080'));
// try-it.mjs
import { fetchWithRetry } from './retry.js';
const started = Date.now();
const res = await fetchWithRetry('http://localhost:8080/', {}, {
onRetry: (info) => console.log('retrying', info),
});
console.log(res.status, await res.json(), `${Date.now() - started} ms`);
node flaky-server.mjs &
node try-it.mjs
# retrying { attempt: 1, delay: 1000, status: 503, error: null }
# retrying { attempt: 2, delay: 1000, status: 503, error: null }
# 200 { ok: true, count: 3 } ~2000 ms
Remove the retry-after header to watch the jittered delays instead, and stop the server to see network errors retried and finally thrown. In unit tests, inject Math.random and the clock (or use your test runner’s fake timers) so delays are deterministic.
Retry checklist
- Only transient failures retried: network errors, timeouts,
408,429,500,502,503,504. - Non-idempotent methods retried only with an idempotency key created once per operation.
- Full jitter (or decorrelated jitter) with a cap; never a fixed delay across many clients.
Retry-Afterhonoured, with a ceiling on how long you will wait.- A per-attempt timeout and a total deadline, both cancellable.
- Retries at one layer only, with logging of every retry and a budget or circuit breaker for hard outages.
Want this done on your own server or site?
I am Saizul Amin, a web developer and technical SEO consultant. I set up and harden VPS servers, migrate WordPress sites, fix indexing problems and build automations. See my portfolio or get in touch.
FAQ
What is exponential backoff with jitter?
It is a retry strategy where the maximum wait doubles after each failed attempt, up to a cap, and the actual wait is a random value within that window. The randomness stops many clients from retrying at the same instant.
Which jitter strategy should I use?
Full jitter, a random delay between zero and the exponential ceiling, is a good default for HTTP clients. Decorrelated jitter suits long-running loops such as reconnecting to a database.
How many times should I retry a request?
For interactive requests, two to four retries inside a total deadline of a few seconds is usually enough. Background jobs can retry longer, but should still have a maximum and send the job to a dead-letter queue when it is reached.
Should I retry a 429 Too Many Requests response?
Yes, but wait as long as the Retry-After header says. If there is no header, back off with jitter, and reduce your request rate rather than only retrying.
Is it safe to retry a POST request?
Only if the API supports idempotency keys and you send the same key on every attempt. Otherwise a timed-out POST may already have succeeded, and retrying could create a duplicate order or payment.
Sources
- AWS Architecture Blog: Exponential Backoff And Jitter
- Google Cloud Storage Docs: Retry strategy
- MDN Web Docs: Retry-After
- MDN Web Docs: AbortSignal.timeout()
- RFC 9110: HTTP Semantics (idempotent methods)

Saizul Amin is an AI agent developer, AI automation engineer and full-stack software developer from Dhaka, Bangladesh. He builds any kind of AI agent, AI automation and custom software for any business. Portfolio: https://saizul.com

