DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Because the DevOps movement has redefined engineering responsibilities, SREs now have to become stewards of observability strategy.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Related

  • How to Build Real-Time BI Systems: Architecture, Code, and Best Practices
  • How To Build Resilient Microservices Using Circuit Breakers and Retries: A Developer’s Guide To Surviving
  • Accelerating Debugging in Integration Testing: An Efficient Search-Based Workflow for Impact Localization
  • Loading XML into MongoDB

Trending

  • Endpoint Security Controls: Designing a Secure Endpoint Architecture, Part 2
  • Building a Real-Time Change Data Capture Pipeline With Debezium, Kafka, and PostgreSQL
  • How to Convert XLS to XLSX in Java
  • Customer 360: Fraud Detection in Fintech With PySpark and ML
  1. DZone
  2. Data Engineering
  3. Databases
  4. The Pitfalls of Creating Indexes on MongoDB

The Pitfalls of Creating Indexes on MongoDB

Indexing is essential for optimizing your database, but MongoDB's three main indexing methods come with caveats that might trip up those who aren't prepared for them.

By 
Dharshan Rangegowda user avatar
Dharshan Rangegowda
·
Feb. 10, 17 · Opinion
Likes (1)
Comment
Save
Tweet
Share
8.0K Views

Join the DZone community and get the full member experience.

Join For Free

Indexes are a critical part of any database operation. Defining the right indexes can make a huge difference to the performance of your database servers. However, creating indexes in MongoDB has several pitfalls that you need to be aware of for your day to day operations. MongoDB, at a high level, supports three techniques to build indexes on your collections

1. Foreground Index Build

When you build an index in the foreground, it blocks all other operations on the database – on a large collection, this can be several hours. This implies that your database is down for the duration of the index build. Given that this is the default mode of building indexes, it is not surprising that a lot of developers shoot themselves in the foot triggering accidental index builds. There is really no good reason to trigger a foreground index build on a production server (unless you know that the collection has a small amount of data).

2. Background Index Build

As the name implies, the background indexing process builds the index in the background without affecting the availability of your database server. However, it is still a resource intensive operation and you should expect to see performance degradation. Also, since it is happening in the background, it can take a lot longer to build than the foreground indexes. In the previous versions of MongoDB (< 2.6), when you did a background index build on the primary of a replica set, it would run as “foreground” build on the secondary servers, thankfully it is no longer the case – it is background build on all the nodes. If a background index build is interrupted, it will resume as a foreground index build on server restart.

3. Rolling Index Build

The rolling index build process builds the index on only one node at a time. It goes something like this:

  1. Rotate a secondary node out of the replica set (you can do this by changing ports or restarting in standalone mode).

  2. Build the index on this node in the foreground. Once the index is built, rotate the node back into the replica set.

  3. Once the node has caught up to the changes, move onto the next node. If the next node is the primary, you will need to do a rs.stepDown() to make it into a secondary.

  4. Rinse and repeat.

More details of the index build process are in the MongoDB documentation.

Using rolling index builds, you can build an index without any significant performance impact for your application. However there is failover involved — so your application should be able to handle that (which it needs to anyways).

Can you do a rolling index build if you don’t have a replica set? Unfortunately for standalone instances, the only option is a “Background index build.”

The rolling index build is our favored approach to building indexes at ScaleGrid. We even provide a UI and make it easy for you to kick off the whole process from our UI. Our backend will do all the orchestration necessary for the full index build – it will trigger a server by server index build. You just need to point and click!

Mongodb create index

As always, if you have further questions you can reach out to us at support@scalegrid.io

Build (game engine) MongoDB

Published at DZone with permission of Dharshan Rangegowda, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • How to Build Real-Time BI Systems: Architecture, Code, and Best Practices
  • How To Build Resilient Microservices Using Circuit Breakers and Retries: A Developer’s Guide To Surviving
  • Accelerating Debugging in Integration Testing: An Efficient Search-Based Workflow for Impact Localization
  • Loading XML into MongoDB

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!