Operation exceeded the transaction time limit
This error indicates that a transaction was automatically aborted by the server because it ran for longer than the `transactionLifetimeLimitSeconds` (default 60 seconds). This is a safeguard to prevent long-running transactions from holding resources indefinitely.
- 1A transaction contains one or more very slow operations (e.g., unindexed queries)
- 2The application pauses for a long time in the middle of a transaction (e.g., waiting for external API call)
- 3High server load or lock contention causes the operations within the transaction to take too long
- 4The `transactionLifetimeLimitSeconds` has been configured to a very small value
A transaction performs a write operation, then waits for more than 60 seconds before attempting another operation.
// Conceptual driver code
await session.withTransaction(async (session) => {
await collection.insertOne({ item: "book" }, { session });
// Wait for 70 seconds
await new Promise(resolve => setTimeout(resolve, 70000));
// This next operation will fail because the transaction has timed out.
await collection.updateOne({ item: "pen" }, { $inc: { qty: -1 } }, { session });
});expected output
MongoServerError: Transaction ... has been aborted.
Fix 1
Optimize Operations Within the Transaction
WHEN The transaction contains slow queries.
Why this works
The best solution is to make the operations inside the transaction faster. Ensure all queries are properly supported by indexes. Analyze the `explain()` plan for each operation.
Fix 2
Keep Transactions Short
WHEN The transaction scope is too large.
Why this works
A transaction should only contain the minimal set of operations that must be atomic. Do not perform long-running computations or external API calls inside a transaction. Prepare all data *before* starting the transaction.
Fix 3
Increase `transactionLifetimeLimitSeconds`
WHEN Transactions are optimized but still require more time (use with caution).
// This is a server-wide setting and must be changed in the config file or via setParameter. // mongod.conf // setParameter: // transactionLifetimeLimitSeconds: 120
Why this works
If absolutely necessary, a database administrator can increase this server-wide limit. This should be a last resort, as it can increase the risk of long-running transactions impacting overall system performance.
✕ Put user interaction loops or long-running tasks inside a transaction
Transactions are designed for short, fast, atomic database operations. Holding a transaction open while waiting for a user or an external system is a major anti-pattern that will lead to timeouts and lock contention.
mongodb/mongo src/mongo/base/error_codes.yml
Production Considerations for Transactions ↗Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev