Regional failovers are a crucial aspect in maintaining Continuous Availability (CA) and business continuity. In Microsoft Azure Cosmos DB, failover capabilities allow applications to be available and up to date even if a data center fails. Azure Cosmos DB allows for manual and automatic failovers, but in this post, we’ll focus on initiating a regional failover manually using PowerShell and Azure CLI.
Why Initiate Failover
A failover should be initiated whenever you anticipate a data center disaster or aimed to test the high availability of your app. Azure Cosmos DB’s multi-region write capabilities provide low latency, high availability, and consistency in data. By configuring multiple regions for writes, you can avoid loss of availability due to regional failures.
Understanding PowerShell and Azure CLI
PowerShell is a task-based command-line shell and scripting language. It’s built on the .NET Common Language Runtime (CLR) and accepts and returns .NET objects. Azure CLI, on the other hand, is a set of commands used to create and manage Azure resources. Contrary to PowerShell, it is a cross-platform tool.
Failing Over with PowerShell
To initiate a manual failover with PowerShell, you will need the AzureRM PowerShell module. Make sure you are running PowerShell 5.1 or later on your machine. The following example demonstrates the process by switching over to a secondary region.
#Login to Azure
Login-AzureRmAccount
#Select subscription to use
$subscriptionId = ‘your-subscription-id’
Select-AzureRmSubscription -SubscriptionId $subscriptionId
#Failover
$resourceGroupName = ‘your-resource-group-name’
$accountName = ‘your-cosmosdb-account-name’
$failoverPolicy = @(
@{
“locationName”= ‘second-write-region’
“failoverPriority”= 0
},
@{
“locationName”= ‘first-write-region’
“failoverPriority”= 1
},
@{
“locationName”= ‘third-write-region’
“failoverPriority”= 2
}
)
Set-AzureRmCosmosDBAccountFailoverPriority -ResourceGroupName $resourceGroupName -Name $accountName -FailoverPolicy $failoverPolicy
Failing Over with Azure CLI
You can also initiate a regional failover using Azure CLI, another powerful tool offered by Azure. Here’s a sample script.
#Login to Azure
az login
#Failover
resourceGroupName=’your-resource-group-name’
accountName=’your-cosmosdb-account-name’
failoverPolicy='[ { “locationName”: “second-write-region”, “failoverPriority”: 0 }, { “locationName”: “first-write-region”, “failoverPriority”: 1 }, { “locationName”: “third-write-region”, “failoverPriority”: 2 } ]’
az cosmosdb failover-priority-change –name $accountName –resource-group $resourceGroupName –failover-policies $failoverPolicy
Summary
Initiating a regional failover in Azure Cosmos DB is a straightforward process whether you’re using PowerShell or Azure CLI. Both tools offer the functionality to manually redistribute the load to other regions in case of a regional outage, or when you want to test the high availability of your application. To sum it up, choosing between Azure CLI and Windows PowerShell would depend on your preferred interface and scripting language, the platforms you’re using, and the tasks you need to perform.
Disclaimer
The code examples in this post are for illustrative purposes only. You’ll need to replace the placeholders with your own values before running the commands. Make sure to consult Azure’s best practices before proceeding with a manual failover.
Practice Test
True or False: PowerShell is a cross-platform task automation solution from Microsoft.
- True
- False
Answer: True
Explanation: PowerShell is a task-based scripting technology and automation framework from Microsoft that is built on the .NET Framework.
Can you use Azure CLI to initiate a regional failover process in Azure Cosmos DB?
- Yes
- No
Answer: Yes
Explanation: Azure CLI (command-line-interface) is a set of commands used to create and manage Azure resources. You can use it to start a regional failover for Azure Cosmos DB.
Which of the following can you use to automate tasks in Microsoft Azure? (multiple select)
- – Azure CLI
- – PowerShell
- – Bash
- – None of the above
Answer: Azure CLI, PowerShell
Explanation: Both Azure CLI and PowerShell can be used to automate tasks in Microsoft Azure. Bash is a Unix shell and command language, which is not specific to Azure.
True or False: You cannot migrate the Azure Cosmos DB to another region using PowerShell.
- True
- False
Answer: False
Explanation: With the help of PowerShell, users can migrate the Azure Cosmos DB to another region.
To perform a manual failover for your Azure Cosmos account, which of the following command you would use with Azure CLI?
- -AccountFailover
- -ManualFailover
- -region-failover
- -None of the above
Answer: None of the above
Explanation: The az cosmosdb failover-priority-change command is used to perform a manual failover for Azure Cosmos DB account using Azure CLI.
True or False: When you initiate a failover, the changes replicate to all regions associated with your Azure Cosmos DB account.
- True
- False
Answer: True
Explanation: When a failover is performed, the changes that were made are replicated across all associated regions of your Azure Cosmos DB account.
Which command is used in PowerShell to change the failover priority of Azure Cosmos DB account?
- -Change-FailoverPriority
- -Set-CosmosDbAccountFailoverPriority
- -Set-AzureRmCosmosDBAccountFailoverPriority
- -None of the above
Answer: Set-AzureRmCosmosDBAccountFailoverPriority
Explanation: Set-AzureRmCosmosDBAccountFailoverPriority is the command used in PowerShell to change the failover priority of Azure Cosmos DB account.
What is the default mode for regional failover in Azure Cosmos DB?
- -Manual
- -Automatic
- -None
Answer: Automatic
Explanation: The default mode for regional failover in Azure Cosmos DB is automatic. You can however change it to manual if needed.
True or False: You need to enable multiple write regions to manually initiate a failover in Azure Cosmos DB.
- True
- False
Answer: True
Explanation: To manually failover, you need to have multiple write regions enabled in your Azure Cosmos DB account.
What is the maximum number of regions that Azure Cosmos DB can associate with your account?
- -99
- -50
- -20
- -10
Answer: 99
Explanation: Azure Cosmos DB can associate a maximum of 99 regions with your Azure Cosmos DB account.
Interview Questions
What is a regional failover in the context of Microsoft Azure Cosmos DB?
A regional failover refers to the process of switching from the primary region to the secondary region for read and write operations in Microsoft Azure Cosmos DB when the primary region becomes unavailable due to certain issues like a natural disaster or network failure.
What are the prerequisites for initiating a regional failover in Azure Cosmos DB?
Before initiating a region failover, you must have multiple regions enabled in your Azure Cosmos account, and your Cosmos DB must be set up for multi-region writes.
How can PowerShell be used to initiate a failover in Azure Cosmos DB?
PowerShell can be used to initiate a regional failover by invoking the Failover-PriorityChange command in the Az.CosmosDB PowerShell module, where the regions are reordered based on their failover priorities.
What is the Azure CLI command to initiate a manual failover in Azure Cosmos DB?
The Azure CLI command to initiate a manual failover is “az cosmosdb failover-priority-change”, followed by the necessary parameters for the account name, resource group and failover policies.
What is the role of Azure PowerShell module for a manual failover process?
Azure PowerShell module provides commands for Azure Cosmos DB management operations such as invoking Failover-PriorityChange command to change the failover priority of Cosmos DB regions for initiating a manual failover.
When using Azure CLI to initiate a regional failover, what does the –failover-policies parameter represent?
The –failover-policies parameter specifies a space-separated list of region=failoverPriority pairs, where ‘region’ is the region name and ‘failoverPriority’ is the failover priority for that region.
What is Microsoft’s recommendation for failover priority in Azure Cosmos DB?
Microsoft recommends setting a lower failover priority value for the most preferred region, as this value determines the failover order. In the event of regional outage, Azure Cosmos DB will failover to the region with the next highest priority.
Can you initiate a manual failover in Azure Cosmos DB if you only have a single region enabled?
No, you need to have multiple regions enabled in your Azure Cosmos account in order to initiate a manual failover.
Is it possible to automate the regional failover process in Azure Cosmos DB?
Yes, Azure Cosmos DB provides automatic failover capabilities which can be enabled or disabled as per requirements. However, manual failovers need to be initiated as needed.
What is the syntax for reordering failover priorities using Azure CLI?
The syntax is “az cosmosdb failover-priority-change –account-name –resource-group –failover-policies” followed by a list of region=failoverPriority pairs in the order of their priority.
What is the result of running the PowerShell command “Failover-PriorityChange”?
Running the PowerShell command “Failover-PriorityChange” initiates a manual failover by changing the failover priority order of the Azure regions in the Cosmos DB account.
Using Azure CLI, how can you check the regional failover progress?
You can check regional failover progress using the command “az cosmosdb show –name –resource-group” which returns the current service level objectives.
Can you cancel a failover process once it has been initiated?
No, once a failover process has been initiated in Azure Cosmos DB, it cannot be canceled.
What is the purpose of Azure CLI in Azure Cosmos DB?
Azure CLI provides a set of commands used to create, manage, and scale applications that take full advantage of all the capabilities of Azure Cosmos DB.
Are there any potential data losses when a failover occurs?
No, Azure Cosmos DB guarantees zero data loss in the case of regional failovers because it uses synchronous replication within and across multiple geographical regions to offer high availability.