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.
About this Task
Use the procedures on this page to restore a replica set member in one of the following scenarios:
Add a new node to a replica set. If the instance has no data, follow the Add Members to a Self-Managed Replica Set procedure to add a new member to a replica set. To change the hostname of an existing member without rebuilding it, see Replace a Self-Managed Replica Set Member.
Rebuild an existing stale member. If the instance already belongs to the replica set but has stale or missing data, see Automatically Sync a Member. This option can take longer to replace the data.
Resync a member from copied data files. To copy a recent data directory from another member in the replica set, see Sync by Copying Data Files from Another Member. This option replaces the data more quickly but requires more manual steps.
Perform a rolling initial sync. To resync every member of a replica set one at a time, see Perform a Rolling Initial Sync.
Considerations
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
localdatabase.
Automatically Sync a Member
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.
Remove the member from the replica set.
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.
Stop the member's mongod instance.
To ensure a clean shutdown, use the db.shutdownServer() method from mongosh or, on Linux systems, the mongod --shutdown option.
(Optional) Back up the member's data directory.
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.
Delete the member's data directory contents.
Delete all data and sub-directories from the member's dbPath directory.
Restart the mongod process.
Add the member back as a non-voting member.
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.
Wait for the member to catch up.
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.
Restore the member's voting rights.
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.
Sync by Copying Data Files from Another Member
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)".
(Existing member) Remove it from the replica set.
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.
Copy the data files from the seed member.
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.
Start the member and add it as a non-voting member.
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.
Allow the member to catch up.
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.
Restore the member's voting rights.
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.
Perform a Rolling Initial Sync
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.
Resync one secondary member.
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.
Step down the primary.
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.
Resync the former primary.
The former primary is now a secondary. Use the automatically sync a member procedure to resync it.
Verify the replica set.
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.