
Azure Cosmos DB Container Conflict Resolution Strategies are designed to handle concurrent updates to the same document.
Azure Cosmos DB provides two conflict resolution strategies: Last Writer Wins (LWW) and Multi-Version Concurrency Control (MVCC).
LWW is the default conflict resolution strategy, where the last write to the document wins.
MVCC, on the other hand, allows multiple versions of a document to coexist, enabling more complex conflict resolution scenarios.
Azure Cosmos DB automatically assigns a version number to each document, allowing for multiple versions to be stored.
This allows for more flexibility in handling conflicts, but also requires more storage space.
In LWW, conflicts are resolved by the system, whereas in MVCC, conflicts are resolved by the application.
Additional reading: Document Db
Last Write Wins Setup
To set up Last Write Wins, you need to enable multi-region writes in your Azure Cosmos DB account. This is done by going to the Azure portal and turning on multi-region writes.
The next step is to configure conflict resolution. You can do this by finding the Conflict Resolution tab and selecting Last Write Wins. Here, you can specify which timestamp property Azure Cosmos DB should use to determine the latest write.
You can use the _ts (timestamp) property, or if you're using the Core SQL API, you can set any numeric property instead of _ts. This will help resolve conflicts by determining the latest write.
In the event of a conflict, the record with the highest conflict resolver property value will win and be updated in all the copies of Cosmos DB. This is a key benefit of using Last Write Wins.
Here's a quick rundown of the setup process:
In Last Write Wins mode, if a conflict occurs, the record with the highest conflict resolver property value will win. This is regardless of whether the conflict involves an insert, replace, or delete operation.
Implement Last-Writer-Wins Policy
Last Write Wins is the default conflict resolution policy in Azure Cosmos DB, and it's based on a system-defined timestamp property. This policy ensures that the item with the highest value for the conflict resolution path becomes the winner in the event of a conflict.
The default conflict resolution path for Last Write Wins is the timestamp field or the _ts property. However, for API for NoSQL, you can also set it to a user-defined path with a numeric type.
To set up a container with a Last Write Wins policy, you need to enable Multi-Region Writes in your Azure Cosmos DB account. This can be done in the Azure portal by turning on multi-region writes.
You can then configure conflict resolution by finding the Conflict Resolution tab and selecting Last Write Wins. Here, you can specify which timestamp property Azure Cosmos DB should use to determine the latest write.
The conflict resolver property is set to _ts (timestamp) by default. However, if you're using Core SQL API, you can set any numeric property instead of _ts.
Here's a summary of the Last Write Wins policy:
- The item with the highest value for the conflict resolution path wins in the event of a conflict.
- The default conflict resolution path is the timestamp field or the _ts property.
- You can set a user-defined path with a numeric type for API for NoSQL.
- Conflicts resolved with this policy do not show up in the conflict feed.
Custom Policy Setup
To set up a custom conflict resolution policy, you need to create a stored procedure that will handle the conflicts. This procedure is automatically invoked when conflicts are detected under a database transaction on the server.
Related reading: Azure Devops Resolve Merge Conflicts
You can use the .NET SDK to create the stored procedure, as shown in Example 2. The procedure accepts two records, the original version and the conflicted version, and can have logic to resolve the conflicts.
The system provides exactly once guarantee for the execution of a merge procedure as part of the commitment protocol, as stated in Example 5. This means that the procedure will be executed only once, even in case of failures.
If you fail to register a merge procedure on the container or the merge procedure throws an exception at runtime, the conflicts are written to the conflicts feed, as mentioned in Example 1. Your application then needs to manually resolve the conflicts in the conflicts feed.
To set up a custom conflict resolution policy, follow these steps:
- Create a Stored Procedure:
- Deploy the Stored Procedure:
- Configure Conflict Resolution:
Note that the custom conflict resolution policy is available only for API for NoSQL accounts and can be set only at creation time, as stated in Example 1. It is not possible to set a custom resolution policy on an existing container.
Take a look at this: Azure Container App Set Environment Variable
Handling Concurrency Issues
Handling concurrency issues is a crucial aspect of working with Azure Cosmos DB containers.
Azure Cosmos DB has built-in tools to help resolve conflicts, including Last Write Wins (LWW) and Custom Conflict Resolution.
Designing for idempotency is key to handling conflicts, which means making sure your operations can be repeated without causing issues. This way, retrying operations won’t lead to unintended results.
To monitor and log conflicts, keep an eye on them and log them. This helps you understand conflict patterns and improve your resolution strategy over time.
A concurrency issue can arise when multiple requests try to update the same document simultaneously. This can lead to inconsistent data, as seen in the example of a car document with conflicting driver and color names.
To identify and measure the problem, you can use the change feed feature in Azure Functions to track changes to a document. This can help you understand the origin of the problem and measure the number of occurrences.
Here are the main ways to handle concurrency issues:
- Design for idempotency
- Use unique identifiers to track and manage conflicts
- Monitor and log conflicts to understand patterns and improve resolution strategies
- Use the change feed feature to track changes to a document and measure the number of occurrences
By implementing these strategies, you can minimize the risk of concurrency issues and ensure data consistency in your Azure Cosmos DB containers.
Sources
- https://learn.microsoft.com/en-us/azure/cosmos-db/conflict-resolution-policies
- https://learn.microsoft.com/en-us/azure/cosmos-db/nosql/how-to-manage-conflicts
- https://www.linkedin.com/pulse/handling-multi-region-write-conflicts-azure-cosmos-db-sharma-eirzf
- https://thecodeblogger.com/2019/12/16/azure-cosmos-db-understanding-conflict-resolution/
- https://medium.com/@chausse.nicolas/reduce-the-concurrency-issues-in-azure-cosmos-db-nosql-in-real-net-production-application-8b18f7da6197
Featured Images: pexels.com