A c# library to retry code using a truncated binary exponential backoff algorithm
To make more robust applications make sure to retry any external call, whether it's a database call or a web request. The truncated binary exponential backoff algorithm is used to avoid congestion and is truncated/capped by default to keep the maximum interval within reason
You can retry either an Action
or a Func<T>
Retry.Action(() => { _databaseRepository.Update(databaseObject); });
return Retry.Function(() => { return _databaseRepository.Get(id); });
The following default settings are used and can be changed by using the following code with different values
Retry.UpdateSettings(new RetrySettings(
jitterSettings: new JitterSettings(percentage: 25),
maximumNumberOfAttempts: 5,
millisecondsPerSlot: 32,
truncateNumberOfSlots: true,
maximumNumberOfSlotsWhenTruncated: 16));
Validation of settings always takes place during construction of the object so it fails as early as possible.
If you wish to be able to inject it - for example for having different settings in different places - you can use the RetryInstance
class:
IRetryInstance instance = new RetryInstance(new RetrySettings(
jitterSettings:new JitterSettings(percentage: 25),
maximumNumberOfAttempts: 5,
millisecondsPerSlot: 32,
truncateNumberOfSlots: true,
maximumNumberOfSlotsWhenTruncated: 16));
This interface and class only has the Action and Function methods without the settings parameter besides the Action
or Func<T>
, because you provide those during construction.
instance.Action(() => { _databaseRepository.Update(databaseObject); });
return instance.Function(() => { return _databaseRepository.Get(id); });
First, install NuGet. Then, install RetryMagic from the package manager console:
PM> Install-Package RetryMagic
RetryMagic is Copyright © 2015 Jorrit Salverda and other contributors under the MIT license.