Image by Yung Chang from Unsplash

Retry & Circuit Breaker Patterns in C# with Polly

Jordan Lee

--

Retry and circuit-breaker patterns are the 2 most common approaches when coding for resiliency.

There’s a ton of other articles already written that go into the nitty gritty details of each approach, so I’ll just reference some of them at the end of this article. But to establish some context for this article, here’s a quick overview:

Retry: If something goes wrong, try repeating the same operation again x number of times before giving up.

Generally, this approach is used when you have a flaky dependency which you have no control over. Examples of this might be your service calling a third-party API.

Circuit-breaker: If something goes wrong, hit the panic button that prevents any further attempts to repeat the operation.

This is typically used when you have an extremely unreliable dependency. In this case, we want to stop calling it altogether, as additional attempts to call it might worsen the situation. An example of this might be an overloaded database.

I decided to write this article because I keep coming across .NET code where both approaches have been written manually — i.e. using loops for retries and locks for circuit breakers (or variations of these).

--

--