Skip to main content

Advanced concepts

Background indexes

In MongoDB, when you create an index, the operation is executed in the foreground by default. This means that the index creation process will block all other operations on the database until it's completed. For large collections, this can result in a significant downtime, affecting the availability of your application.

The background option allows you to build the index in the background, enabling other database operations to continue while the index is being built. This is particularly useful for production environments where you can't afford to block database operations.

How to Create a Background Index

You can create a background index using the createIndex method with the background option set to true.

// Create a background index on the "username" field
db.users.createIndex({ "username": 1 }, { background: true })

Advantages of Background Indexing

  1. No Downtime: One of the most significant advantages is that it allows the database to continue functioning without any downtime during the index creation.

  2. Operational Flexibility: You can build indexes on large collections without affecting the performance and availability of your application.

  3. Batch Processing: Background indexing processes the documents in smaller batches, reducing the lock time on the database.

Considerations and Trade-offs

  1. Slower Build: Building an index in the background is generally slower than building it in the foreground because the database continues to handle other read and write operations simultaneously.

  2. Resource Consumption: Background indexing consumes more system resources like CPU and memory, as it has to manage both regular database operations and index building.

  3. Replica Sets: In a replica set, the background index build is propagated to secondaries as a foreground index build. This means that while the primary can still handle requests, the secondaries will be blocked.

  4. Consistency: Since other operations can proceed while the index is being built, there's a possibility that the index may include changes that were made to the data during its build process.

  5. Disk Usage: Background indexing might use more disk space as it has to maintain the existing index while building a new one.

TTL Index

A Time-To-Live (TTL) index in MongoDB is a special type of index that automatically removes documents from a collection after a certain amount of time or at a specific clock time. This is particularly useful for managing data that has a natural expiration time, such as session data, logs, or cached information.

How to Create a TTL Index

You can create a TTL index using the createIndex method and specifying the expireAfterSeconds option.

// Create a TTL index that will remove documents 3600 seconds (1 hour) after the "createdAt" field
db.sessions.createIndex({ "createdAt": 1 }, { expireAfterSeconds: 3600 })

Features of TTL Indexes

  1. Automatic Deletion: Documents will be automatically deleted when the time specified in the TTL index is reached.

  2. Date Field: The TTL index is usually created on a field that holds BSON date values. It can also be an array of date values.

  3. Custom Expiration: You can set custom expiration times on a per-document basis by setting the indexed date field to a future date/time.

  4. Compound Index Support: TTL indexes can be part of compound indexes as long as the TTL index field is the first in the order and the expireAfterSeconds option is specified.

Considerations and Limitations

  1. No Immediate Deletion: MongoDB's background task checks for expired documents every 60 seconds, so the actual deletion might not occur precisely at the expiration time.

  2. No Support for Sharding: As of MongoDB 4.4, TTL indexes are not supported for sharded collections.

  3. Single Field: Only one field can be set with expireAfterSeconds in a TTL index.

  4. No Updates on TTL Field: If you update the value of the TTL field, MongoDB will use the new value to determine expiration.

  5. Resource Utilization: The background task that removes expired documents can impact performance, especially if a large number of documents expire simultaneously.

  6. No Manual Deletion: While documents are automatically deleted, manual deletion or updates that should trigger immediate removal are not supported directly through the TTL index.