For AI agents: a documentation index is available at https://www.mongodb.com/docs/llms.txt — markdown versions of all pages are available by appending .md to any URL path.

Resync a Member of a Self-Managed Replica Set

A replica set member becomes "stale" when its replication process falls so far behind that the primary overwrites oplog entries the member has not yet replicated. The member cannot catch up and becomes "stale." When this occurs, you must completely resynchronize the member by removing its data and performing an initial sync.

When syncing a member, choose a time when the system has the bandwidth to move a large amount of data. Schedule the synchronization during a time of low usage or during a maintenance window.

Use the procedures on this page to restore a replica set member in one of the following scenarios:

  • Use the most up-to-date member in the replica set as the sync source. If the sync source has stale data, the resynced member can lose majority-committed writes, which can result in data loss.

  • If you resync more than one member, resync the most out-of-date secondary first. In most cases, the data-bearing secondary with the highest replication lag is the most out-of-date. Use rs.printSecondaryReplicationInfo() to check replication lag.

  • To prevent the replica set from losing its voting majority, never resync more than one replica set member at a time.

  • If you copy data files, ensure that your copy includes the content of the local database.

This procedure deletes the member's existing data. The member then restores its data from another member through the regular initial sync process. For an overview of that process, see Replica Set Data Synchronization.

Initial sync operations can impact the other members of the set and create additional traffic to the source member. The syncing member requires another member of the set that is reachable and up to date.

Warning

During initial sync, mongod removes the contents of the dbPath directory.

1

Connect to the primary with mongosh and run rs.conf(). Note the member's host, priority, votes, and any other settings that you changed from the defaults. You need them to restore the member later.

2

Use rs.remove() to remove the member. Wait for the method to return successfully. If the method times out, do not continue until the new configuration has been installed on a majority of voting members. To learn more, see Reconfiguration Waits Until a Majority of Members Install the Replica Configuration.

3

To ensure a clean shutdown, use the db.shutdownServer() method from mongosh or, on Linux systems, the mongod --shutdown option.

4

Make a backup of all data and sub-directories from the member's dbPath directory. If a full backup is not required, consider backing up just the diagnostic.data directory to preserve potentially-useful troubleshooting data in the event of an issue. See Full Time Diagnostic Data Capture for more information.

5

Delete all data and sub-directories from the member's dbPath directory.

6
7

Connect to the primary and use rs.add() to add the member with votes: 0 and priority: 0. Non-voting members must have a priority of 0.

rs.add( { host: "<hostname>:<port>", votes: 0, priority: 0 } )

Wait for the method to return successfully. The mongod then performs an initial sync. The length of the initial sync process depends on the size of the database and the network latency between members of the replica set.

Adding the member as a non-voting member prevents the member's initial sync from stalling writes that use the "majority" write concern.

8

Use rs.status() to confirm that the member has returned to the SECONDARY state. Then use rs.printSecondaryReplicationInfo() to confirm that its replication lag is close to 0. The SECONDARY state alone does not guarantee that the member is up to date.

9

Use rs.conf() to retrieve the current configuration. Set the member's votes and priority to the values that you recorded in the first step, and then apply the configuration with rs.reconfig(). Wait for the method to return successfully.

This approach "seeds" a new or stale member using the data files from an existing member of the replica set. The data files must be sufficiently recent to allow the new member to catch up with the oplog. Otherwise, the member would need to perform an initial sync.

If you are adding a new member, skip the steps labeled "(Existing member)".

1

If the member already belongs to the replica set, connect to the primary with mongosh and run rs.conf(). Note the member's host, priority, votes, and any other settings that you changed from the defaults. You need them to restore the member later.

2

Use rs.remove() to remove the member. Wait for the method to return successfully. If the method times out, do not continue until the new configuration has been installed on a majority of voting members. To learn more, see Reconfiguration Waits Until a Majority of Members Install the Replica Configuration.

3

You can capture the data files as either a snapshot or a direct copy. However, in most cases you cannot copy data files from a running mongod instance to another because the data files will change during the file copy operation.

Important

You cannot use a mongodump backup for the data files: only a snapshot backup. For approaches to capturing a consistent snapshot of a running mongod instance, see the Backup Methods for a Self-Managed Deployment documentation.

4

Start the mongod instance with a new members[n]._id. Then connect to the primary and use rs.add() to add the member with votes: 0 and priority: 0. Non-voting members must have a priority of 0.

rs.add( { host: "<hostname>:<port>", votes: 0, priority: 0 } )

Wait for the method to return successfully.

5

Allow the member to apply all operations from the oplog until it reflects the current state of the replica set. To see the current status of the replica set, use rs.printSecondaryReplicationInfo() or rs.status(). Wait until the member's replication lag is close to 0.

6

Use rs.conf() to retrieve the current configuration. Set the member's votes and priority to the values that you recorded earlier, or to 1 and 1 for a new member, and then apply the configuration with rs.reconfig(). Wait for the method to return successfully.

Resync one data-bearing member at a time, with the secondaries first and the primary last. Resyncing one member at a time prevents the replica set from losing its voting majority. Arbiters hold no data, so skip them.

Resyncing the only secondary in a replica set with an arbiter can stall writes that use the "majority" write concern. The automatic sync procedure avoids this stall. It re-adds the member as a non-voting member until the member catches up. To learn more, see Mitigate Performance Issues in Self-Managed PSA Replica Sets.

Before you begin, confirm that all members are healthy. Also confirm that the oplog window is long enough to cover the time that an initial sync takes.

1

Identify the most out-of-date secondary. In most cases, it is the secondary with the highest replication lag, which you can check with rs.printSecondaryReplicationInfo(). Then use the automatically sync a member procedure to resync that secondary.

2

Repeat step 1 for each remaining data-bearing secondary, starting with the most out-of-date.

3

When the secondaries are healthy and caught up, use rs.stepDown() to step down the primary. The method waits for an electable secondary to catch up before it steps down the primary. Confirm that a secondary is elected as the new primary before proceeding.

4

The former primary is now a secondary. Use the automatically sync a member procedure to resync it.

5

Use rs.status() and check replication lag to confirm that all data-bearing members have returned to a healthy state and are sufficiently caught up.

Documentation