Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How often does MongoDB check its index 'expireAfterSeconds'?

I use a single instance MongoDB locally and in a Spring Boot Repository, I annotate a document-class with this annotation:

@Indexed(name = "deleteAt", expireAfterSeconds = 0)
private LocalDateTime deleteAt;

As I read from the docs here, documents should automatically get deleted at the specified DateTime. It works mostly but I see a delay in deletion when I poll the same document frequently. Most documents were deleted immediately, yet some remain in the DB for up to 30 seconds after the specified deletion time.

That makes me wonder whether MongoDB runs a scheduler to clean up such documents and if so, how often does it run?

like image 921
user3105453 Avatar asked Jan 09 '18 08:01

user3105453


People also ask

Does MongoDB auto index?

Each collection in MongoDB automatically has an index on the _id field. This index can then be used to fetch documents from the database efficiently. However, you will need to query data on other specific fields most of the time.

Does MongoDB index every field?

MongoDB provides complete support for indexes on any field in a collection of documents. By default, all collections have an index on the _id field, and applications and users may add additional indexes to support important queries and operations. This document describes ascending/descending indexes on a single field.

Does MongoDB $in use index?

MongoDB uses indexing in order to make the query processing more efficient. If there is no indexing, then the MongoDB must scan every document in the collection and retrieve only those documents that match the query.


1 Answers

Spring Data's expireAfterSeconds attribute engages a MongoDB TTL index and the behaviour of a TTL index's expireAfterSeconds attribute is described in the docs as follows:

When you build a TTL index in the background, the TTL thread can begin deleting documents while the index is building. If you build a TTL index in the foreground, MongoDB begins removing expired documents as soon as the index finishes building.

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

The background task that removes expired documents runs every 60 seconds. As a result, documents may remain in a collection during the period between the expiration of the document and the running of the background task.

Because the duration of the removal operation depends on the workload of your mongod instance, expired data may exist for some time beyond the 60 second period between runs of the background task.

So, the answer is: 60 seconds though the expired data may live for longer than 60s because "the duration of the removal operation depends on the workload of your mongod instance ...".

like image 85
glytching Avatar answered Sep 21 '22 13:09

glytching