skip to Main Content

I am working on a Spring application which has an existing mongo collection with a huge amount of data.
I need to create an index on that collection. I will use Mongobee/Mongock like migration framework to create the index.
I want to know that will this index creation affect the duration of Spring application’s deployment? What if I set the background property as true for index creation ?

Basically, my desired scenario would be that :-

  • application’s deployment should not affected by the index creation in any way
  • index creation should happen in background and meanwhile mongo should be able to serve queries on that collection

2

Answers


  1. Please always be sure to include the version that you are running when asking questions like this. There are often important behavioral changes between the versions that are relevant for answers.

    Generally speaking, background index creation does what you are looking for. Which is to say that such index builds do not lock the collection and allow the application to continue functioning while the index build is in progress.

    That said, the concepts of foreground and background index builds no longer exist. As of 4.2 all index builds are effectively done in the background (ignoring the background argument if provided when issuing the command to create the index). You can read more about that here.

    It may also be worth mentioning that you may also choose to build indexes in a rolling manner on a replica set. Clusters in Atlas can choose to use this automatically or you can perform this technique manually otherwise. It is uncommon for this to provide much benefit though, particularly since the new index build method was introduced in version 4.2.

    Login or Signup to reply.
  2. while @user20042973’s answer is right and very useful, it only applies to MongoDB.

    However, If I am not wrong, you’re also worry about Mongock’s behaviour and how it may affect your deployment.

    What @user20042973 explained above, combined with the use of runner-type: applicationrunner in Mongock, will provide what you are looking for:

    • Application starts and serves requests, without waiting for Mongock to finish.
    • MongoDB is available while building the index(for MongoDB version +=4.2)
    • The deployment of all the instances of your service will start and serve without waiting for the Mongock’s lock(not to be confused with MongoDB’s lock).

    However, it is worth mentioning the following:

    1. You mention the mongock configuration property indexCreation. Well, nothing to do here, it’s for the internal Mongock’s structure. This is for uses cases where the application doesn’t have rights to create the indexes.
    2. If the ChangeUnit fails creating the index and throws an exception, the application is aborted.
    3. If, instead of using runner-type: applicationrunner, you use runner-type: initializingbean, you get the opposite behaviour. Your application won’t start until Mongock finishes
    4. runner-type: applicationrunner is the default
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search