Build Indexes on Replica Sets
On this page
To minimize the impact of building an index on replica sets and sharded clusters with replica set shards, use the following procedure to build indexes in a rolling fashion.
This procedure does take one member out of the replica set at a time. However, this procedure will only 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 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, or - issuing
db.collection.createIndex()
on themongos
for a sharded cluster.
Oplog Size
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.
Procedure
NOTE
If you need to build an index in a sharded cluster, repeat the following procedure for each replica set that provides each shard.
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. If you cannot stop all writes to the collection, do not use the following procedure to create unique indexes.
Stop One Secondary
Stop the mongod
process on one secondary. Restart the mongod
process without the --replSet
option and running on a different port. [1] This instance is now in “standalone” mode.
For example, if your mongod
normally runs with on the default port of 27017
with the --replSet
option you would use the following invocation:
mongod --port 47017
[1] | 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. |
Build the Index
Create the new index using the createIndex()
in the mongo
shell, or comparable method in your driver. This operation will create or rebuild the index on this mongod
instance
For example, to create an ascending index on the username
field of the records
collection, use the following mongo
shell operation:
db.records.createIndex( { username: 1 } )
Restart the Program mongod
When the index build completes, start the mongod
instance with the --replSet
option on its usual port:
mongod --port 27017 --replSet rs0
Modify the port number (e.g. 27017
) or the replica set name (e.g. rs0
) as needed.
Allow replication to catch up on this member.
Repeat the Procedure for the Remaining Secondaries
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:
Build the Index on the Primary
When all the secondaries have the new index, step down the primary, restart it as a standalone, and build the index on the former primary:
- Use the
rs.stepDown()
method in themongo
shell to step down the primary. Upon successful stepdown, the current primary becomes a secondary and the replica set members elect a new primary. - Stop One Secondary
- Build the Index
- Restart the Program mongod