1

I'm trying to configure NServiceBus to implement an exponential backoff scheme for delayed retries. For example:

1) on the first failure retry in 2^0 minutes

2) on the second failure retry in 2^1 minutes

3) on the third failure retry in 2^2 minutes

I found an issue on github that seems to indicate that exponential backoff is possible with a Custom Recoverability Policy, but I am not able to determine from Particular's documentation how to achieve that.

Would anyone be able to point me in the right direction for setting up a Custom Recoverability Policy for NServiceBus that would enable exponential backoff for delayed retries?

1 Answers1

1

Edited to incorporate answer from @sabacc's comment

From my research, NServiceBus does not support exponential backoff out of the box. However, the Custom Recoverability policy allows you to configure the number of immediate and delayed retries as well as the time between delayed retries.

Since NServiceBus includes the number of delayed deliveries performed within the ErrorContext object as DelayedDeliveriesPerformed, we can incorporate that into the delay calculation on each delay iteration.

I've provided the code below for the case of a Semi-Transient exception (InternalServerException) that ought to have one immediate and 3 delayed retries in an exponential backoff fashion.

private RecoverabilityAction RetryPolicy(RecoverabilityConfig config, ErrorContext context)
{
    var delay = Math.Pow(10, context.DelayedDeliveriesPerformed);

    var semiTransientConfiguration = new RecoverabilityConfig(
             new ImmediateConfig(1), 
             new DelayedConfig(3, TimeSpan.FromSeconds(delay)),
             config.Failed);

    if (context.Exception is InternalServerException)
    {
        var action = DefaultRecoverabilityPolicy.Invoke(
                     semiTransientConfiguration, context);
    }

    return action;
}
  • 1
    A custom recoverability policy should allow you to implement an exponential backoff strategy. However, you have to pass the `RecoverabilityAction.DelayedRetry` method the calculated timespan. You can base the timespan based on the number of previous retries which you can access on `ErrorContext` passed to your custom recoverability policy. – Sabacc Jan 27 '20 at 09:59
  • @Sabacc This worked, thank you! If you'd like to expand your comment into an answer I can give you credit for the answer. Otherwise, I'll incorporate your comment into my answer above. – LearningThings Jan 27 '20 at 16:20
  • 1
    feel free to incorporate this into your answer, I don't have a good code example at hand to share at the moment. If you got it working already, that's perfect to share :) – Sabacc Jan 28 '20 at 07:39
  • @sabacc Thanks! Feel free to edit the answer further for clarity. – LearningThings Jan 28 '20 at 16:32