Search code examples
azure-cosmosdbazure-cosmosdb-sqlapicosmos

CosmosDB 429 appears in azure portal but c# code against same collection/database not raise 429 exception


At specific given point in time, if we refer azure portal metrics option against our cosmos db account it shows 429 happened many times but c# code written (for same cosmos db account/database/collection) using cosmos sql sdk, not throwing 429 even for single time because of that RUs scale up logic written for the every occurrence of 429/throttle issue is not getting triggered at all. Confusion is whether throttle happened like azure portal shows or it is projecting wrong data. Which one to consider as true, portal data or c# code behavior? If portal is correct why c# code not raising the same issue? any suggestions please.

exception catch logic:

 catch (DocumentClientException ex)
                {
                    if (ex.StatusCode == (HttpStatusCode)429)
                    {
                        //RU scale up logic
                    }
                }

Solution

  • The SDK will retry automatically upon receiving a 429 up to a number of times defined in the RetryOptions.MaxRetryAttemptsOnThrottledRequests.

    If you don't want the SDK to retry, you can set this value to 0, and any 429s will get thrown to your user code:

    ConnectionPolicy connectionPolicy = new ConnectionPolicy();
    connectionPolicy.RetryOptions.MaxRetryAttemptsOnThrottledRequests = 0;
    DocumentClient client = new DocumentClient(new Uri("service endpoint"), "auth key", connectionPolicy);