Does CosmosDb SDK v3 automatically retry while inserting in bulk?

Viewed 2266

I'm new to CosmosDB and I'm trying to grasp the R/U's settings limits.

Situation

Within an ASP.NET Core 2.1 application, I want to insert +/- 3000 documents at once. Looping these items and adding them one by one takes minutes of time. So bulk might be the way to go. I've followed some resources like:

Bulk import data to Azure Cosmos DB SQL API account by using the .NET SDK on docs.microsoft.com

Introducing Bulk support in the .NET SDK

In my code I used the sample code from the blog.

List<Task> concurrentTasks = new List<Task>();
foreach (var entity in entities)
{
    entity.Id = GenerateId(entity);

    concurrentTasks.Add(Container.CreateItemAsync(entity, new PartitionKey(entity.RoleId)));
}

await Task.WhenAll(concurrentTasks);

Inserting one document costs about 6 R/U's from my local development machine into Azure.

When I provision the default 400 R/U's a second, I quickly get 429 Too Many Requests exceptions. When I switch to Autoscale, it's finished within about 20 seconds without exceptions.

My question is: what if I want to limit the R/U's and still using this concurrentTasks approach, will there be retry handling done by the SDK? Or do I need to write my own 429-retry.

1 Answers

Bulk in V3 does apply retries on 429 (you can verify this by taking a look at the Diagnostics property in any of the operations).

The amount of retries is governed by CosmosClientOptions.MaxRetryAttemptsOnRateLimitedRequests (default 9). The fact that you are getting the error it means the SDK already retried 9 times. You can increase this value and keep the SDK retrying (which will take longer).

The fact that enabling Autoscale helped, means the load of data you want to push in is too high for the provisioned throughput (400 RU as you mention). Autoscale will detect the throttling and increase the provisioned throughput to acomodate the load.

Related