Skip to content

Contact sales

By filling out this form and clicking submit, you acknowledge our privacy policy.
  • Labs icon Lab
  • A Cloud Guru
Azure icon

Implement a Conflict Resolution Policy in Cosmos DB for NoSQL

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.

Azure icon

Path Info

Clock icon Beginner
Clock icon 45m
Clock icon Dec 16, 2022

Contact sales

By filling out this form and clicking submit, you acknowledge our privacy policy.

Table of Contents

  1. Challenge


    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.

  2. Challenge

    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.

    1. 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.
    2. As you author the code, you should be able to save and build the solution without error after each section of code is written.
    3. 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": [

    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.

The Cloud Content team comprises subject matter experts hyper focused on services offered by the leading cloud vendors (AWS, GCP, and Azure), as well as cloud-related technologies such as Linux and DevOps. The team is thrilled to share their knowledge to help you build modern tech solutions from the ground up, secure and optimize your environments, and so much more!

What's a lab?

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.

Provided environment for hands-on practice

We will provide the credentials and environment necessary for you to practice right within your browser.

Guided walkthrough

Follow along with the author’s guided walkthrough and build something new in your provided environment!

Did you know?

On average, you retain 75% more of your learning if you get time for practice.

Start learning by doing today

View Plans