Increasing Fault Tolerance with Retry Policies

Lets say your application calls a REST API searching for an entity that has not yet been created due to a race condition. Or maybe that same API intermittently returns non-success status codes like gateway timeouts or internal server errors. If your application depends on that API to finish whatever task it trying to complete then you are out of luck.

We have all been there. It creates a bad user experience for your customers and, because of the dependency on that system, is totally out of your control. One thing we can do to help make our code more fault tolerant when it comes to issues like this is using Retry Policies.

Controlling What You Can

Obviously we cannot control external services our code may rely on so lets focus on what we can control. Using Retry Policies can help us get more granular control of our code and how we handle failures and exceptions.

The Implementation

So let’s say we make a call to that third party API and that call fails but if we call it again we know it may work so we write some retry logic to do just that:

public void DoWork()
{
    var retryCount = 0;
    while (retryCount < 3)
    {
        try
        {
            // call unreliable service here
        }
        catch
        {
            retryCount++;
            if (retryCount == 3)
            {
                throw;
            }
        }
    }
}

So the code above says “try to make this API call three times before throwing the exception”, pretty basic. Now you can write a reusable method that takes in a delegate to make this retry policy reusable but why recreate the wheel when we can use something like Polly?

Make Your Code Tougher Using 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. 

http://www.thepollyproject.org/
The ED patients don’t need to spend lots of time, money and efforts to buy these medications. http://pamelaannschoolofdance.com/wp-content/uploads/2016/08/2016-2017-Fall-Schedule-8-23-16-Basic.pdf cheapest viagra

Polly is an awesome library that has out of the box Retry Policies you can implement directly into your code with tons of cool customization options. Polly is maintained by the developers over at App vNext and you can pull down into your code via Nuget and is now part of .NET Core 3.1.

Let’s look at creating that same Retry Policy we wrote above using Polly:

private Policy retryPolicy => Policy.Handle<Exception>().Retry(3);

Now lets implement it:

private Policy retryPolicy => Policy.Handle<Exception>().Retry(3);

public void DoWork()
{
    retryPolicy.Execute(CallUnreliableCode);
}

private void CallUnreliableCode()
{
    // call unreliable service here
}

Now we have a reusable retryPolicy in our class that does everything we wrote above only shorter and cleaner. We can make the policy even more specific by telling it to only perform the retry if a specific type of exception is thrown like an HttpRequestException:

private Policy retryPolicy => Policy.Handle<HttpRequestException>().Retry(3);

The policy will now only retry running that block of code if the exception thrown is a type of HttpRequestException, otherwise it will just throw the exception on the first failure.

But Polly gives us a lot more than the basic Retry policies above:

So what should I take away here?

  • Using retry policies can help improve the fault tolerance of your code, especially when dealing with external services you.
  • Using libraries like Polly you can implement retry policies quickly and easily.
  • Using other policies such as Circuit-Breaker and Bulkhead Isolation can give you more granular control of your code and help improve your user experience.

Leave a Reply

Your email address will not be published. Required fields are marked *