Skip to main content
This page is a placeholder. All examples on this page are currently AI-generated and are not correct. This documentation will be completed in the future with accurate, tested examples.

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:
Default configuration:
  • 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:
With jitter enabled (default), the delay is randomized:

Example Timeline

Configuration: { initialDelay: 1000, factor: 2, maxDelay: 10000, jitter: false }

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

Use onRetry callback for observability:

API Reference

retryWithBackoff

Retry an async operation with exponential backoff. Parameters:
  • fn: Async function to retry
  • options: Retry configuration
Returns: Promise resolving to operation result Throws: Last error if all retries exhausted

withRetry

Create a retry wrapper for a function. Parameters:
  • fn: Function to wrap with retry logic
  • options: Retry configuration
Returns: Wrapped function with automatic retry

See Also