partial update on cosmos db

Yang Chow Mun 121 Reputation points
2024-04-07T11:13:39.5933333+00:00

Hi all,

I have a few data sources which going to write into a single cosmos db container based on the id but the the update will be on different fields.

Example. the document json will be like

{id: xxxx, car_number:xxx, car_model:xxx, car_color:xxx, datetime:xxx} (please ignore the syntax error)

My data source A will have id, car_number.

My data source B will have id, car_model.

My data source C will have id, car_color.

In a particular time all the data sources are sending data for the same id which mean concurrent write is happening.

Is there any special handling is required when using the partial document update method in cosmos db to handle concurrent write or it will be handled internally by cosmos in the backend?

Thanks in advance!

Azure Cosmos DB
Azure Cosmos DB
An Azure NoSQL database service for app development.
1,639 questions
0 comments No comments
{count} votes

Accepted answer
  1. Vahid Ghafarpour 21,635 Reputation points
    2024-04-07T19:31:53.6966667+00:00

    Thanks for posting your question in the Microsoft Q&A forum.

    In Cosmos DB, concurrent writes to the same document can be handled using optimistic concurrency control (OCC). This means that when you perform a partial document update, Cosmos DB checks the document's _etag value to ensure that no other write operation has modified the document since it was read. If the _etag value does not match, indicating that the document has been modified, Cosmos DB will return a 412 (Precondition Failed) status code, and you can handle the conflict accordingly.

    https://learn.microsoft.com/en-us/azure/cosmos-db/nosql/database-transactions-optimistic-concurrency

    ** Please don't forget to close up the thread here by upvoting and accept it as an answer if it is helpful **


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.