What is the best way to insert items to Azure Cosmos in bulk, provided I have multiple jobs doing this operation in parallel?

Viewed 388

There are multiple jobs that runs on demand (and some scheduled too). These Jobs have to insert data into Cosmos (via C# Cosmos SDK - SQL API). Currently we see throttling limit and 429 exceptions as at it crosses RUs given our scenario. What could be the best possible solution ?

1 Answers

There are a few things you could do:

  • Increase RUs before insert and then decrease it afterwards: Possibly the easiest (may be not the most optimal) thing to do is to increase the throughput before the jobs and decrease it after the jobs are completed. This will ensure that you always have the necessary throughput provisioned to perform the bulk operations. Please note that while increasing the throughput is instantaneous, decreasing the throughput may take some time.
  • Implement retry policies: Implement proper retry policies to retry the bulk insert operations on 429 errors. When you get 429 error, usually Cosmos DB SDK will also tell you how much time you should wait before retrying the operation. This will slow down your bulk operations but you will stay in the provisioned throughput.
  • Disable indexing before insert operation and then re-enable it: You can disable indexing before insert operation and re-enable it once the operation is complete. By doing so, you will save on the throughput that is spent on indexing the documents as they are added.

You may find this link useful for optimizing the throughput in Cosmos DB: https://docs.microsoft.com/en-us/azure/cosmos-db/optimize-cost-throughput.

Related