Error Retry Boundary
Retry transient failures with backoff, give up after a budget, and render a retry-able error boundary instead of a blank page when the budget is spent.
On this page
Introduction
This guide covers error retry with a boundary, a recurring concern in the data-fetching scenario. A network blip should not blank the page, and an endpoint that is down should not retry forever. Retrying transient failures with backoff, capping the attempts, and rendering an error boundary with a retry button when the budget is spent gives the user a path forward instead of a dead end.
Classify the error
Treat 5xx and network errors as transient and 4xx as fatal, so you retry only what might succeed and do not waste budget on a 404.
The trade-off worth naming is that treat 5xx and network errors as transient and 4xx as fatal, so you retry only what might succeed and do not waste budget on a 404 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 isTransient(err) {
if (err.code === 'ECONNRESET') return true;
if (err.status >= 500 && err.status < 600) return true;
return false;
}
Backoff with jitter
Wait an exponentially growing delay with a random jitter so a retry storm does not synchronize and hammer the server again on the same slot.
The trade-off worth naming is that wait an exponentially growing delay with a random jitter so a retry storm does not synchronize and hammer the server again on the same slot 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) {
for (let i = 0; i < tries; i++) {
try { return await fn(); }
catch (e) { if (!isTransient(e) || i === tries - 1) throw e; }
await new Promise(r => setTimeout(r, 200 * 2 ** i * Math.random()));
}
}
Cap the budget
Give up after a small number of tries so a down endpoint does not retry forever, and hand off to the boundary so the user can retry manually.
The trade-off worth naming is that give up after a small number of tries so a down endpoint does not retry forever, and hand off to the boundary so the user can retry manually 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.
Retry-able boundary
Render a boundary with a retry button that resets the error state, so the user is not stuck on a blank page when the budget is spent.
The trade-off worth naming is that render a boundary with a retry button that resets the error state, so the user is not stuck on a blank page when the budget is spent 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 retries
Retry only idempotent calls, or make the write idempotent with a request id, so a retry that lands twice does not create two orders, which is the subtle danger.
The trade-off worth naming is that retry only idempotent calls, or make the write idempotent with a request id, so a retry that lands twice does not create two orders, which is the subtle danger 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 boundary shows a retry button after the budget is spent.
The trade-off worth naming is that assert a 503 retries up to the budget and a 404 fails immediately, and that the boundary shows a retry button after the budget is spent 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 patterns, React error boundaries.
FAQ
- Which status codes should retry?
- 5xx and network errors are transient; 4xx are not, because retrying a 404 or 401 will not make it succeed, and retrying them wastes budget.
- Why add jitter to backoff?
- So a retry storm from many clients does not synchronize on the same backoff slot and hammer the server again, which is the thundering herd.
Report a problem with this articleOpens a pre-filled issue on GitHub.