You are currently viewing Azure Cosmos DB enable multi-region writes

Azure Cosmos DB enable multi-region writes

In previous article, we have seen how to enable global replication for Cosmos DB databases. In this article let’s have a look at how to enable multiple write regions.

Why multi-region writes?

For lower latency, you can enable geo-replication on Cosmos DB, allowing the data to replicate in different Azure regions. By default, there will be a write region, which will accept all the write requests. And all rest of the Azure regions will accept read requests.

But if you have write enabled Azure region in West US region, then write requests coming from South East Asia region or Central India region might have high latency. In order to reduce the write latency, you can enable writes on multiple regions.

Please note that if you enable multi-region writes, you should understand that conflicts may arise because same data was updated in two different regions. For handling such instances, you may have to apply the conflict resolution policy. This article solely focuses on enabling multi-region writes, it does not talk about conflicts.

How to enable multi-region writes ?

Login to Azure Portal and navigate to the Cosmos DB account. Then select the Replicate data globally option from left side menu. Then you can enable Multi-region writes by toggling the button.

Then you can add the regions by clicking on the blue bubbles on world map or by clicking on Add regions hyperlink. All the regions you add will have both read and writes enabled. Once all modifications are done, you can hit save button to apply the changes.

Alternatively, you can also use below Azure CLI to create the new Cosmos DB account with multi-region writes enabled.

# Create account with session consistency and multi-master enabled
az cosmosdb create                  \
    --resource-group "samples"      \
    --name "SamplesCosmosDB"        \
    --kind GlobalDocumentDB         \
    --locations regionName="South Central US" failoverPriority=0 --locations regionName="North Central US" failoverPriority=1 \
    --default-consistency-level "Session" \
    --enable-multiple-write-locations true

Code Modifications

Above steps help you to enable multi-region writes on Cosmos DB account level. Once these modifications are done, you need to make sure that you have set cosmosClientOptions.ApplicationRegion property to the region in which the application is being deployed.

This will populate the PreferredLocations property based on the geo-proximity from location passed in. If a new region is later added to the account, the application does not have to be updated or redeployed, it will automatically detect the closer region and will auto-home on to it should a regional event occur.

The code will vary based on which client SDK (.NET, Java, Python, etc) you are using. Below is sample code of how to set CosomosClientOptions if you are using .NET client SDK.

CosmosClient cosmosClient = new CosmosClient(
    "<connection-string-from-portal>", 
    new CosmosClientOptions()
    {
        ApplicationRegion = Regions.WestUS2,
    });

I hope you enjoyed this article. Let me know your thoughts.

Leave a Reply