> This page location: /docs.md > /docs/management.md > /docs/atlas/create-connect-deployments.md > /docs/atlas/manage-clusters.md > /docs/atlas/tutorial/test-resilience.md

> For the complete MongoDB documentation index, see www.mongodb.com/docs/llms.txt

<!--
Tab options on this page. Append to the .md URL to filter:
  ?tabs=<id,...>   select specific tabs (e.g. ?tabs=nodejs,shell)
  ?allTabs=true    include every tab
  (no param)       default: one tab per tabset

Available tabs:
  other tabs: atlascli, api, ui
-->

# Test Primary Failover

**Note:**

This feature is available for `M10` clusters or higher. To learn more about which features are available for Free clusters, see [Atlas Free Cluster Limits](https://www.mongodb.com/docs/atlas/reference/free-shared-limitations.md#std-label-atlas-free-tier), and for Flex clusters, see [Atlas Flex Limitations.](https://www.mongodb.com/docs/atlas/reference/flex-limitations.md#std-label-flex-cluster-limitations)

Your applications should handle a change of primary without any downtime.

On Atlas Core clusters:

- Atlas conducts [replica set elections](https://www.mongodb.com/docs/manual/core/replica-set-elections.md#std-label-replica-set-elections) when it makes configuration changes, such as patch updates and scaling events, and when failures occur.

On Atlas Infinite clusters:

- The storage layer coordinates failover instead of an election among the compute nodes. A standby compute node becomes the primary node without oplog replay or a cold start.

- The primary node can change during operating system or VM maintenance, vertical scaling up or down, MongoDB version upgrades, and failures.

- Atlas maintains availability during rolling restarts.

To learn how to build a resilient application, see [Build a Resilient Application with MongoDB Atlas.](https://www.mongodb.com/docs/atlas/resilient-application.md#std-label-resilient-application)

You can enable retryable writes by adding [retryWrites=true](https://www.mongodb.com/docs/manual/reference/connection-string/#urioption.retryWrites) to your Atlas URI connection string. To learn more, see [Retryable Writes.](https://www.mongodb.com/docs/manual/core/retryable-writes/)

You can use the Atlas UI and API (Application Programming Interface) to test the failure of the replica set [primary](https://www.mongodb.com/docs/manual/reference/glossary.md#std-term-primary) in your Atlas cluster and observe how your application handles a replica set failover.

## Required Access

To start a failover test, you must have [`Organization Owner`](https://www.mongodb.com/docs/atlas/reference/user-roles.md#mongodb-authrole-Organization-Owner), [`Project Owner`](https://www.mongodb.com/docs/atlas/reference/user-roles.md#mongodb-authrole-Project-Owner), [`Project Cluster Manager`](https://www.mongodb.com/docs/atlas/reference/user-roles.md#mongodb-authrole-Project-Cluster-Manager) or [`Project Stream Processing Owner`](https://www.mongodb.com/docs/atlas/reference/user-roles.md#mongodb-authrole-Project-Stream-Processing-Owner) access to the project.

## Prerequisites

Before you test the failure of the replica set [primary](https://www.mongodb.com/docs/manual/reference/glossary.md#std-term-primary), you must meet the following conditions:

- All pending changes to your cluster must be complete.

- All members of the cluster must be in a healthy state with up-to-date monitoring data.

- Each replica set or shard must have a primary node.

- Any member of the cluster must have less than a 10-second replication lag.

- All members of your cluster must have at least 5% of available disk space remaining.

- All primary node oplogs must have enough space for a three-hour operation.

## Test Primary Failover Process

**Important:**

Ensure that your Atlas cluster is healthy before you test primary failover. Otherwise, Atlas might reject your request.

When you submit a request to test primary failover, Atlas simulates a failover event. During this process:

Atlas shuts down the current [primary.](https://www.mongodb.com/docs/manual/reference/glossary.md#std-term-primary)

The members of the replica set hold an [election](https://www.mongodb.com/docs/manual/core/replica-set-elections.md#std-label-replica-set-elections) to choose which of the secondaries will become the new primary. On average, an election takes approximately five seconds.

Atlas brings the original primary back to the [replica set](https://www.mongodb.com/docs/manual/reference/glossary.md#std-term-replica-set) as a [secondary](https://www.mongodb.com/docs/manual/reference/glossary.md#std-term-secondary). When the old primary rejoins the replica set, it will sync with the new primary to catch up any writes that occurred during its downtime.

The following statements describe Atlas behavior during rollovers and when testing failover in sharded clusters:

- On Atlas Core clusters, the original primary might have accepted write operations that weren't successfully replicated to the secondaries before it stepped down. When that primary re-joins the replica set and begins synchronizing, it rolls back those unreplicated writes. To learn more, see [Rollbacks During Failover in Atlas.](https://www.mongodb.com/docs/atlas/replica-set-rollbacks.md#std-label-atlas-replica-set-rollbacks)

- On Atlas Infinite clusters, rollbacks don't occur. The storage layer commits write operations, so a former primary node has no unreplicated writes to revert.

- Only the [`mongos`](https://www.mongodb.com/docs/manual/reference/program/mongos.md#mongodb-binary-bin.mongos) processes that are on the same instances as the primaries of the replica sets in the sharded cluster are restarted.

- The primaries of the replica sets in the sharded cluster are restarted in parallel.

### Atlas CLI

To start a failover test for the specified cluster in your project using the Atlas CLI, run the following command:

```sh

atlas clusters failover <clusterName> [options]

```

To learn more about the command syntax and parameters, see the Atlas CLI documentation for [atlas clusters failover.](https://www.mongodb.com/docs/atlas/cli/current/command/atlas-clusters-failover/)

## Verify the Failover

To verify that the failover is successful:

1. In Atlas, go to the Clusters page for your project.

   If it's not already displayed, select the organization that contains your desired project from the  Organizations menu in the navigation bar.

   If it's not already displayed, select your desired project from the Projects menu in the navigation bar.

   In the sidebar, click Clusters under the Database heading.

   The [Clusters](https://cloud.mongodb.com/go?l=https%3A%2F%2Fcloud.mongodb.com%2Fv2%2F%3Cproject%3E%23%2Fclusters) page displays.

2. Observe the nodes.

   Click the name of the cluster for which you performed the failover test.

   Observe the following changes in the list of nodes in the Overview tab:

   - The original `PRIMARY` node is now a `SECONDARY` node.

   - A former `SECONDARY` node is now the `PRIMARY` node.

## Troubleshoot Failover Issues

If your application doesn't handle the failover gracefully, ensure the following:

- You are using the [SRV Connection Format.](https://www.mongodb.com/docs/manual/reference/connection-string-formats.md#std-label-connections-dns-seedlist)

- You are using the latest version of the driver.

- You have implemented appropriate retry logic in your application.
