Skip to content

Azure Cosmos DB Transactions from .NET

Sponsor: Do you build complex software systems? See how NServiceBus makes it easier to design, build, and manage software systems that use message queues to achieve loose coupling. Get started for free.

Learn more about Software Architecture & Design.
Join thousands of developers getting weekly updates to increase your understanding of software architecture and design concepts.

I received a comment to my Optimistic Concurrency in Azure Cosmos DB  a couple weeks ago from Jerry Goyal:
Can we somehow handle the concurrency among multiple documents (transactions)?
Since ETags are defined on each document, you must build your concurrency around them.  However, this made me start to wonder how to update multiple documents at the same time using their respective ETags.  Meaning you would want both documents to update together only if both of their ETags were current.  If one was valid and the other was out of date, none of the documents would update.


It’s pretty obvious that I’m looking for transactions within Azure Cosmos DB.
Azure Cosmos DB supports language-integrated transactions via JavaScript stored procedures and triggers. All database operations inside scripts are executed under snapshot isolation scoped to the collection if it is a single-partition collection, or documents with the same partition key value within a collection, if the collection is partitioned.
There is no client transaction scope you can use with the .NET SDK.   Meaning from the client (.NET) you cannot create a transaction scope.  It has to be done on the Azure Cosmos DB server via  stored procedure or trigger.

Azure Cosmos DB Emulator

There are several ways you can create a stored procedure.  One of which is via the Azure Cosmos DB Emulator web UI. You can create a store procedure under any given collection. You can also create stored procedures via the .NET SDK using the CreateStoredProcedureAsync.  However for this demo, I’m going to be creating it via the emulator.

Stored Procedure

For demo purposes I wanted a way to do a bulk insert.  If any of the customers failed to be added to the collection, due to the Name being falsey, I don’t want any to be created. Here is the stored procedure I created called sp_bulkinsert.  Notice the getContext() method which is available to.  Check out the JavaScript docs for more on the Context/Request/Response.


Here are a couple tests using the .NET SDK which call the stored procedure passing in an array of new customers. The first test passes returning the number of created customer documents.  The second test fails because the customer name is empty/blank.

Demo Source Code

I’ve put together a small .NET Core sample with an XUnit test from above. All the source code for this series is available on GitHub. Are you using Azure Cosmos DB? I’d love to hear your experiences so far along. Let me know on twitter or in the comments.


3 thoughts on “Azure Cosmos DB Transactions from .NET”

Leave a Reply

Your email address will not be published. Required fields are marked *