Create a chaos experiment that uses a service-direct fault to fail over an Azure Cosmos DB instance
You can use a chaos experiment to verify that your application is resilient to failures by causing those failures in a controlled environment. In this guide, you will cause a multi-read, single-write Azure Cosmos DB failover using a chaos experiment and Azure Chaos Studio. Running this experiment can help you defend against data loss when a failover event occurs.
These same steps can be used to set up and run an experiment for any service-direct fault. A service-direct fault runs directly against an Azure resource without any need for instrumentation, unlike agent-based faults, which require installation of the chaos agent.
Prerequisites
- An Azure subscription. If you don't have an Azure subscription, create an Azure free account before you begin.
- An Azure Cosmos DB account. If you do not have an Azure Cosmos DB account, you can follow these steps to create one.
- At least one read and one write region setup for your Azure Cosmos DB account.
Enable Chaos Studio on your Azure Cosmos DB account
Chaos Studio cannot inject faults against a resource unless that resource has been onboarded to Chaos Studio first. You onboard a resource to Chaos Studio by creating a target and capabilities on the resource. Azure Cosmos DB accounts only have one target type (service-direct) and one capability (failover), but other resources may have up to two target types - one for service-direct faults and one for agent-based faults - and many capabilities.
- Open the Azure portal.
- Search for Chaos Studio (preview) in the search bar.
- Click on Targets and navigate to your Azure Cosmos DB account.
- Check the box next to your Azure Cosmos DB account and click Enable targets then Enable service-direct targets from the dropdown menu.
- A notification will appear indicating that the resource(s) selected were successfully enabled.
You have now successfully onboarded your Azure Cosmos DB account to Chaos Studio. In the Targets view you can also manage the capabilities enabled on this resource. Clicking the Manage actions link next to a resource will display the capabilities enabled for that resource.
Create an experiment
With your Azure Cosmos DB account now onboarded, you can create your experiment. A chaos experiment defines the actions you want to take against target resources, organized into steps, which run sequentially, and branches, which run in parallel.
- Click on the Experiments tab in the Chaos Studio navigation. In this view, you can see and manage all of your chaos experiments. Click on Add an experiment
- Fill in the Subscription, Resource Group, and Location where you want to deploy the chaos experiment. Give your experiment a Name. Click Next : Experiment designer >
- You are now in the Chaos Studio experiment designer. The experiment designer allows you to build your experiment by adding steps, branches, and faults. Give a friendly name to your Step and Branch, then click Add fault.
- Select CosmosDB Failover from the dropdown, then fill in the Duration with the number of minutes you want the failure to last and readRegion with the read region of your Azure Cosmos DB account. Click Next: Target resources >
- Select your Azure Cosmos DB account, and click Next
- Verify that your experiment looks correct, then click Review + create, then Create.
Give experiment permission to your target resource
When you create a chaos experiment, Chaos Studio creates a system-assigned managed identity that executes faults against your target resources. This identity must be given appropriate permissions to the target resource for the experiment to run successfully. These steps can be used for any resource and target type by modifying the role assignment in step #3 to match the appropriate role for that resource and target type.
- Navigate to your Azure Cosmos DB account and click on Access control (IAM).
- Click Add then click Add role assignment.
- Search for Cosmos DB Operator and select the role. Click Next
- Click Select members and search for your experiment name. Select your experiment and click Select. If there are multiple experiments in the same tenant with the same name, your experiment name will be truncated with random characters added.
- Click Review + assign then Review + assign.
Run your experiment
You are now ready to run your experiment. To see the impact, we recommend opening your Azure Cosmos DB account overview and going to Replicate data globally in a separate browser tab. Refreshing periodically during the experiment will show the region swap.
- In the Experiments view, click on your experiment, and click Start, then click OK.
- When the Status changes to Running, click Details for the latest run under History to see details for the running experiment.
Next steps
Now that you have run a Azure Cosmos DB service-direct experiment, you are ready to:
Feedback
Submit and view feedback for