Retry with Exponential Backoff
Automatically retry failed operations using exponential backoff with jitter. Essential for handling transient failures in network requests, RPC calls, and other unreliable operations common in Ethereum applications.Overview
The retry utilities implement exponential backoff with jitter:- Exponential backoff: Delay increases exponentially after each failure
- Jitter: Adds randomness to prevent thundering herd
- Configurable: Control max retries, delays, and retry conditions
- Type-safe: Full TypeScript support with generics
Basic Usage
Simple Retry
Retry an RPC call with default settings:- maxRetries: 3
- initialDelay: 1000ms
- factor: 2 (doubles each time)
- maxDelay: 30000ms
- jitter: true
Custom Configuration
Configure retry behavior:Retry Options
RetryOptions
Conditional Retry
Only retry specific errors:Retry Callbacks
Monitor retry attempts:Exponential Backoff Algorithm
The retry delay is calculated as:Example Timeline
Configuration:{ initialDelay: 1000, factor: 2, maxDelay: 10000, jitter: false }
| Attempt | Delay Calculation | Delay (ms) |
|---|---|---|
| 1st retry | 1000 * 2^0 | 1000 |
| 2nd retry | 1000 * 2^1 | 2000 |
| 3rd retry | 1000 * 2^2 | 4000 |
| 4th retry | 1000 * 2^3 | 8000 |
| 5th retry | min(1000 * 2^4, 10000) | 10000 (capped) |
Advanced Usage
Wrapping Functions
Create reusable retry-wrapped functions:Custom Retry Logic
Implement custom retry conditions:Aggressive Retry
For critical operations, use aggressive retry:Integration with HttpProvider
The HttpProvider already has basic retry built-in, but you can wrap it for more control:Common Patterns
Retry with Maximum Total Time
Limit total retry duration:Retry Different Operations
Different retry strategies for different operations:Combine with Timeout
Retry with per-attempt timeout:Best Practices
Choose Appropriate Delays
- Fast operations (balance, blockNumber): 500-1000ms initial
- Medium operations (calls, receipts): 1000-2000ms initial
- Slow operations (logs, traces): 2000-5000ms initial
Set Reasonable Max Retries
- Public RPC: 3-5 retries (may have rate limits)
- Private RPC: 5-10 retries (more reliable)
- Critical operations: 10+ retries
Use Jitter
Always enable jitter (default) to prevent thundering herd problems when many clients retry simultaneously.Monitor Retries
UseonRetry callback for observability:
API Reference
retryWithBackoff
fn: Async function to retryoptions: Retry configuration
withRetry
fn: Function to wrap with retry logicoptions: Retry configuration
See Also
- Timeout utilities - Add timeouts to retries
- Polling - Poll operations with backoff
- Rate Limiting - Rate limit retry attempts

