Skip to content

Commit c0e80d5

Browse files
committed
updated retry policy
1 parent c75ef3c commit c0e80d5

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

src/Microsoft.Health.Fhir.CosmosDb/Features/Storage/RetryExceptionPolicyFactory.cs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ public class RetryExceptionPolicyFactory
3030
private readonly AsyncPolicy _bundleActionRetryPolicy;
3131
private readonly AsyncPolicy _backgroundJobRetryPolicy;
3232

33+
private const int _exponentialBackoffBaseDelayMs = 100;
34+
private const int _exponentialMaxJitterMs = 300;
35+
private const int _exponentialMaxDelayMs = 60 * 1000;
36+
3337
public RetryExceptionPolicyFactory(CosmosDataStoreConfiguration configuration, RequestContextAccessor<IFhirRequestContext> requestContextAccessor, ILogger<RetryExceptionPolicyFactory> logger)
3438
{
3539
_requestContextAccessor = EnsureArg.IsNotNull(requestContextAccessor, nameof(requestContextAccessor));
@@ -42,7 +46,7 @@ public RetryExceptionPolicyFactory(CosmosDataStoreConfiguration configuration, R
4246
? CreateExtendedRetryPolicy(configuration.IndividualBatchActionRetryOptions.MaxNumberOfRetries / configuration.RetryOptions.MaxNumberOfRetries, configuration.IndividualBatchActionRetryOptions.MaxWaitTimeInSeconds)
4347
: Policy.NoOpAsync();
4448

45-
_backgroundJobRetryPolicy = CreateExtendedRetryPolicy(100, -1, true);
49+
_backgroundJobRetryPolicy = CreateExtendedRetryPolicy(30, -1, true);
4650
}
4751

4852
public AsyncPolicy RetryPolicy
@@ -77,11 +81,14 @@ TimeSpan SleepDurationProvider(int retryAttempt, Exception exception)
7781
return cosmosException.RetryAfter.Value;
7882
}
7983

84+
// Exponential backoff is used for background jobs. Given current values, exponential backoff is used for the first 10 retries. After that a fixed wait time of_exponentialMaxDelayMs (60 seconds) is used.
85+
// Jitter is multiplied by the retry attempt to increase the randomness of the retry interval for longer retry delays (especially retry > 10).
8086
if (useExponentialRetry)
8187
{
82-
// Exponential backoff with jitter
83-
var backoff = Math.Pow(2, retryAttempt) * 100; // Exponential backoff in milliseconds
84-
var jitter = RandomNumberGenerator.GetInt32(0, 300); // Add jitter in milliseconds
88+
// Calculate exponential backoff with a cap of 60 seconds
89+
var backoff = Math.Min(Math.Pow(2, retryAttempt) * _exponentialBackoffBaseDelayMs, _exponentialMaxDelayMs);
90+
91+
var jitter = RandomNumberGenerator.GetInt32(0, _exponentialMaxJitterMs) * retryAttempt; // Add jitter in milliseconds
8592
return TimeSpan.FromMilliseconds(backoff + jitter);
8693
}
8794

0 commit comments

Comments
 (0)