liveBook · Manning

Index build

As explained in MongoDB 8.0 in Action, Third Edition

An index build is the process of constructing an index for a collection while coordinating access, replication, voting, and commit or abort decisions.

An index build constructs an index while preserving access to the collection, coordinating replica-set voting, and committing or aborting consistently. MongoDB takes exclusive locks at the beginning and end, but permits interleaved reads and writes during construction. section 7.5

A replicated index build proceeds through these operational stages:

  • The primary logs a startIndexBuild oplog entry; secondary members begin after replicating it. section 7.5
  • Each data-bearing member indexes its collection data and votes to commit after completion. section 7.5
  • While the primary waits for the voting quorum, secondary members incorporate new writes into the index. section 7.5
  • The primary checks constraints such as duplicate keys, marks the index ready, and logs commitIndexBuild when validation succeeds. section 7.5
Table 7.1 Mongo build behavior for different versions

MongoDB 7.1

Earlier MongoDB versions

During the collection scan phase, any index errors detected, except duplicate key errors, are reported immediately, and the index build is halted.

Delays in reporting index build errors may occur because the errors are returned toward the end of the index build, in the commit phase.

If an index build encounters an error, a secondary member can instruct the primary member to halt the index build, preventing the secondary member from crashing.

An error during an index build might lead to the crash of a secondary member.

An index build can be halted automatically if available disk space falls below the threshold specified in the indexBuildMinAvailableDiskSpaceMB parameter.

An index build is not halted due to insufficient available disk space.

Compare MongoDB 7.1 with earlier behavior for error reporting, secondary failure handling, and disk-space safeguards. section 7.5

Monitor or stop a build

Use db.adminCommand({ currentOp: true, $or: [ { op: "command", "command.createIndexes": { $exists: true } }, { op: "command", "command.$truncated": /^\{ createIndexes/ }, { op: "none", "msg": /^Index Build/ } ] }) to inspect active builds. Use dropIndexes, dropIndex(), or dropIndexes() to halt one; do not use killOp in replica sets or sharded clusters. section 7.5.1 section 7.5.2

How MongoDB 8.0 in Action, Third Edition covers it

How deep it goes
Explains locking, oplog events, quorum voting, write integration, constraint checks, version differences, monitoring, and termination.
Examples
Uses startIndexBuild and commitIndexBuild events, a currentOp monitoring command, and dropIndexes termination commands.
What you can do after
Operate/configure it by monitoring active builds and safely terminating them with dropIndexes rather than killOp.
How it connects
Ties index builds to replica sets, sharded clusters, oplog replication, quorum, duplicate key errors, currentOp, and dropIndexes.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of MongoDB 8.0 in Action, Third Edition. The section links point to the parts of the book it draws on.