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
No Downtime: One of the most significant advantages is that it allows the database to continue functioning without any downtime during the index creation.
Operational Flexibility: You can build indexes on large collections without affecting the performance and availability of your application.
Batch Processing: Background indexing processes the documents in smaller batches, reducing the lock time on the database.
Considerations and Trade-offs
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.
Resource Consumption: Background indexing consumes more system resources like CPU and memory, as it has to manage both regular database operations and index building.
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.
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.
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
Automatic Deletion: Documents will be automatically deleted when the time specified in the TTL index is reached.
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.
Custom Expiration: You can set custom expiration times on a per-document basis by setting the indexed date field to a future date/time.
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
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.
No Support for Sharding: As of MongoDB 4.4, TTL indexes are not supported for sharded collections.
Single Field: Only one field can be set with
expireAfterSeconds
in a TTL index.No Updates on TTL Field: If you update the value of the TTL field, MongoDB will use the new value to determine expiration.
Resource Utilization: The background task that removes expired documents can impact performance, especially if a large number of documents expire simultaneously.
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.