409 Conflict error using multi-write regions

João Pereira 0 Reputation points
2025-03-14T03:41:15.1633333+00:00

Hello,

I'm currently trying to understand how to implement conflict resolution in my project.

I'm trying to create conflicts to test my stored procedures but I keep receiving the error 409 Conflict with the following message: "Entity with the specified id already exists in the system.".

I'm inserting two items with the same ID on different regions on purpose, trying to create a conflict, but when I fetch all the conflicts it says that there are no conflicts in the system.

My question is why do I get the conflict error but the system does not store the conflicts for the stored procedures to solve? I'm using the Nodejs SDK in case that's relevant.

Azure Cosmos DB
Azure Cosmos DB
An Azure NoSQL database service for app development.
1,903 questions
{count} votes

4 answers

Sort by: Most helpful
  1. Vinodh247 34,661 Reputation points MVP Volunteer Moderator
    2025-03-14T03:47:37.8+00:00

    Hi ,

    Thanks for reaching out to Microsoft Q&A.

    It sounds like you are running into cosmos db's behavior in multi-write regions, but there are a few key points to consider:

    Why are you getting a 409 conflict but not seeing conflicts stored?

    1. Default "last writer wins" mode
      • In a multi-region write-enabled cosmos db setup, the default conflict resolution strategy is last writer wins (LWW). This means that when a conflict occurs, the system automatically resolves it based on the defined conflict resolution policy.
      • If you haven't explicitly set a custom conflict resolution strategy (custom stored procedure based resolution), conflicts will be automatically resolved without being recorded in the conflict feed.
      1. The conflict feed is empty
        • The conflict feed is only populated if you set custom conflict resolution using stored procedures.
          • If your container is using the default lww strategy, conflicts will never appear in the conflict feed because cosmos db resolves them automatically.
          1. 409 conflict vs. Conflict feed
            • A 409 conflict error occurs at the sdk level when you try to insert a document with an already existing id.
              • However, this is not the same as a conflict that is eligible for conflict resolution. If two regions try to insert different versions of the same document at the same time, and there’s a conflict, cosmos db handles it based on your conflict resolution policy.
    • If you're getting a 409 Conflict, it's due to an attempt to insert an existing ID, which is an SDK level constraint.
    • Cosmos DB doesn't store conflicts unless conflict resolution is explicitly set to "Custom".
    • If you want stored procedures to resolve conflicts, you must configure a custom conflict resolution policy and query the conflict feed. 

    Please feel free to click the 'Upvote' (Thumbs-up) button and 'Accept as Answer'. This helps the community by allowing others with similar queries to easily find the solution.


  2. Sai Raghunadh M 4,640 Reputation points Microsoft External Staff Moderator
    2025-03-17T03:50:25.48+00:00

    Hi @ João Pereira

    If Promise.all() performs simultaneous writes to the same region, it may not replicate the multi-region update scenario needed to trigger conflicts.

    This is because simultaneous writes in the same region aren't considered conflicts; they are resolved through the unique ID constraint

    Please try the below Suggestions:

    • Verify that the container’s conflict resolution policy is set to "custom" and not the default LWW mode.
    • Use the readConflicts() method in the SDK to fetch conflicts and debug the process.
    • When testing, make sure writes occur in different regions. You can specify the target region in your SDK configuration to explicitly direct traffic.

  3. PratikLad 1,830 Reputation points Microsoft External Staff Moderator
    2025-03-18T10:22:59.3366667+00:00

    Hi @João Pereira,

    My question is why do I get the conflict error, but the system does not store the conflicts for the stored procedures to solve?

    When Do Conflicts Appear in the Conflict Feed?

    As per MS Document Conflicts will only appear in the conflict feed under the following conditions:

    1. Automatic Conflict Resolution Fails:
      • If the system has an automatic conflict resolution policy (such as "Last Write Wins"), it tries to resolve conflicts automatically.
      • If automatic resolution succeeds, the conflict does not get logged in the conflict feed.
      • If automatic resolution fails, the conflict is stored in the feed.
    2. Stored Procedure Fails to Resolve Conflict:
      • If your system uses a stored procedure for conflict resolution but that stored procedure encounters an error or fails to execute, the conflict is logged in the feed.
    3. Custom Conflict Resolution Without a Stored Procedure:
      • If your conflict resolution policy is set to custom, but you have not specified a stored procedure to handle conflicts, the system does not resolve them automatically, and conflicts are logged in the conflict feed.

    Why You Might Get a Conflict Error Without Seeing It in the Conflict Feed?

    • If your system is set to Last Write Wins, conflicts are resolved automatically, meaning they never appear in the conflict feed.
    • If you use a custom policy but have defined a proper stored procedure, conflicts may be resolved before they need to be stored.

  4. Oury Ba-MSFT 20,911 Reputation points Microsoft Employee Moderator
    2025-03-25T15:04:03.8533333+00:00

    @João Pereira Thank you for reaching out.

    Inserting a document with same id is not a conflict - it is a unique id violation, you are getting the expected error. To simulate a conflict, you have to update the same record on two different regions at the same time. That would result in a conflict, and as Vinodh247 mentioned in their response, by default Cosmos DB would not store the conflict as the default conflict resolution strategy is last writer wins (LWW). 

    Regards,

    Oury

    0 comments No comments

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.