Skip to content
This repository was archived by the owner on Nov 17, 2023. It is now read-only.
This repository was archived by the owner on Nov 17, 2023. It is now read-only.

Add a Jitter strategy to the Retry policy #188

@CESARDELATORRE

Description

@CESARDELATORRE

We might want to add a Jitter strategy to the Retry policy.
Related to this issue I opened into Polly's repo, although it could be implemented easily, too:
App-vNext/Polly#245

Basically, a regular Retry policy can impact your system in cases of high concurrency and scalability and under high contention.
Would be great to have it Polly and not very complicated to add Jitter to the retry algorithm/poilicy.
It'd improve the overall performance to the end-to-end system by adding randomness to the exponential backoff. It'd spread out the spikes when issues arise.

The problem is explained here:
https://brooker.co.za/blog/2015/03/21/backoff.html
https://www.awsarchitectureblog.com/2015/03/backoff.html

As suggested by @reisenberger, it could already be achieved with Polly, by using one of the .WaitAndRetry(...) configuration overloads which allow you to specify a Func<..., TimeSpan> for the amount of wait. (Similar overloads exist for async.)

We could implement something like:

Random jitterer = new Random(); 
Policy
  .Handle<HttpResponseException>() // etc
  .WaitAndRetry(5,  
      retryAttempt => TimeSpan.FromSeconds(Math.Pow(2, retryAttempt))  // exponential back-off
                    + TimeSpan.FromMilliseconds(jitterer.Next(0, 100)) // plus some jitter
  );

The principle of how to do this with Polly is the same: using the Func<..., Timespan> overloads, you have complete control to adopt whatever randomness/jitter algorithm you like.
It might get improved further in future versions of Polly, according to reisenberger.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions