Reduce/chance Mongodb TTL time (60 seconds) in java

Hello, I would like to change mongo db ttl time which is 60 seconds. How can I do it in java?
Thank you in advance

Welcome to the MongoDB Community @seda_tankiz !

I assume you are referring to how often the Time-To-Live (TTL) Monitor thread checks to see if there are any matching documents to remove, which is every 60 seconds by default.

The TTL sleep interval is controlled by a global ttlMonitorSleepSecs parameter that can be changed via the setParameter administrative command or passed as a configuration option when starting mongod.

The MongoDB shell command would be:

db.adminCommand({setParameter:1, ttlMonitorSleepSecs:60})

The MongoDB Java driver documentation includes an example of running a command, which you should be able to adapt: Run a Command (Java Driver). Administrative commands must be run in the admin database, so replace sample_mflix with admin in this code example.

Regards,
Stennie

Thank you Stennie,
As you got the topic it is about MongoDB thread monitoring time.
I would like to run below code for component test. For the component test I would like to sleep(at most 30 sec not 1 minute) and check if expired documents are removed or not. But it doesnt work. If I run thread.sleep(60000) (1 minute) It works but it is too much delay for a component test. Do you have any solution for this?
‘’’
runCommand(new BsonDocument(“setParameter”, new BsonInt32(1)).append(“ttlMonitorEnabled”, new BsonBoolean(false)));
runCommand(new BsonDocument(“setParameter”, new BsonInt32(1)).append(“ttlMonitorSleepSecs”, new BsonInt64(5)));
runCommand(new BsonDocument(“setParameter”, new BsonInt32(1)).append(“ttlMonitorEnabled”, new BsonBoolean(true)));
Thread.sleep(15000);
long afterCount = getCounter(TrainedDbModel.class);

// then
assertThat(afterCount).isEqualTo(1);
public void runCommand(final Bson command) {
final MongoDatabase db = mongoClient.getDatabase(“admin”);
db.runCommand(command);
}
‘’’

Hi,
Is there any solution for my problem explained above?
Thanks,
Seda

Hi @seda_tankiz

Even if you set the TTL thread timing (say, to 30 seconds), there is no guarantee that the documents will be removed exactly 30 seconds later. This is mentioned in https://www.mongodb.com/docs/manual/core/index-ttl/#timing-of-the-delete-operation

The TTL index does not guarantee that expired data will be deleted immediately upon expiration. There may be a delay between the time that a document expires and the time that MongoDB removes the document from the database.

The TTL feature is heavily tested in the server codebase, so in my opinion, you don’t have much value in testing this feature yourself. Although we can never prove the absence of bugs, I believe it’s reasonable from your point of view to assume that the TTL index will do its job as expected. Although I would recommend you to keep upgrading to the latest supported versions for bugfixes and improvements :slight_smile:

Hope this answers your question!

Best regards
Kevin

1 Like