Modern applications rarely operate in isolation. They communicate with payment gateways, authentication providers, weather services, AI APIs, databases, and dozens of internal microservices. Unfortunately, these dependencies are not always available. Temporary network failures, rate limits, timeouts, and server errors are common in distributed systems.
If an application immediately fails whenever a downstream service experiences a transient issue, reliability suffers. Polly is a resilience library for .NET that helps applications automatically recover from temporary failures by applying policies such as retries, timeouts, circuit breakers, fallback, and rate limiting.
Rather than writing complex retry logic manually, this article explains how to build resilient HTTP clients in ASP.NET Core using Polly.
Note: Retries should only be used for transient failures. Retrying permanent failures or invalid requests can make problems worse and increase load on downstream services.
Why HTTP Resilience Matters
Without resilience policies, applications may experience:
Failed API requests
Cascading service failures
Poor user experience
Increased downtime
Resource exhaustion
Unstable microservices
Resilience patterns help applications recover automatically from temporary failures.
Common Failure Scenarios
Production systems frequently encounter:
Network interruptions
DNS resolution failures
HTTP 500 responses
HTTP 503 Service Unavailable
HTTP 429 Too Many Requests
Request timeouts
Temporary cloud outages
Slow downstream services
These failures are often short-lived and suitable for automated recovery.
Common Polly Policies
| Policy | Purpose |
|---|---|
| Retry | Retry transient failures |
| Wait and Retry | Retry with configurable delay |
| Circuit Breaker | Stop requests to failing services temporarily |
| Timeout | Cancel slow requests |
| Fallback | Return an alternative response |
| Rate Limiter | Control outgoing request volume |
Multiple policies can be combined to create a comprehensive resilience strategy.
Installing Polly
Install the required NuGet packages.
dotnet add package Microsoft.Extensions.Http.Polly
dotnet add package Polly
These packages integrate Polly with HttpClientFactory.
Registering HttpClient
Configure a typed HTTP client.
builder.Services.AddHttpClient<ProductApiClient>(
client =>
{
client.BaseAddress =
new Uri("https://api.example.com");
});
Using HttpClientFactory prevents socket exhaustion and centralizes client configuration.
Adding a Retry Policy
Configure automatic retries.
builder.Services.AddHttpClient<ProductApiClient>()
.AddPolicyHandler(
HttpPolicyExtensions
.HandleTransientHttpError()
.WaitAndRetryAsync(
3,
retryAttempt =>
TimeSpan.FromSeconds(retryAttempt)));
Transient failures are retried up to three times with increasing delays.
Request Flow with Retry
sequenceDiagram
participant Client
participant API
participant External Service
Client->>API: Request
API->>External Service: HTTP Call
External Service-->>API: HTTP 503
API->>External Service: Retry
External Service-->>API: Success
API-->>Client: Response
The client receives a successful response without knowing a retry occurred.
Adding a Circuit Breaker
Prevent repeated calls to failing services.
.AddPolicyHandler(
HttpPolicyExtensions
.HandleTransientHttpError()
.CircuitBreakerAsync(
handledEventsAllowedBeforeBreaking: 5,
durationOfBreak: TimeSpan.FromSeconds(30)));
Once the failure threshold is reached, the circuit opens and temporarily rejects further requests.
Join the conversation! Your thoughts help the community grow.