Conflict and Not Found Errors When Creating Stored Procedures in Azure Cosmos DB

Rohitkumar M Mistry 0 Reputation points
2025-04-24T09:37:57.66+00:00

A Cosmos DB database contains two containers, ContainerA and ContainerB. Stored procedures (SPs) are being created for both containers with the same ID and body.

The consistency level for Cosmos DB is set to strong. ContainerA does not have any SPs, while ContainerB contains several. An attempt to create the same SP in ContainerA leads to various errors.

When trying to read the SP with a particular ID, the following error is received:

Response status code does not indicate success: NotFound (404);
code : NotFound
message : Message: {"Errors":["Resource Not Found."]}

When attempting to create the SP with a particular ID, the error encountered is:

Response status code does not indicate success: Conflict (409);
code : Conflict
message : Message: {"Errors":["Resource with specified id, name, or unique index already exists."]}

After receiving the conflict error, an attempt is made to replace the existing SP by waiting for some time for propagation, but the following error is returned:

Response status code does not indicate success: NotFound (404);
code : NotFound
message : Entity with the specified id does not exist in the system.

Creating SPs directly in the Azure portal's Data Explorer results in:

Error while creating stored procedure spProductUpsert:
Message: {"code":"NotFound","message":"Entity with the specified id does not exist in the system."}

The questions are:

  • How can the issues of transitioning from Not Found to Conflict (and vice versa) be resolved?
  • How can the same SPs with the same ID be created in ContainerA?
private async Task<StoredProcedureProperties> GetOrCreateStoreProcedure(string embeddedResourcePath, string spId, CosmosClient documentClient, Container collection)
{
    try
    {
        string body;
        using (StreamReader sr = new StreamReader(Assembly.GetExecutingAssembly().GetManifestResourceStream(embeddedResourcePath), Encoding.UTF8))
        {
            body = sr.ReadToEnd();
        }
        try
        {
            return await collection.Scripts.ReadStoredProcedureAsync(spId);
        }
        catch (CosmosException ex) when (ex.StatusCode == HttpStatusCode.NotFound)
        {
            try
            {
                return await collection.Scripts.CreateStoredProcedureAsync(new StoredProcedureProperties { Id = spId, Body = body });
            }
            catch (CosmosException exception) when (exception.StatusCode == HttpStatusCode.Conflict)
            {
                await Task.Delay(2000); //wait 2 sec
                return await collection.Scripts.ReplaceStoredProcedureAsync(new StoredProcedureProperties { Id = spId, Body = body });
            }
        }
    }
    catch (Exception ex)
    {
        throw ex;
    }
}
ASP.NET Core Training
ASP.NET Core Training
ASP.NET Core: A set of technologies in the .NET Framework for building web applications and XML web services.Training: Instruction to develop new skills.
59 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Pradeep M 7,705 Reputation points Microsoft External Staff
    2025-04-24T12:22:09.77+00:00

    Hi Rohitkumar M Mistry

    Thank you for reaching out to Microsoft Q & A forum. 

    The NotFound (404) and Conflict (409) errors are likely due to replication delays and the fact that stored procedures are container-specific in Cosmos DB. Ensure you're referencing the correct container (ContainerA). A Conflict can occur if the stored procedure exists in metadata but hasn't fully propagated, causing a NotFound error when reading. Replacing the stored procedure immediately after a Conflict can fail if it's not fully available. 

    To resolve this, try creating the stored procedure if you encounter a NotFound error. If you get a Conflict, wait briefly (2 seconds) and then retry Read. If successful, proceed with Replace; otherwise, retry Create. Also, verify in the Azure Portal Data Explorer that the stored procedure doesn't exist or isn't in a partial state. For further details, please refer to the Troubleshoot NotFound errors and Troubleshoot Conflict errors documentation.     

    If you have found the answer provided to be helpful, please click on the "Accept answer/Upvote" button so that it is useful for other members in the Microsoft Q&A community.

    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.