Introduction
For years, Polly was the go-to library for building resilient .NET applications. Retry policies, circuit breakers, timeouts—everything lived in Polly.
Starting with .NET 8, Microsoft introduced built-in Resilience Pipelines, marking a major shift in how fault tolerance is implemented in ASP.NET Core.
This article explores:
What resilience pipelines are
How they differ from Polly
How to build retry, timeout, and circuit breaker pipelines
Real-world API examples
Best practices for production systems
Why Resilience Matters in Distributed Systems
In modern systems:
APIs depend on external services
Networks fail
Latency spikes
Downstream services become unstable
Without resilience:
One failure cascades
Threads block
APIs crash under load
Resilience pipelines solve this inside the framework, not via third-party middleware.
What Are Resilience Pipelines?
A Resilience Pipeline is a composable execution flow that applies fault-handling strategies such as:
Retry
Timeout
Circuit Breaker
Hedging
Rate Limiting
They are:
Built on
System.Threading.RateLimitingFully async
Dependency-injection friendly
Designed for cloud-native workloads
Adding Resilience Support
Required Package
dotnet add package Microsoft.Extensions.Resilience
Configuring a Resilience Pipeline
Step 1: Register Pipelines in Program.cs
using Microsoft.Extensions.Resilience;
using Polly.Timeout;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddResiliencePipeline("external-api", pipeline =>
{
pipeline
.AddTimeout(TimeSpan.FromSeconds(3))
.AddRetry(new RetryStrategyOptions
{
MaxRetryAttempts = 3,
Delay = TimeSpan.FromMilliseconds(500),
BackoffType = DelayBackoffType.Exponential
})
.AddCircuitBreaker(new CircuitBreakerStrategyOptions
{
FailureRatio = 0.5,
SamplingDuration = TimeSpan.FromSeconds(30),
MinimumThroughput = 10,
BreakDuration = TimeSpan.FromSeconds(15)
});
});
builder.Services.AddHttpClient<WeatherClient>()
.AddResilienceHandler("external-api");
var app = builder.Build();
app.MapControllers();
app.Run();
What’s Happening Here?
| Strategy | Purpose |
|---|---|
| Timeout | Prevents thread starvation |
| Retry | Handles transient failures |
| Circuit Breaker | Stops cascading failures |
Join the conversation! Your thoughts help the community grow.