0
votes

In my scenario i'm processing changes in a Cosmos Db Collection with Azure Functions using a CosmosDbTrigger.

The source Cosmos Db Collection is partitioned by Device Id. Outputs are persisted in another Cosmos Db Collection. In some cases i have to correlate multiple documents from the same device and output one single document.

So i'm wondering if i have to apply some sort of transactional logic in order to prevent race conditions when i receive 2 documents who should merge into a single one at the same time by multiple instances of my Azure Function? Or can i rely on getting a batch of changes per partition on the same function instance where i can process and correlate all the related documents in a loop?

The number of changes per partition will never exceed the default 100 items for a single batch.

1

1 Answers

3
votes

Each partition key will only be processed by a single instance at a time. Before starting any processing, each instance will acquire a lease for a given partition. That's what Leases collection is used for.

See this doc in Change Feed Processor Library: https://azure.microsoft.com/nl-nl/blog/introducing-the-azure-cosmosdb-change-feed-processor-library/

However, as far as I understand your scenario, there is no guarantee that you will get all the related changes in one single batch, even if the amount of changes is below 100. If related changes are in different batches, they might get processed by different instances in the unlucky case of switching the lease holder at that exact moment.