question

teshayat avatar image
0 Votes"
teshayat asked ·

Cosmos DB Throttled Requests

Hi there,

I have a few thousands request every 30 secs. I have autopilot mode on with 4000 RU/s.
But I can see I'm getting lot of throttling requests. I'm currently storing all the insert functions into a task and
then using Task.WhenAll.


 // Cosmos Tasks Execution
 await Task.WhenAll(concurrentTasks);

8946-screenshot-1.jpg

The bulk insert would have been a batter choice for me, but I couldn't find any.
Is there any way where I can wait a couple of seconds so that I don't get 429 errors.

I have added a custom throttling function in the builder but seems to be not working.

 configurationBuilder.AddCustomHandlers(new ThrottlingHandler());

Help would be appreciated. Thanks








azure-cosmos-db
screenshot-1.jpg (31.4 KiB)
· 1
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@HasinEshayat-7121 Were you able to try this. If this helped you, please accept the reply as an answer. If you need further help , please get back to us.

Thanks
Navtej S

0 Votes 0 ·

1 Answer

MarkBrownMSFT avatar image
0 Votes"
MarkBrownMSFT answered ·

If the instance of your Cosmos client only does these bulk inserts then create your client using the AllowBulkExecution setting below.

 new CosmosClient(endpoint, authKey, new CosmosClientOptions() { AllowBulkExecution = true});

Would be helpful to see the code for your custom handler to better understand why that is not working. But generally speaking you should trap for 429 responses and implement a backoff/retry policy with Polly with a one second interval. However, with bulk mode enabled you should see far fewer of these anyway.

Thanks.


· 3 ·
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thanks.

I have the AllowBulkExecution to true. I'm adding throttling handler like this :

 configurationBuilder.WithRequestTimeout(new TimeSpan(0,0,30)); 
 configurationBuilder.WithThrottlingRetryOptions(new TimeSpan(0, 0, 30), 20);
 configurationBuilder.AddCustomHandlers(new ThrottlingHandler());

Custom throttling function with Polly :

ThrottlingHandler

 public override Task<ResponseMessage> SendAsync(
             RequestMessage request,
             CancellationToken cancellationToken)
         {
             return Policy
                 .HandleResult<ResponseMessage>(r => (int) r.StatusCode == 429)
                 .RetryAsync(3)
                 .ExecuteAsync(() => base.SendAsync(request, cancellationToken));
         }

Thanks let me know, where I'm missing.


0 Votes 0 ·

Try removing the WithRequestTimeout and WithThrottlingRetryOptions.

Why retry just three times? Try using .RetryForeverAsync() in your override and amortize them over a longer period of time until they are all completed.

0 Votes 0 ·

Thanks, mark.

That helped.

1 Vote 1 ·