Build Indexes on Replica Sets

To minimize the impact of building an index on replica setdeployments, use the following procedure to build indexes in arolling fashion. For building an index on sharded clusters, seeBuild Indexes on Sharded Clusters instead.

The following procedure for replica set deployments does take onemember out of the replica set at a time. However, this procedure willonly affect one member of the set at a time rather than _all_secondaries at the same time.

Considerations

Unique Indexes

To create unique indexes using the followingprocedure, you must stop all writes to the collection during thisprocedure.

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

Oplog Size

Ensure that your oplog is large enough to permit the indexingor re-indexing operation to complete without falling too far behind tocatch up. See the oplog sizingdocumentation for additional information.

Prerequisites

  • For building unique indexes
  • To create unique indexes using thefollowing procedure, you must stop all writes to the collectionduring the index build. Otherwise, you may end up with inconsistentdata across the replica set members.

Warning

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

Procedure

Important

The following procedure to build indexes in a rolling fashionapplies to replica set deployments, and not sharded clusters. Forthe procedure for sharded clusters with replica set shards, seeBuild Indexes on Sharded Clusters instead.

A. Stop One Secondary and Restart as a Standalone

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

  • Configuration File
  • Command-line Options

If you are using a configuration file, make the followingconfiguration updates:

For example, the updated configuration file for a replicaset member will include content like the following example:

  1. net:
  2. bindIp: localhost,<hostname(s)|ip address(es)>
  3. port: 27217
  4. # port: 27017
  5. #replication:
  6. # replSetName: myRepl
  7. setParameter:
  8. disableLogicalSessionCacheRefresh: true

Other settings (e.g. storage.dbPath, etc.) remain the same.

And restart:

  1. mongod --config <path/To/ConfigFile>

If using command-line options, make the followingconfiguration updates:

For example, if your replica set member normally runswith on the default port of 27017 and the—replSet option, you wouldspecify a different port, omit the —replSet option,and set disableLogicalSessionCacheRefresh parameterto true:

  1. mongod --port 27217 --setParameter disableLogicalSessionCacheRefresh=true

Other settings (e.g. —dbpath, etc.) remain the same.

[1](1, 2) By running the mongod on a differentport, you ensure that the other members of the replica set and allclients will not contact the member while you are building theindex.

B. Build the Index

Connect directly to the mongod instance running as astandalone on the new port and create the new index for thisinstance.

For example, connect a mongo shell to the instance, anduse the createIndex() to create an ascendingindex on the username field of the records collection:

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

C. Restart the Program mongod as a Replica Set Member

When the index build completes, shutdown the mongodinstance. Undo the configuration changes made when starting as astandalone to return the its original configuration and restart asa member of the replica set.

Important

Be sure to remove the disableLogicalSessionCacheRefreshparameter.

For example, to restart your replica set member:

  • Configuration File
  • Command-line Options

If you are using a configuration file:

For example:

  1. net:
  2. bindIp: localhost,<hostname(s)|ip address(es)>
  3. port: 27017
  4. replication:
  5. replSetName: myRepl

Other settings (e.g. storage.dbPath, etc.) remain the same.

And restart:

  1. mongod --config <path/To/ConfigFile>

If you are using command-line options,

  • Revert to the original port number
  • Include the —replSetoption.
  • Remove parameter disableLogicalSessionCacheRefresh.

For example:

  1. mongod --port 27017 --replSet myRepl

Other settings (e.g. —dbpath, etc.) remain the same.

Allow replication to catch up on this member.

D. Repeat the Procedure for the Remaining Secondaries

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

E. Build the Index on the Primary

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: