Retry Strategy
The retry reactive resilience strategy re-executes the same callback method if its execution fails. Failure can be either anException 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
Installation
Usage
Basic Retry
Instant Retries
Advanced Configuration
Custom Delay Generator
Handling HTTP Responses
Retry Events
Configuration Options
Defines which results and/or exceptions should trigger a retry.
The maximum number of retry attempts to use, in addition to the original call.
The back-off algorithm type:
Constant: Same delay between retriesLinear: Delay increases linearlyExponential: Delay increases exponentially
The base delay between retry attempts. The actual delay depends on the
BackoffType.If provided, caps the calculated retry delay to this value.
If
true, adds randomness to retry delays:- For
ConstantandLinear: adds ±25% random variation - For
Exponential: uses Decorrelated Jitter Backoff V2 algorithm
Dynamically calculates the retry delay. If this returns
null, the strategy uses the calculated delay from other properties.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.Best Practices
Use appropriate retry counts
Use appropriate retry counts
Don’t retry too many times. 3-5 retries is usually sufficient for most scenarios. More retries increase latency and may overwhelm failing systems.
Enable jitter for external services
Enable jitter for external services
Always use
UseJitter = true when calling external services to prevent synchronized retry storms from multiple clients.Set maximum delays
Set maximum delays
Use
MaxDelay to prevent exponential backoff from creating excessive wait times on later retry attempts.Only retry transient failures
Only retry transient failures
Configure
ShouldHandle to only retry temporary failures (network issues, timeouts) and not permanent failures (400 Bad Request, 401 Unauthorized).Combine with other strategies
Combine with other strategies
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
What happens when all retries are exhausted?
What happens when all retries are exhausted?
The retry strategy rethrows the final exception back to the calling code. You should handle this exception appropriately.
Can I retry indefinitely?
Can I retry indefinitely?
Yes, set
MaxRetryAttempts = int.MaxValue. However, this is generally not recommended. Consider using a circuit breaker or implementing a maximum time limit instead.How do I see retry attempts in logs?
How do I see retry attempts in logs?
Use the
OnRetry callback to log retry attempts. You can also enable Polly telemetry for comprehensive observability.