Configure delayed retries

Component: NServiceBus
NuGet Package NServiceBus (6.x)
Starting from NServiceBus version 6, the delayed retries policy (formerly known as second level retries policy) has been deprecated in favor of the new custom recoverability policy which allows more control over the recoverability behavior. This documentation shows how the previous delayed retries policies can be implemented with the new recoverability policy.
Delayed retries cannot be used when transport transactions are disabled or delayed delivery is not available. For more information about transport transactions, refer to transport transactions. For more details on the caveats, see the delayed delivery article.

Configuring delayed retries

  • TimeIncrease: Specified as a TimeSpan. Specifies the delay interval for each retry attempt. This delay increases by the same timespan with each delayed delivery. For example, if the specified value is ten seconds, i.e. 00:00:10, then the first delayed retry will be at ten seconds, the subsequent delayed retry will be twenty seconds, and so on.
  • NumberOfRetries: Number of times delayed retries are performed. Default: 3.
var recoverability = endpointConfiguration.Recoverability();
recoverability.Delayed(
    delayed =>
    {
        delayed.NumberOfRetries(2);
        delayed.TimeIncrease(TimeSpan.FromMinutes(5));
    });

Disabling through code

var recoverability = endpointConfiguration.Recoverability();
recoverability.Delayed(
    delayed =>
    {
        delayed.NumberOfRetries(0);
    });

Custom retry policy

Custom retry logic can be configured via code.

var recoverability = endpointConfiguration.Recoverability();
recoverability.CustomPolicy(MyCustomRetryPolicy);

Simple policy

The following retry policy overrides the default ten second delay interval and sets it to five seconds.

RecoverabilityAction MyCustomRetryPolicy(RecoverabilityConfig config, ErrorContext context)
{
    var action = DefaultRecoverabilityPolicy.Invoke(config, context);
    if (!(action is DelayedRetry delayedRetryAction))
    {
        return action;
    }
    // Override default delivery delay.
    return RecoverabilityAction.DelayedRetry(TimeSpan.FromSeconds(5));
}

Exception-based policy

Sometimes the number of retries or the delay interval might depend on the error exception thrown. The following retry policy extends the previous one by skipping delayed retries whenever MyBusinessException has been thrown during incoming message processing.

RecoverabilityAction MyCustomRetryPolicy(RecoverabilityConfig config, ErrorContext context)
{
    var action = DefaultRecoverabilityPolicy.Invoke(config, context);

    if (!(action is DelayedRetry delayedRetryAction))
    {
        return action;
    }
    if (context.Exception is MyBusinessException)
    {
        return RecoverabilityAction.MoveToError(config.Failed.ErrorQueue);
    }
    // Override default delivery delay.
    return RecoverabilityAction.DelayedRetry(TimeSpan.FromSeconds(5));
}

Legacy .Retries message receiver

In versions 5 and below, delayed retries use the [endpoint_name].Retries queue for persistent storage of messages to be retried. To prevent message loss when upgrading to version 6, a dedicated .Retries queue receiver is started if not explicitly disabled. It serves the purpose of forwarding messages from the .Retries queue to the endpoint's main queue to be retried appropriately.

The receiver is needed only during the upgrade from versions 5 and below and is not needed for new endpoints using version 6. For details on the upgrade process and how to safely disable the receiver, refer to the version 6 upgrade guide.

Letting the receiver continue to run might have negative performance implications depending on the transport being used. For example, for endpoints using either SQL Server or MSMQ as its transport, an endpoint will periodically poll the .Retries queue to check for messages.

The .Retries receiver can be disabled via code using:

var recoverability = endpointConfiguration.Recoverability();
recoverability.DisableLegacyRetriesSatellite();
This .Retries message receiver and the API to disable it have been removed in version 7.

Samples


Last modified