Read concern and write concern in transactions in MongoDB - Time & Space Complexity
When using transactions in MongoDB, read and write concerns control how data is read and saved safely.
We want to understand how the time to complete a transaction changes as data size grows.
Analyze the time complexity of this transaction with read and write concerns.
const session = client.startSession();
session.startTransaction({
readConcern: { level: 'snapshot' },
writeConcern: { w: 'majority' }
});
const docs = await collection.find({ status: 'active' }, { session }).toArray();
await collection.updateMany({ status: 'active' }, { $set: { processed: true } }, { session });
await session.commitTransaction();
session.endSession();
This code reads active documents and updates them inside a transaction with specific read and write guarantees.
Look for repeated work that grows with input size.
- Primary operation: Scanning documents matching the filter and updating them.
- How many times: Once for each matching document in the collection.
As the number of active documents grows, the time to read and update them grows too.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 reads and 1 update |
| 100 | About 100 reads and 1 update |
| 1000 | About 1000 reads and 1 update |
Pattern observation: The work grows roughly in direct proportion to the number of matching documents.
Time Complexity: O(n)
This means the time to complete the transaction grows linearly with the number of documents processed.
[X] Wrong: "Transactions always take the same time regardless of data size because they run as one unit."
[OK] Correct: The transaction time depends on how many documents are read and written; more data means more work and longer time.
Understanding how transaction time grows helps you design better database operations and explain your choices clearly in discussions.
What if we changed the read concern level from 'snapshot' to 'local'? How would the time complexity change?
Practice
readConcern control in a MongoDB transaction?Solution
Step 1: Understand the role of readConcern
Read concern defines the level of isolation and freshness of data read during a transaction.Step 2: Differentiate from writeConcern
Write concern controls confirmation of writes, not reads.Final Answer:
How fresh or confirmed the data you read is during the transaction -> Option AQuick Check:
readConcern = data freshness [OK]
- Confusing readConcern with writeConcern
- Thinking readConcern controls write confirmations
- Assuming readConcern affects transaction speed
writeConcern in a MongoDB transaction?Solution
Step 1: Recall writeConcern syntax
Write concern uses the keywto specify acknowledgement level, e.g., 'majority'.Step 2: Check options for correctness
{ writeConcern: { w: 'majority' } } correctly useswriteConcern: { w: 'majority' }. Others misuse keys or mix readConcern and writeConcern.Final Answer:
{ writeConcern: { w: 'majority' } } -> Option AQuick Check:
writeConcern uses w key [OK]
- Using 'r' instead of 'w' for writeConcern
- Mixing readConcern and writeConcern keys
- Passing writeConcern as a string instead of object
const session = client.startSession();
session.startTransaction({ readConcern: { level: 'snapshot' }, writeConcern: { w: 'majority' } });
// transaction operations
await session.commitTransaction();Solution
Step 1: Identify readConcern in transaction options
The transaction is started withreadConcern: { level: 'snapshot' }.Step 2: Understand readConcern levels
'snapshot' means the transaction reads a consistent snapshot of data at start time.Final Answer:
snapshot -> Option DQuick Check:
readConcern level = snapshot [OK]
- Confusing writeConcern level with readConcern level
- Assuming default readConcern instead of specified
- Mixing up 'majority' with readConcern levels
const session = client.startSession();
session.startTransaction({ readConcern: { level: 'majority' }, writeConcern: { w: 1 } });
// transaction operations
await session.commitTransaction();Solution
Step 1: Check for session lifecycle management
The code does not callsession.endSession()aftercommitTransaction(). This is necessary to properly release session resources and avoid leaks.Step 2: Verify other components
readConcern level 'majority' is valid; writeConcern{ w: 1 }is valid (acknowledgement from primary); syntax is correct.Final Answer:
Missing session.endSession() call -> Option BQuick Check:
endSession after commit/abort [OK]
- Thinking readConcern 'majority' is invalid for transactions
- Assuming writeConcern w:1 is invalid
- Believing session.endSession() is optional
Solution
Step 1: Understand readConcern 'majority'
This level reads data confirmed by a majority of servers, ensuring recent confirmed data.Step 2: Understand writeConcern { w: 'majority' }
This ensures writes are acknowledged by a majority of servers before commit.Step 3: Evaluate other options
readConcern: 'local', writeConcern: { w: 1 } uses weaker writeConcern; C uses no write acknowledgement; D uses linearizable readConcern which is stricter than needed.Final Answer:
readConcern: 'majority', writeConcern: { w: 'majority' } -> Option CQuick Check:
Majority read and write concern = readConcern: 'majority', writeConcern: { w: 'majority' } [OK]
- Choosing writeConcern w:1 for majority confirmation
- Confusing 'snapshot' with 'majority' readConcern
- Ignoring writeConcern w:0 means no acknowledgement
Ready to prove it?
Pass the test and this pattern moves unknown
