Implement a Conflict Resolution Policy in Cosmos DB for NoSQL

45 minutes
  • 2 Learning Objectives

About this Hands-on Lab

When you choose to geo-replicate Cosmos DB data across multiple regions and enable multi-region write capabilities, you also open up the possibility of write conflicts on the same item coming from different regions. By default, Cosmos DB for NoSQL applies a Last Writer Wins policy using a built-in time stamp. However, you do have the option of defining your own conflict resolution policy when you create a new container. In this hands-on lab, you get a chance to practice doing just that using the Azure Cosmos DB SDK for .Net C#.

Students with solid experience coding in .Net C# — and/or experience with the Cosmos DB for NoSQL SDK for any language — will be the most prepared to complete this lab without assistance. However, tips are available in the solution videos and the lab guide including the full solutions.

Learning Objectives

Successfully complete this lab by achieving the following learning objectives:

Housekeeping
  1. Open an incognito or in-private window and log in to the Azure portal using the username and password provided in the lab environment.
  2. From within the portal, initiate the Cloud Shell to select Bash (versus PowerShell) and set up with new backing storage.
  3. From the Bash command prompt, execute the git clone command using the URL provided in the Additional Information and Resources section of the lab, followed by DP420Labs to alias the downloaded folder to a friendly name.
  4. Once the project is downloaded, navigate to the DP420Labs/DP420/ConflictResolution folder, and use Cloud Editor to open the Program.cs file.
  5. From the Bash command prompt, change to the working directory DP420Labs/DP420/ConflictResolution.

NOTE: You are free to write the code for this lab in Visual Studio Code or another IDE if you have experience in that environment. Just make sure you download the project file to ensure you have the right library references and using directives. Be aware that the lab guide and the solution video are based on working in the Cloud Shell Editor, but it won’t substantially change the code you write.

Create a Conflict Resolution Policy on a New Cosmos DB Container
You are Here

At the end of the housekeeping tasks, you should have Cloud Shell open with the Program.cs file open in the Editor. Be sure the namespace near the top of the file is namespace ConflictResolution. This confirms that you are in the right code base. Also check that your command prompt is at DP420Labs/DP420/ConflictResolution.

The Context

By default, Cosmos DB for NoSQL applies a Last Writer Wins policy using the _ts built-in timestamp on each JSON document. This is sufficient for some applications. However, in several of your team’s gaming applications, conflicting updates should be first based on the number of reputation points a user has earned, which will trump any simultaneous changes by other users. You have a custom field that adds time to the last-update timestamp based on the user’s reputation points. This custom property is called myAltTimeStamp.

The Task

For your conflict resolution policy, you want to apply the same Last Writer Wins approach as the default policy, but based on myAltTimeStamp instead of _ts. You don’t have to worry about coding the logic that populates myAltTimeStamp. For this exercise, you will just assume it is accurately storing the data as intended.

  1. Starting from the Azure portal, retrieve the primary connection string.
  2. Back in Cloud Shell, paste the connection string in the Program.cs file.
  3. The remainder of the skeletal code in the Program.cs file has comments that you should use to guide each line of code required to build the solution.

    NOTE: A couple of lines of code are provided for you, and the comments contain some clues regarding the classes and methods you might need to employ. There are a few more clues you can glean from the test results, followed by the Hints and Tips section.

  4. The name of the container that you create and the partition key for that container should be exactly as stated in the comments in Program.cs.
  5. As you author the code, you should be able to save and build the solution without error after each section of code is written.
  6. When you are done, save, build, and run the code.
The Test

To quickly test if the container was created and that it includes your conflict resolution policy, go back to the Cosmos DB account and generate the ARM template. Scroll through the ARM template JSON to find the definition for the LabItems container. Confirm the partition key is set to labPK. The block of JSON will look something like this:

                    "partitionKey": {
                        "paths": [
                            "/labPK"
                        ],

Then, nearby is another block of code that will show the conflict resolution policy, which should look like the code below, with myAltTimeStamp as the resolution path:

                    "conflictResolutionPolicy": {
                        "mode": "LastWriterWins",
                        "conflictResolutionPath": "myAltTimeStamp"
                    }

Hints and Tips

  • The command to build the project is dotnet build and the command to run the code is dotnet run.
  • There are three possible conflict resolution objects you might include in your container properties. You only need two of them:
    ConflictResolutionPolicy.Mode, and/or ConflictResolutionPolicy.ResolutionPath and/or ConflictResolutionPolicy.ResolutionProcedure.
  • There are two possible conflict resolution modes: ConflictResolutionMode.LastWriterWins or ConflictResolutionMode.Custom
  • Similar to the partition key path, the conflict resolution path value should include a forward slash before the property name.
  • The full solution, plus an alternative is in the ProgramSolution.txt file, within the same folder as the Program.cs file.
  • Your solution may or may not look like the sample solutions. As long as your code produces the same container with the same conflict resolution policy configuration, then you have successfully completed the lab objective.

Additional Resources

Suppose your team is ready to make the leap to global distribution of your Cosmos DB for NoSQL that backs a gaming platform. In order to provide better performance for your users, you also intend to enable multi-region write capability. Given that your games are often highly collaborative, the team members are keenly aware of the possibility of write conflict on the same item coming from different regions, so you've decided to investigate by configuring your own conflict resolution policy. The lab environment is already provisioned with an Azure Cosmos for NoSQL account and a database, so you're ready to try out adding a new database container with a custom conflict resolution policy.

This is the URL for the code repository referenced in the Housekeeping objective: https://github.com/linuxacademy/DP-420-Designing-and-Implementing-Cloud-Native-Applications-Using-Microsoft-Azure-Cosmos-DB

What are Hands-on Labs

Hands-on Labs are real environments created by industry experts to help you learn. These environments help you gain knowledge and experience, practice without compromising your system, test without risk, destroy without fear, and let you learn from your mistakes. Hands-on Labs: practice your skills before delivering in the real world.

Sign In
Welcome Back!

Psst…this one if you’ve been moved to ACG!

Get Started
Who’s going to be learning?