Error
Error Code:
290
MongoDB Error 290: Transaction Lifetime Exceeded
Description
This error indicates that a multi-document transaction has exceeded its configured `transactionLifetimeLimitSeconds` and was automatically aborted by MongoDB. It commonly occurs when a transaction's operations take too long to complete or when the lifetime limit is set too low for the application's workload, ensuring resources are not tied up indefinitely.
Error Message
Transaction Exceeded Lifetime Limit Seconds
Known Causes
3 known causesLong Transaction Execution
Operations within the transaction took longer than the configured `transactionLifetimeLimitSeconds`, possibly due to complex queries or large data modifications.
Insufficient Lifetime Limit
The `transactionLifetimeLimitSeconds` setting is too short for the typical duration of transactions required by your application's workload.
Database Performance Bottlenecks
Slow disk I/O, high CPU utilization, or network latency can prolong transaction execution, causing them to exceed the lifetime limit.
Solutions
3 solutions available1. Increase Transaction Lifetime Limit medium
Adjust the MongoDB configuration to allow longer-running transactions.
1
Locate your MongoDB configuration file. This is typically `mongod.conf` or `mongod.yaml`.
2
Add or modify the `transactionLifetimeLimitSeconds` parameter within the `setParameter` section of your configuration. Set it to a value higher than your longest expected transaction. For example, to set it to 1 hour (3600 seconds):
setParameter:
transactionLifetimeLimitSeconds: 3600
3
If you are using a YAML configuration file, ensure the indentation is correct.
4
Restart the `mongod` service for the changes to take effect.
# For systemd-based systems (e.g., Ubuntu, CentOS 7+)
sudo systemctl restart mongod
5
Alternatively, for older systems or different init systems, use the appropriate command, e.g.:
# For sysvinit-based systems (e.g., older Ubuntu/Debian)
sudo service mongod restart
2. Optimize Transaction Logic medium
Refactor your application code to reduce the duration of transactions.
1
Identify the specific transactions in your application that are exceeding the lifetime limit. This might involve logging transaction start and end times in your application code.
2
Analyze the operations within these long-running transactions. Look for opportunities to break them down into smaller, independent transactions.
3
Consider moving non-essential operations (e.g., complex calculations, external API calls, sending notifications) outside of the transaction. These operations do not need to be atomic and can be performed before or after the transaction commits.
4
If a transaction involves reading a large amount of data, try to fetch only the necessary fields and filter data efficiently. Use projection and appropriate query operators.
// Example of efficient read within a transaction (conceptual)
db.collection.findOne({ _id: 'someId' }, { field1: 1, field2: 1 })
5
If the transaction involves multiple steps that can be executed independently, explore using separate transactions for each step, and handle potential failures gracefully.
3. Implement Transaction Timeouts in Application Code medium
Manually enforce transaction timeouts within your application to prevent exceeding MongoDB's limits.
1
When starting a transaction in your application, record the start time.
const startTime = new Date();
2
Before performing any operation within the transaction, check if the elapsed time has exceeded a predefined acceptable limit (e.g., slightly less than MongoDB's `transactionLifetimeLimitSeconds`).
const maxTransactionDuration = 50000; // 50 seconds, adjust as needed
if (new Date() - startTime > maxTransactionDuration) {
throw new Error('Transaction timed out');
}
3
If the timeout is reached, abort the transaction and handle the error appropriately in your application logic.
await session.abortTransaction();
4
Ensure your application code is robust enough to handle these timeouts gracefully, perhaps by retrying the operation or informing the user.