Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When configuring RetryOnFailure what is the maxRetryDelay parameter

I'm using Entity Framework Core 2.2 and i decided to follow some blog sugestion and enable retry on failure:

services.AddDbContext<MyDbContext>( options =>
    options.UseSqlServer(Configurations["ConnectionString"]),
    sqlServerOptionsAction: sqlOptions =>
    {
        sqlOptions.EnableRetryOnFailure(
        maxRetryCount: 10,
        maxRetryDelay: TimeSpan.FromSeconds(5),
        errorNumbersToAdd: null);
    });

My question is what is the maxRetryDelay argument for? I would expect it to be the delay time between retries, but the name implies its the maximum time, does that mean i can do my 10 retries 1 second apart and not 5 seconds apart as i desire?

like image 363
RagnaRock Avatar asked Apr 24 '19 09:04

RagnaRock


1 Answers

The delay between retries is randomized up to the value specified by maxRetryDelay.

This is done to avoid multiple retries occuring at the same time and overwhelming a server. Imagine for example 10K requests to a web service failing due to a network issue and retrying at the same time after 15 seconds. The database server would get a sudden wave of 15K queries.

By randomizing the delay, retries are spread across time and client.

The delay for each retry is calculated by ExecutionStragegy.GetNextDelay. The source shows it's a random exponential backoff.

The default SqlServerRetryingExecutionStrategy uses that implementation. A custom retry strategy could use a different implementation

like image 180
Panagiotis Kanavos Avatar answered Oct 19 '22 12:10

Panagiotis Kanavos