I would like to understand how best to implement a retry/backoff stratergy for cosmos db (documentdb). I understand that there is some default retry mechanics built into the sdk which I can change in the connectionpolicy like so:
RetryOptions = new RetryOptions() { MaxRetryAttemptsOnThrottledRequests = 3, MaxRetryWaitTimeInSeconds = 60 }
However, I am not sure how that impacts how I should do exception management.
Currently I am doing the following:
GetAsync<T>(Uri, Id) {
try {
ResourceResponse<Document> response = await client.ReadDocumentAsync(URiFactory.CreateDocumentUri(uri), new RequestOptions { PartitionKey = new PartitonKey(convert.ToInt64(id)) }).ConfigureAwait(false);
}
catch(DocumentClientException ex) {
if(ex.StatusCode == (HttpStatusCode)TooManyRequests) {
await Task.Run(async () =>
{
await Task.Delay(ex.RetryAfter);
return await GetAsync<T>(Uri, Id).ConfigureAwait(false);
}
}
}
}
Do I need to do this retry? and if I catch the exception does that stop the default retry attempts? Also, what are the default retry attempts catching? i.e. is it just 429? if so would I need to handle the error code 449 manually?
Custom RetryOptions
is only used to handle throttles (429 error code). Refer https://docs.microsoft.com/en-us/azure/cosmos-db/performance-tips#429 for more details.
On exception part: API will only bail after all retries exhausted with exception.
By default, the DocumentClientException with status code 429 is returned after a cumulative wait time of 30 seconds if the request continues to operate above the request rate. This occurs even when the current retry count is less than the max retry count, be it the default of 9 or a user-defined value.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With