Skip to main content

Retry Strategy

The retry reactive resilience strategy re-executes the same callback method if its execution fails. Failure can be either an Exception or a result object indicating unsuccessful processing.
Between retry attempts, the retry strategy waits a specified amount of time. You have fine-grained control over how to calculate the next delay.

When to Use Retry

Use the retry strategy when:
  • Dealing with transient failures that are likely to self-correct
  • Calling remote services that may experience temporary network issues
  • Accessing resources that may be temporarily unavailable
  • Working with rate-limited APIs that return 429 (Too Many Requests) responses
Don’t use retry for operations that are not idempotent (operations that can be safely repeated without side effects).

Installation

Usage

Basic Retry

Instant Retries

Advanced Configuration

Custom Delay Generator

Handling HTTP Responses

Retry Events

Configuration Options

Predicate
Defines which results and/or exceptions should trigger a retry.
int
default:"3"
The maximum number of retry attempts to use, in addition to the original call.
DelayBackoffType
default:"Constant"
The back-off algorithm type:
  • Constant: Same delay between retries
  • Linear: Delay increases linearly
  • Exponential: Delay increases exponentially
TimeSpan
default:"2 seconds"
The base delay between retry attempts. The actual delay depends on the BackoffType.
TimeSpan?
default:"null"
If provided, caps the calculated retry delay to this value.
bool
default:"false"
If true, adds randomness to retry delays:
  • For Constant and Linear: adds ±25% random variation
  • For Exponential: uses Decorrelated Jitter Backoff V2 algorithm
Func<DelayGeneratorArguments, ValueTask<TimeSpan?>>
default:"null"
Dynamically calculates the retry delay. If this returns null, the strategy uses the calculated delay from other properties.
Func<OnRetryArguments, ValueTask>
default:"null"
Invoked before the strategy delays the next attempt. Useful for logging and metrics.

Backoff Types Explained

Constant Backoff

Same delay between all retry attempts.

Linear Backoff

Delay increases by the base delay amount with each attempt.

Exponential Backoff

Delay doubles with each attempt.
Use exponential backoff with jitter for best results when dealing with external services. This prevents the “thundering herd” problem where many clients retry simultaneously.

Best Practices

Don’t retry too many times. 3-5 retries is usually sufficient for most scenarios. More retries increase latency and may overwhelm failing systems.
Always use UseJitter = true when calling external services to prevent synchronized retry storms from multiple clients.
Use MaxDelay to prevent exponential backoff from creating excessive wait times on later retry attempts.
Configure ShouldHandle to only retry temporary failures (network issues, timeouts) and not permanent failures (400 Bad Request, 401 Unauthorized).
Combine retry with circuit breaker to prevent retrying when a system is known to be down. Add timeout to prevent individual retry attempts from taking too long.

Common Patterns

Quick Retries Then Slow Retries

Retry with Maximum Duration

Examples

HTTP Client with Retry

Database Connection with Retry

The retry strategy rethrows the final exception back to the calling code. You should handle this exception appropriately.
Yes, set MaxRetryAttempts = int.MaxValue. However, this is generally not recommended. Consider using a circuit breaker or implementing a maximum time limit instead.
Use the OnRetry callback to log retry attempts. You can also enable Polly telemetry for comprehensive observability.