MongoDB.live, free & fully virtual. June 9th - 10th. Register Now MongoDB.live, free & fully virtual. June 9th - 10th. Register Now

TTL index internals

I have some questions about ttl based indexes

  1. Does mongod create 1 thread for all the collections with ttl index enabled or is it 1 per collection
  2. What happens when the this thread created by mongod doesn’t exit in 60 seconds, does a new thread gets created after 60 seconds, if yes, then I guess doesn’t it mean that ttl based index should not be used for write heavy systems?
  3. Can we change the frequency of this thread, make it run every hour or day instead?
  4. Where is the scheduler running to schedule this thread, does it always run, or it gets created when any ttl based index is added?

Here, is the information I have received over chat
1)
Does mongod create 1 thread for all the collections with ttl index enabled or is it 1 per collection

MongoDB index creation is not Multi- Threaded .

You can try increasing the maxIndexBuildMemoryUsageMegabytes parameter value.

The default value is 500 MB.

What does it do?

Limits the amount of memory that the simultaneous foreground index builds on one collection may consume for the duration of the builds.

So by increasing this limit, may increase the performance of index creation. For Example:

db.adminCommand( { setParameter: 1, maxIndexBuildMemoryUsageMegabytes: 70000 } )
2) What happens when the this thread created by mongod doesn't exit in 60 seconds, does a new thread gets created after 60 seconds, if yes, then I guess doesn't it mean that ttl based index should not be used for write heavy systems?

It looks like it will go beyond the 60seconds until it deletes the doc.

By Default, the TTLMonitor thread runs once in every 60 seconds. You can find out the sleep interval using following admin command.

||> db.adminCommand({getParameter:1, ttlMonitorSleepSecs: 1});

{ “ttlMonitorSleepSecs” : 60, “ok” : 1 }

To change this interval, supply another admin command with the desired interval:

||> db.adminCommand({setParameter:1, ttlMonitorSleepSecs: 3600}); // 1 hour
{ “was” : 60, “ok” : 1 }

Only for 4th Question I seek answer

Welcome to the community @Prateek_GUpta,

It looks like you already have answers to most of your questions aside from #4. Your comment for #1 is also related to index creation, not the TTL background thread.

There is only a single TTLMonitor thread, which wakes up every 60 seconds by default and iterates TTL indexes to find expired documents to remove.

The TTL background thread is enabled by default on all data-bearing mongod instances. The TTL background thread will be idle for a replica set member unless it is currently in Primary state, since documents can only be directly deleted on a Primary. Secondary members apply delete operations via replication so they are consistent with the TTL expiry outcome on the Primary.

Regards,
Stennie

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.