Drop/Create MongoDB TTL Index vs. collMod in Production

mongodb

I'm working with a MongoDB collection containing over 250 million documents that has an existing TTL index and is written to with a pretty high frequency. I'd like to reduce the TTL value for the index from 60 days to 30 days, while minimizing production impact. It seems that I have two options: dropping and recreating the index in the background or running the collMod command to modify the index. The simplicity of the collMod command is appealing, but I'm concerned about the impact it might have, especially since there appears to be no way to run it in the background. Would dropping/recreating the index lead to less lock contention and have less of an impact?

Best Answer

The first you need to do is calculate how many documents the index will delete if you change the 60 days into 30. That is a simple count with {TTL_FIELD: $gt:30days}.

If the number of documents is big (how "big" depends from the server specs and the workload) I recommend to run an incremental Colmod like (pseudocode):

While achieve 30 days {

NUM = 60 days;

NUM = NUM - threshold;

db.runCommand( {"collMod" : "COLLLECTION" , "index" : { "keyPattern" : {TTL_FIELD : 1} , "expireAfterSeconds" : NUM } } )

wait 2 minutes;}

If the number of deleted document is small just do the collMod at once or on 2-3 steps manually.

Your approach to drop and recreate the index takes more time and if you have lot of documents for deletion you will get performance degradation when the index completes.

Hope that helps