My previous blog post gave a very short description of Polly and that we were going to use it at work. The Polly library expose a set of options how to make your software more resilient. The most common (my guess) is a simple retry functionality, retry calling e.g. the database x number of times (x can also be indefinitely). Other options are timeouts, cache functionality, fallbacks and a couple more. Our demand, however, didn’t exactly fit into the retry policy of Polly. We wanted to try x number of times (e.g. five times with two seconds of sleep between tries), then sleep for some time (e.g. 30 or 60 seconds) and then try again five times. Repeat until success. Also, we only wanted to log first time there is an error, and then when the try was successful (after retries, not if successful on first attempt). using System; using Microsoft.Extensions.Logging; using Polly; using Polly.Retry; namespace RetrySample {
Implementing retry logic (e.g. when calling another module with a REST API) can be a bit of a mess. You start at one place in your code and everything looks nice, then after a while a need for configuration develops, and then retry-logic is needed somewhere else. On top of that, software is developed in different teams. At my work we are examining Polly ("Polly is a .NET resilience and transient-fault-handling library that allows developers to express policies such as Retry, Circuit Breaker, Timeout, Bulkhead Isolation, and Fallback in a fluent and thread-safe manner.") to see if that framework can help us. So far, I have just made simple tests and it looks promising. Stay tuned.