Make fetch resilient
@vereda/http
Make fetch resilient.
Retries, backoff, timeouts, per-host isolation and circuit breaking for fetch on Node.js, Bun and Cloudflare Workers, with typed results instead of thrown errors. Zero runtime dependencies.
npm install @vereda/httpEvery PR runs the suite on Node 22, Node 24 and Bun, and a smoke test in workerd. Support policy →
The loop you'd write by hand
fetch makes one attempt. Everything after that is yours to write, and the comments are the questions it leaves open.
Even once it's correct, the loop still has:
- no limit on how many retries pile onto a struggling host,
- no way to cancel,
- errors you can only tell apart by parsing a message.
Vereda is that loop written carefully, once.
async function getUser(id: string) { for (let attempt = 0; ; attempt++) { try { const res = await fetch(`https://api.example.com/users/${id}`, { signal: AbortSignal.timeout(5_000), }); if (res.ok) return await res.json(); throw new Error(`HTTP ${res.status}`); // 404 or 503? retry both? } catch (err) { // timeout, DNS failure, or the throw above? caught all the same // is this request safe to repeat? what if it were a POST? if (attempt === 3) throw err; } await new Promise((r) => setTimeout(r, 200 * 2 ** attempt)); // jitter? Retry-After? }}One call, every safeguard
A dropped connection, a timeout or a 503 is retried up to three times, with jittered exponential backoff, before your code sees an error.
attemptMs bounds each attempt. An optional totalMs deadline bounds the whole call.
The result is a value, not an exception: result.success or a typed result.error.kindyou can switch on.
import { HttpClient } from "@vereda/http";
const api = HttpClient.create({ baseUrl: "https://api.example.com", timeout: { attemptMs: 5_000 },});
const result = await api.get("/users/42").toPromise(); // never rejects
if (result.success) { const user = await result.raw.json();} else { console.error(result.error.kind, result.error.message); // a typed error}Patterns, animated
Each simulator runs the same traffic with and without the pattern. A test checks every model against the real client.
- TimeoutsPer-attempt timeout or total deadline? What
attemptMsandtotalMseach protect you from. - Backoff and jitterWhy clients that retry on the same schedule come back together, and how full jitter spreads them out.
- Retry budgetHow plain retries keep a slow host down after it recovers, and how a budget caps the extra load.
- Circuit breakerWhat happens to callers when a host goes down, with and without a breaker that fails fast.
- BulkheadA slow service fills the shared global limit and blocks a fast one. A partition limit for the slow one keeps the fast one working.
- Rate limiterA burst of 100 requests hits an API that allows 20 per second and answers 429. A token bucket spreads the burst out instead.
