MongoDB 2.6 Indexing Improvements: Things worth knowing


TL;DR: Background indexing now works with replica set secondaries.

MongoDB has, over recent versions, improved its ability to index stored data on the primary server, bringing in background indexing for a single index in 2.2 and multiple background indexes in 2.4. Building an index in the background keeps the database available (meaning, operations are not blocked) while the index is being created.

Creating a background index

An index can be made to go in the background by adding { background: true } to the options list for ensureIndex(). By default, indexes are built in the foreground. Therefore, it is good to double-check before sending off the index creation command, otherwise your database could stop responding to other requests.

But, back on topic - support for this indexing feature on secondary members of MongoDB replica sets lagged behind. Background indexes, on a replica set primary, were translated into foreground index tasks on replica set secondaries. This meant that any reads and other replication activities were blocked on secondaries while the index built. While background indexing was very useful in the case of a single server, its usage was somewhat limited for replica sets.

As of MongoDB 2.6 though, that situation has improved with support for background indexing on secondaries. Once the primary has finished its background indexing, the secondaries can begin their background indexing. There's still a number of things to take into account when building an index on a replica set though; these are covered in the MongoDB documentation.

Index builds will continue after being stopped

Also worth knowing is that MongoDB 2.6 will now automatically restart index rebuilding when a mongod primary or standalone instance is started up but only if that instance hasn't been cleanly shutdown or a user had specifically stopped/killed the index build.

Normal termination doesn't trigger the behavior and it can be disabled with the command line option --noIndexBuildRetry.

DropDups deprecated

And finally, if you use ensureIndex()'s dropDups option, you may want to consider ways to engineer it out of your system. MongoDB 2.6 has deprecated the dropDups option as a precursor to future removal. DropDups allows indexes to be created on fields which may have duplicates. However, its technique for resolving duplicates leaves much to be desired and, due to the fact that it keeps the first instance it finds and deletes any remaining ones, it can lead to unintended consequences.

Ready to dive into MongoDB 2.6?

Excited about all the improvements that MongoDB 2.6 offers and ready to give it a go? If so, the MongoHQ Elastic Deployments are the best way to get started. If you don’t have a MongoHQ account already, it's quick and easy to sign up. If you’re already a MongoHQ user, you can provision a new Elastic Deployment with the Create Database button.

Happy database'ing!

Dj Walker-Morgan
Dj Walker-Morgan was Compose's resident Content Curator, and has been both a developer and writer since Apples came in II flavors and Commodores had Pets. Love this article? Head over to Dj Walker-Morgan’s author page to keep reading.

Conquer the Data Layer

Spend your time developing apps, not managing databases.