Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retry Policy in Cosmos DB

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?

like image 746
Pectus Excavatum Avatar asked Oct 17 '17 14:10

Pectus Excavatum


1 Answers

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.

like image 93
Kiran Kolli Avatar answered Sep 22 '22 11:09

Kiran Kolli