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
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
andbackground
index builds no longer exist. As of4.2
all index builds are effectively done in the background (ignoring thebackground
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
.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:However, it is worth mentioning the following:
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.runner-type: applicationrunner
, you userunner-type: initializingbean
, you get the opposite behaviour. Your application won’t start until Mongock finishesrunner-type: applicationrunner
is the default