MongoDB MongoDB Transactions and ACID Compliance 2 — Questions and Answers
Question 1: What is a transient transaction error in MongoDB and how should it be handled?
- An error that permanently invalidates the transaction; application must abort
- A temporary error (e.g., write conflict or network issue) that can be resolved by retrying the entire transaction (Correct answer)
- An error that commits a partial transaction
- An error caused by schema validation failure within the transaction
Correct answer: A temporary error (e.g., write conflict or network issue) that can be resolved by retrying the entire transaction
Transient transaction errors (ErrorLabel: 'TransientTransactionError') indicate temporary conditions like write conflicts or network issues. The application should catch these errors and retry the entire transaction from the beginning, as the original transaction was rolled back.
Question 2: What is a write conflict in a MongoDB transaction?
- A conflict between two schema validation rules
- When two concurrent transactions attempt to modify the same document simultaneously (Correct answer)
- A conflict between a write and a read in the same transaction
- When a transaction exceeds the oplog window
Correct answer: When two concurrent transactions attempt to modify the same document simultaneously
A write conflict occurs when two concurrent transactions attempt to write to the same document. MongoDB detects this and aborts one of the conflicting transactions with a TransientTransactionError, expecting the application to retry the aborted transaction.
Question 3: Which of the following operations CANNOT be performed inside a MongoDB multi-document transaction?
- Inserting documents into multiple collections
- Updating documents across different collections
- Creating a new collection or index (Correct answer)
- Deleting documents from a sharded collection
Correct answer: Creating a new collection or index
DDL operations such as creating collections, dropping collections, or creating indexes cannot be performed inside a multi-document transaction. Transactions are limited to DML operations (insert, update, delete, find) on existing collections.
Question 4: What is the default transaction timeout limit in MongoDB?
- 30 seconds
- 60 seconds (Correct answer)
- 120 seconds
- 300 seconds
Correct answer: 60 seconds
MongoDB transactions have a default timeout of 60 seconds (configurable via transactionLifetimeLimitSeconds). Transactions running longer than this limit are automatically aborted. Long-running transactions should be avoided as they hold locks and impact performance.
Question 5: In MongoDB, single-document operations are atomic by default. What does this mean?
- Single documents are replicated atomically across all secondaries
- An update to a single document either fully succeeds or fully fails — partial updates never occur (Correct answer)
- Single document reads are always consistent with the latest write
- Single documents cannot be deleted and updated simultaneously
Correct answer: An update to a single document either fully succeeds or fully fails — partial updates never occur
MongoDB guarantees atomicity at the single-document level without needing transactions. An operation modifying a single document (including updating multiple fields or embedded arrays) either applies all changes or none — observers never see a partial update.
Question 6: What is the impact of using transactions on MongoDB performance?
- Transactions always improve performance by batching operations
- Transactions introduce overhead from lock management and oplog entries, reducing throughput compared to non-transactional operations (Correct answer)
- Transactions have no performance impact on replica sets
- Transactions only impact performance on sharded clusters
Correct answer: Transactions introduce overhead from lock management and oplog entries, reducing throughput compared to non-transactional operations
Multi-document transactions in MongoDB introduce overhead: they hold locks, create larger oplog entries, and require coordination across members. MongoDB documentation recommends using the document model to avoid transactions where possible and to keep transactions short when they are necessary.
What is a transient transaction error in MongoDB and how should it be handled?