Building Resilient Applications with Retry Pattern

Maicon Carlos Pereira
DHL Supply Chain Brazil
3 min readApr 15, 2019
(portuguese version)

In the previous article, we mentioned that some of the possible failures in our application are transient failures. These faults are automatically fixed after a short time.

The Retry pattern (or policy) makes our application resilient by automatically retrying on failure.

The animation below shows the working principle of Retry. In this scenario we configure the mechanism to make up to 5 retries in case of failure (if you prefer a gif, click here):

Retry Pattern Video

An important point in this example is that by making our application resilient, the end user did not know that there were any failures in communication. He made a request and had his result. With this, we can say that we have improved the user experience.

Some other possible settings for this pattern are:

  • RetryForever: make attempts (eternally) until you succeed;
  • Retry (N): make N attempts, and only after N attempts the fault is presented; and
  • WaitAndRetry (N, T): will make up to maximum N attempts and wait for T time between each attempts (the animation case).

Retry Exponential (Backoff)

In the Wait and Retry configuration, one of the usual ways of setting the intervals between retries is called Exponential Retry, in which the interval grows exponentially.

If you’re a Gmail® user, for example, you may have noticed this. See the (accelerated) animation below:

Plotting the intervals between the attempts evidences that this is an exponential distribution:

Implementing the Retry Pattern

If you are a developer you should already be wondering how to implement this pattern using some structures like conditionals, loop, try-catch …

You don’t need rack your brains, because there are frameworks ready that will make our life easier, like Polly Framework for .NET applications.

Polly is a library that implements a set of resiliency and fault tolerance patterns for .Net and it’s part of the .Net Foundation. Other important features of this library are the fluent language, reusability, thread safe, and Async support. Very good!!!

Implementing Retry with Polly

Here’s how simple it is:

Analyzing:

  • Policy indicates that we will initiate a new resilience policy;
  • The Handle function indicates that the policy will only be activated if the SomeException exception is raised;
  • The WaitAndRetry function indicates the used policy, and argument 5 indicates that it will make up to 5 attempts with an exponential interval (Math.Pow…); and
  • We put in the function Execute the code that will be executed by the policy, be it a procedure (void) or function.

Note that Polly made our retry policy explicit and readable, without the need to interpret conditionals and loops, etc. 👏

The Polly library allows other ways to build Retry’s policy. The previous code is one of the simplest and sufficient ways for many cases.

I hope you enjoyed it, and do not forget to leave your feedbacks.

In the next article we’ll talk about the Circuit Breaker pattern. Follow me! ;)

--

--

Maicon Carlos Pereira
DHL Supply Chain Brazil

Graduado em Ciência da Computação e MBA em Gerenciamento de Projetos, Scrum Master Professional. Com mais de 15 anos no mundo de desenvolvimento de software