Docs Menu

Docs HomeDevelop ApplicationsMongoDB Manual

Rolling Index Builds on Replica Sets

On this page

  • Considerations
  • Prerequisites
  • Procedure

Index builds can impact replica set performance. By default, MongoDB 4.4 and later build indexes simultaneously on all data-bearing replica set members. For workloads which cannot tolerate performance decrease due to index builds, consider using the following procedure to build indexes in a rolling fashion.

Rolling index builds take at most one replica set member out at a time, starting with the secondary members, and builds the index on that member as a standalone. Rolling index builds require at least one replica set election.

To create unique indexes using the following procedure, you must stop all writes to the collection during this procedure.

If you cannot stop all writes to the collection during this procedure, do not use the procedure on this page. Instead, build your unique index on the collection by issuing db.collection.createIndex() on the primary for a replica set.

Ensure that your oplog is large enough to permit the indexing or re-indexing operation to complete without falling too far behind to catch up. See the oplog sizing documentation for additional information.

For building unique indexes

To create unique indexes using the following procedure, you must stop all writes to the collection during the index build. Otherwise, you may end up with inconsistent data across the replica set members.

Warning

If you cannot stop all writes to the collection, do not use the following procedure to create unique indexes.

Important

The following procedure to build indexes in a rolling fashion applies to replica set deployments, and not sharded clusters. For the procedure for sharded clusters, see Rolling Index Builds on Sharded Clusters instead.

Stop the mongod process associated with a secondary. Restart after making the following configuration updates:

[1](1, 2) By running the mongod on a different port, you ensure that the other members of the replica set and all clients will not contact the member while you are building the index.

Connect directly to the mongod instance running as a standalone on the new port and create the new index for this instance.

For example, connect mongosh to the instance, and use the createIndex() to create an ascending index on the username field of the records collection:

db.records.createIndex( { username: 1 } )

When the index build completes, shutdown the mongod instance. Undo the configuration changes made when starting as a standalone to return the its original configuration and restart as a member of the replica set.

Important

Be sure to remove the disableLogicalSessionCacheRefresh parameter.

For example, to restart your replica set member:

Allow replication to catch up on this member.

Once the member catches up with the other members of the set, repeat the procedure one member at a time for the remaining secondary members:

  1. A. Stop One Secondary and Restart as a Standalone

  2. B. Build the Index

  3. C. Restart the Program mongod as a Replica Set Member

When all the secondaries have the new index, step down the primary, restart it as a standalone using the procedure described above, and build the index on the former primary:

  1. Use the rs.stepDown() method in mongosh to step down the primary. Upon successful stepdown, the current primary becomes a secondary and the replica set members elect a new primary.

  2. A. Stop One Secondary and Restart as a Standalone

  3. B. Build the Index

  4. C. Restart the Program mongod as a Replica Set Member

←  Index Builds on Populated CollectionsRolling Index Builds on Sharded Clusters →