ExponentialBackoff
Retry policy with exponential delay growth and an upper delay bound.
ExponentialBackoff increases delay over time, reducing pressure on unstable upstream services.
Import
import { ExponentialBackoff } from "@zap-studio/retry/exponential-backoff";Configuration
const policy = new ExponentialBackoff({
maxAttempts: 6,
baseDelayMs: 100,
maxDelayMs: 5_000,
});Delay Model
Delay grows exponentially and is capped:
- attempt 1 ->
baseDelayMs - attempt 2 ->
baseDelayMs * 2 - attempt 3 ->
baseDelayMs * 4 - ...
- capped at
maxDelayMs
When to Use
Use ExponentialBackoff when:
- retries target shared/networked services
- transient failures are common
- aggressive immediate retries could amplify outages
Example
import { $fetch } from "@zap-studio/fetch";
const policy = new ExponentialBackoff({
maxAttempts: 5,
baseDelayMs: 100,
maxDelayMs: 2_000,
});
const user = await policy.run(async () => {
const response = await $fetch("https://api.example.com/users/1", {
throwOnFetchError: true,
});
return await response.json();
});Edit on GitHub
Last updated on