Polly is a .NET resilience and transient-fault-handling library that allows developers to express policies such as Retry, Circuit Breaker, Timeout, Bulkhead Iso
Polly is a .NET resilience and transient-fault-handling library that allows developers to express policies such as Retry, Circuit Breaker, Timeout, Bulkhead Iso
[!IMPORTANT] Polly now participates in the Open Source Maintenance Fee (OSMF). Starting November 16, 2026, companies that earn at least US $20,000 from a product or project that uses Polly will be asked to pay a US $20/month maintenance fee to help fund Polly's ongoing upkeep. The source code stays free and open, and individuals, hobbyists, and organizations below the threshold owe nothing. Read the announcement: Introducing the Open Source Maintenance Fee for Polly · Learn about the OSMF · Become a sponsor
Polly is a .NET resilience and transient-fault-handling library that allows developers to express resilience strategies such as Retry, Circuit Breaker, Hedging, Timeout, Rate Limiter and Fallback in a fluent and thread-safe manner.
We are a member of the .NET Foundation!
Keep up to date with new feature announcements, tips & tricks, and other news through www.thepollyproject.org
[!IMPORTANT] This documentation describes the new Polly v8 API. If you are using the v7 API, please refer to the previous version of the documentation.
| Package | Latest Version | About |
|---|---|---|
Polly.Core |
The core abstractions and built-in strategies. | |
Polly.Extensions |
Telemetry and dependency injection support. | |
Polly.RateLimiting |
Integration with System.Threading.RateLimiting APIs. |
|
Polly.Testing |
Testing support for Polly libraries. | |
Polly |
This package contains the legacy API exposed by versions of the Polly library before version 8. |
This README aims to give a quick overview of some Polly features - including enough to get you started with any resilience strategy. For deeper detail on any resilience strategy, and many other aspects of Polly, be sure also to check out pollydocs.org.
To use Polly, you must provide a callback and execute it using resilience pipeline. A resilience pipeline is a combination of one or more resilience strategies such as retry, timeout, and rate limiter. Polly uses builders to integrate these strategies into a pipeline.
To get started, first add the Polly.Core package to your project by running the following command:
dotnet add package Polly.CoreYou can create a ResiliencePipeline using the ResiliencePipelineBuilder class as shown below:
// Create an instance of builder that exposes various extensions for adding resilience strategies
ResiliencePipeline pipeline = new ResiliencePipelineBuilder()
.AddRetry(new RetryStrategyOptions()) // Add retry using the default options
.AddTimeout(TimeSpan.FromSeconds(10)) // Add 10 seconds timeout
.Build(); // Builds the resilience pipeline
// Execute the pipeline asynchronously
await pipeline.ExecuteAsync(static async token => { /* Your custom logic goes here */ }, cancellationToken);If you prefer to define resilience pipelines using IServiceCollection, you'll need to install the Polly.Extensions package:
dotnet add package Polly.ExtensionsYou can then define your resilience pipeline using the AddResiliencePipeline(...) extension method as shown:
…Polly provides a variety of resilience strategies. Alongside the comprehensive guides for each strategy, the wiki also includes an overview of the role each strategy plays in resilience engineering.
Polly categorizes resilience strategies into two main groups:
These strategies handle specific exceptions that are thrown, or results that are returned, by the callbacks executed through the strategy.
| Strategy | Premise | AKA | Mitigation |
|---|---|---|---|
| Retry family | Many faults are transient and may self-correct after a short delay. | Maybe it's just a blip | Allows configuring automatic retries. |
| Circuit-breaker family | When a system is seriously struggling, failing fast is better than making users/callers wait. Protecting a faulting system from overload can help it recover. |
Stop doing it if it hurts Give that system a break |
Breaks the circuit (blocks executions) for a period, when faults exceed some pre-configured threshold. |
| Fallback | Things will still fail - plan what you will do when that happens. | Degrade gracefully | Defines an alternative value to be returned (or action to be executed) on failure. |
| Hedging | Things can be slow sometimes, plan what you will do when that happens. | Hedge your bets | Executes parallel actions when things are slow and waits for the fastest one. |
Unlike reactive strategies, proactive strategies do not focus on handling errors, but the callbacks might throw or return. They can make proactive decisions to cancel or reject the execution of callbacks.
| Strategy | Premise | AKA | Prevention |
|---|---|---|---|
| Timeout | Beyond a certain wait, a success result is unlikely. | Don't wait forever | Guarantees the caller won't have to wait beyond the timeout. |
| Rate Limiter | Limiting the rate a system handles requests is another way to control load. This can apply to the way your system accepts incoming calls, and/or to the way you call downstream services. |
Slow down a bit, will you? | Constrains executions to not exceed a certain rate. |
Visit resilience strategies docs to explore how to configure individual resilience strategies in more detail.
…If all retries fail, a retry strategy rethrows the final exception back to the calling code.
For more details, visit the retry strategy documentation.
…For more details, visit the circuit breaker strategy documentation.
…For more details, visit the fallback strategy documentation.
…If all hedged attempts fail, the hedging strategy will either re-throw the original exception or return the original failed result to the caller.
For more details, visit the hedging strategy documentation.
The timeout resilience strategy assumes delegates you execute support co-operative cancellation. You must use Execute/Async(...) overloads taking a CancellationToken, and the executed delegate must honor that CancellationToken.
…Timeout strategies throw TimeoutRejectedException when a timeout occurs.
For more details, visit the timeout strategy documentation.
…Rate limiter strategy throws RateLimiterRejectedException if execution is rejected.
For more details, visit the rate limiter strategy documentation.
Starting with version 8.3.0, Polly has integrated Simmy, a chaos engineering library, directly into its core. For more information, please refer to the dedicated chaos engineering documentation.
To learn more about Polly, visit pollydocs.org.
Thanks to the following companies for sponsoring the ongoing development of Polly.
Polly participates in the Open Source Maintenance Fee (OSMF). Starting November 16, 2026, companies that earn at least US $20,000 from a product or project that uses Polly are asked to pay a US $20/month maintenance fee, regardless of how many of their projects use Polly. This helps fund the ongoing work of keeping the project secure, current, and well maintained.
The source code remains free and open under its existing license. Individuals, hobbyists, students, non-profits, and organizations below the revenue threshold owe nothing. For full details, see the announcement post and the OSMF FAQ.
Help support this project by becoming a sponsor through GitHub Sponsors.
Licensed under the terms of the New BSD License
No open issues yet, or sync has not completed.