- Indexes >
- Indexing Tutorials >
- Index Creation Tutorials >
- Build Indexes on Replica Sets
Build Indexes on Replica Sets¶
On this page
Background index creation operations become foreground indexing operations on secondary members of replica sets. The foreground index building process blocks all replication and read operations on the secondaries while they build the index.
Secondaries will begin building indexes after the
primary finishes building the index. In sharded clusters, the mongos will send ensureIndex() to the primary members of the replica
set for each shard, which then replicate to the secondaries after the
primary finishes building the index.
To minimize the impact of building an index on your replica set, use the following procedure to build indexes on secondaries:
See
Indexing Tutorials and Index Concepts for more information.
Considerations¶
- 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.
- 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.
- Do not use this procedure when building a unique index with the
dropDupsoption.
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.
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:
| [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 ensureIndex()
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:
See also
Create an Index and Create a Compound Index for more information.
Restart the Program mongod¶
When the index build completes, start the mongod instance
with the --replSet option on its usual port:
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.
Build Indexes on all Secondaries¶
For each secondary in the set, build an index according to the following steps:
Build the Index on the Primary¶
To build an index on the primary you can either:
Build the index in the background on the primary.
Step down the primary using the
rs.stepDown()method in themongoshell to cause the current primary to become a secondary graceful and allow the set to elect another member as primary.Then repeat the index building procedure, listed below, to build the index on the primary:
Building the index on the background, takes longer than the foreground index build and results in a less compact index structure. Additionally, the background index build may impact write performance on the primary. However, building the index in the background allows the set to be continuously up for write operations during while MongoDB builds the index.