Retrying Transient Errors

By default RestClient doesn’t retry transient errors, however it is recomended that you configure the RetryHandler in your pipeline to retry transient errors.

The RetryHandler will retry a request if a transient fault occurs this could be DNS / host timeout etc or any status code > 500 e.g. Internal Server Error or Gateway Timeout. Both exponential and linear retry strategies are supported.

You configure the RetryHandler in your pipeline using the UseRetryHandler() extension method:

var config = new Config()
   .UseRetryHandler
   (
     maxRetries:4, 
     waitToRetryInSeconds:2, 
     maxWaitToRetryInSeconds: 10, 
     backOffStrategy: RetryHandler.BackOffStrategy.Exponential
   );

dynamic client = new RestClient("http://jsonplaceholder.typicode.com", config);

maxRetries How many times to retry the request if a transient fault occurs (this includes the first request).

waitToRetryInSeconds Sets the amount of seconds to wait between requests. If your backOffStrategy is linear the RetryHandler will just wait and retry after this time. If your backOffStrategy is Exponential then the RetryHandler will back off exponentially on each retry - in this example that would be 2, 4, 8… seconds.

maxWaitToRetryInSeconds The maximum amount of seconds to wait before retrying. In our example the 4th retry should wait for 16 seconds, however because maxWaitToRetryInSeconds is set to 10 it will wait 10 seconds.

backOffStrategy Can be RetryHandler.BackOffStrategy.Exponential or RetryHandler.BackOffStrategy.Linear

You can also use the UseRetryHandler() extension method with no parameters

var config = new Config().UseRetryHandler();

dynamic client = new RestClient("http://jsonplaceholder.typicode.com", config);

This sets a Exponential BackOffStrategy with maxRetries set to 3, waitToRetryInSeconds set to 1.44 and maxWaitToRetryInSeconds set to 10. This is optimum and safe for most use cases including Azure.

RetryHandler should work well for most use cases, if you have more complex requirements write your own Handler using a library such as Polly. Feel free to copy how I catch transient exceptions.