In this article, we show how to use the Azure portal to setup Azure Cosmos DB global distribution and then connect using the Graph API (preview).
This article covers the following tasks:
- Configure global distribution using the Azure portal
- Configure global distribution using the Graph APIs (preview)
You can learn about Azure Cosmos DB global distribution in this Azure Friday video with Scott Hanselman and Principal Engineering Manager Karthik Raman.
For more information about how global database replication works in Azure Cosmos DB, see Distribute data globally with Cosmos DB.
Add global database regions using the Azure Portal
Azure Cosmos DB is available in all Azure regions world-wide. After selecting the default consistency level for your database account, you can associate one or more regions (depending on your choice of default consistency level and global distribution needs).
- In the Azure portal, in the left bar, click Azure Cosmos DB.
- In the Azure Cosmos DB blade, select the database account to modify.
- In the account blade, click Replicate data globally from the menu.
In the Replicate data globally blade, select the regions to add or remove by clicking regions in the map, and then click Save. There is a cost to adding regions, see the pricing page or the Distribute data globally with Azure Cosmos DB article for more information.
Once you add a second region, the Manual Failover option is enabled on the Replicate data globally blade in the portal. You can use this option to test the failover process or change the primary write region. Once you add a third region, the Failover Priorities option is enabled on the same blade so that you can change the failover order for reads.
Selecting global database regions
There are two common scenarios for configuring two or more regions:
- Delivering low-latency access to data to end users no matter where they are located around the globe
- Adding regional resiliency for business continuity and disaster recovery (BCDR)
For delivering low-latency to end-users, it is recommended to deploy both the application and add Azure Cosmos DB in the regions thats correspond to where the application's users are located.
For BCDR, it is recommended to add regions based on the region pairs described in the Business continuity and disaster recovery (BCDR): Azure Paired Regions article.
Connecting to a preferred region using the Graph API using the .NET SDK
The Graph API is exposed as an extension library on top of the DocumentDB SDK.
In order to take advantage of global distribution, client applications can specify the ordered preference list of regions to be used to perform document operations. This can be done by setting the connection policy. Based on the Azure Cosmos DB account configuration, current regional availability and the preference list specified, the most optimal endpoint will be chosen by the SDK to perform write and read operations.
This preference list is specified when initializing a connection using the SDKs. The SDKs accept an optional parameter "PreferredLocations" that is an ordered list of Azure regions.
- Writes: The SDK will automatically send all writes to the current write region.
- Reads: All reads will be sent to the first available region in the PreferredLocations list. If the request fails, the client will fail down the list to the next region, and so on. The SDKs will only attempt to read from the regions specified in PreferredLocations. So, for example, if the Cosmos DB account is available in three regions, but the client only specifies two of the non-write regions for PreferredLocations, then no reads will be served out of the write region, even in the case of failover.
The application can verify the current write endpoint and read endpoint chosen by the SDK by checking two properties, WriteEndpoint and ReadEndpoint, available in SDK version 1.8 and above. If the PreferredLocations property is not set, all requests will be served from the current write region.
Using the SDK
For example, in the .NET SDK, the
ConnectionPolicy parameter for the
DocumentClient constructor has a property called
PreferredLocations. This property can be set to a list of region names. The display names for Azure Regions can be specified as part of
The URLs for the endpoints should not be considered as long-lived constants. The service may update these at any point. The SDK handles this change automatically.
// Getting endpoints from application settings or other configuration location Uri accountEndPoint = new Uri(Properties.Settings.Default.GlobalDatabaseUri); string accountKey = Properties.Settings.Default.GlobalDatabaseKey; ConnectionPolicy connectionPolicy = new ConnectionPolicy(); //Setting read region selection preference connectionPolicy.PreferredLocations.Add(LocationNames.WestUS); // first preference connectionPolicy.PreferredLocations.Add(LocationNames.EastUS); // second preference connectionPolicy.PreferredLocations.Add(LocationNames.NorthEurope); // third preference // initialize connection DocumentClient docClient = new DocumentClient( accountEndPoint, accountKey, connectionPolicy); // connect to Azure Cosmos DB await docClient.OpenAsync().ConfigureAwait(false);
That's it, that completes this tutorial. You can learn how to manage the consistency of your globally replicated account by reading Consistency levels in Azure Cosmos DB. And for more information about how global database replication works in Azure Cosmos DB, see Distribute data globally with Azure Cosmos DB.
In this tutorial, you've done the following:
- Configure global distribution using the Azure portal
- Configure global distribution using the DocumentDB APIs
You can now proceed to the next tutorial to learn how to develop locally using the Azure Cosmos DB local emulator.