DEV Community

Cover image for Building Resilient .NET 10 Applications with Polly
Ali Shahriari (MasterPars)
Ali Shahriari (MasterPars)

Posted on

Building Resilient .NET 10 Applications with Polly

In modern distributed systems and microservices, failure is inevitable. Building resilient applications that can gracefully handle failures and recover quickly is critical. In .NET 10, Polly provides a powerful way to implement resilience patterns like Retry, Circuit Breaker, Timeout, and Fallback.


🚧 Why Resilience Matters

  • Network instability
  • Timeouts from external APIs
  • Temporary unavailability of services
  • Slow downstream services

Without resilience strategies, your application may crash or degrade badly under such conditions.


🛠 Introduction to Polly

Polly is a .NET resilience and transient-fault-handling library that allows developers to express policies such as:

  • Retry
  • Circuit Breaker
  • Timeout
  • Fallback
  • Bulkhead Isolation

It works seamlessly with HttpClientFactory and the new features in .NET 10.


🔁 Retry Policy

Retries a failed operation a specified number of times before throwing an exception.

var retryPolicy = Policy
    .Handle<HttpRequestException>()
    .RetryAsync(3);
Enter fullscreen mode Exit fullscreen mode

⚡ Circuit Breaker Policy

Prevents your system from overwhelming failing services. Opens the circuit after a number of failures.

var circuitBreakerPolicy = Policy
    .Handle<HttpRequestException>()
    .CircuitBreakerAsync(
        handledEventsAllowedBeforeBreaking: 5,
        durationOfBreak: TimeSpan.FromSeconds(30));
Enter fullscreen mode Exit fullscreen mode

⏳ Timeout Policy

Cancels requests that take longer than a specified time.

var timeoutPolicy = Policy
    .TimeoutAsync<HttpResponseMessage>(5); // seconds
Enter fullscreen mode Exit fullscreen mode

🧰 Fallback Policy

Provides a default value or alternative logic when a request fails.

var fallbackPolicy = Policy<HttpResponseMessage>
    .Handle<Exception>()
    .FallbackAsync(new HttpResponseMessage(HttpStatusCode.OK)
    {
        Content = new StringContent("Fallback response")
    });
Enter fullscreen mode Exit fullscreen mode

🧪 Combining Policies

You can chain multiple policies for more robust control.

var policyWrap = Policy.WrapAsync(retryPolicy, circuitBreakerPolicy, timeoutPolicy);
Enter fullscreen mode Exit fullscreen mode

🌐 Using Polly with HttpClientFactory in .NET 10

builder.Services.AddHttpClient("ResilientClient")
    .AddPolicyHandler(policyWrap);
Enter fullscreen mode Exit fullscreen mode

📌 Best Practices

  • Use policy logging for observability
  • Avoid retrying non-transient faults
  • Tune thresholds based on your system’s behavior
  • Combine policies wisely to avoid over-complexity

✅ Conclusion

Resilience is not a luxury—it's a necessity in distributed applications. With Pollyand .NET 10, you can build applications that are robust, responsive, and reliable even in the face of failure. Start small, test often, and tune gradually to create truly resilient systems.

Top comments (0)