Write concern levels (w: 1, majority) in MongoDB - Time & Space Complexity
When we write data to MongoDB, the database confirms the write in different ways.
We want to understand how the time to confirm a write changes with different write concern levels.
Analyze the time complexity of these write operations with different write concerns.
// Write with w:1 (acknowledge from primary only)
db.collection.insertOne({name: "Alice"}, {writeConcern: {w: 1}})
// Write with w: "majority" (acknowledge from majority of nodes)
db.collection.insertOne({name: "Bob"}, {writeConcern: {w: "majority"}})
This code writes a document and waits for confirmation from either the primary node or the majority of nodes.
Look at what happens behind the scenes when confirming writes.
- Primary operation: Waiting for acknowledgments from nodes.
- How many times: For w:1, once from primary; for w: "majority", from multiple nodes.
The time to confirm a write depends on how many nodes must respond.
| Input Size (n) | Approx. Operations |
|---|---|
| 3 nodes | w:1 waits for 1 response; w:majority waits for 2 responses |
| 5 nodes | w:1 waits for 1 response; w:majority waits for 3 responses |
| 7 nodes | w:1 waits for 1 response; w:majority waits for 4 responses |
Pattern observation: w:1 time stays the same; w:majority time grows roughly with half the number of nodes.
Time Complexity: O(w)
This means the time to confirm a write grows with the number of nodes that must acknowledge it.
[X] Wrong: "Waiting for majority is always the same speed as waiting for one node."
[OK] Correct: Majority requires more nodes to respond, so it usually takes longer than just one node.
Understanding how write concern affects response time helps you explain trade-offs between speed and data safety in real systems.
"What if we changed write concern to w: 0 (no acknowledgment)? How would the time complexity change?"
Practice
w: 1 mean in MongoDB?Solution
Step 1: Understand write concern
This means the write operation waits for acknowledgment from only one server.w: 1Step 2: Compare with other write concerns
w: majoritywaits for most servers, sow: 1is less strict and faster.Final Answer:
The write is acknowledged by only one server before returning success. -> Option BQuick Check:
Write concernw: 1= one server acknowledgment [OK]
- Confusing w:1 with w:majority
- Thinking w:1 waits for all servers
- Assuming w:1 means no acknowledgment
Solution
Step 1: Identify correct syntax for string values
Write concern values like 'majority' must be strings enclosed in quotes.Step 2: Check each option
{ writeConcern: { w: 'majority' } } uses quotes correctly. { writeConcern: { w: majority } } misses quotes. { writeConcern: { w: majority'' } } has syntax error with extra quotes. { writeConcern: { w: "1" } } sets w to string '1', not 'majority'.Final Answer:
{ writeConcern: { w: 'majority' } } -> Option DQuick Check:
Use quotes for string write concern values [OK]
- Omitting quotes around 'majority'
- Using invalid quotes or syntax
- Confusing w:1 string with w:'majority'
db.collection.insertOne({name: 'Alice'}, {writeConcern: {w: 'majority'}})What does this guarantee about the write?
Solution
Step 1: Understand
This means the write waits for acknowledgment from most members of the replica set.w: 'majority'write concernStep 2: Compare with other options
The write is confirmed by at least one server before success. isw:1. The write is not confirmed by any server before success. means no acknowledgment. The write is confirmed by all servers including secondaries and arbiters. is incorrect because arbiters do not store data and are not counted.Final Answer:
The write is confirmed by the majority of replica set members before success. -> Option AQuick Check:
w: 'majority'means majority confirmation [OK]
- Thinking arbiters count towards majority
- Confusing majority with all servers
- Assuming w:majority is same as w:1
db.collection.insertOne({item: 'book'}, {writeConcern: {w: majority}})But it throws an error. What is the problem?
Solution
Step 1: Check syntax of write concern value
The value majority must be a string, so it needs quotes: 'majority'.Step 2: Verify other parts
writeConcern is spelled correctly, insertOne accepts writeConcern, and document is valid.Final Answer:
The write concern value majority must be a string in quotes. -> Option AQuick Check:
Write concern strings need quotes [OK]
- Forgetting quotes around 'majority'
- Assuming writeConcern is misspelled
- Thinking insertOne disallows writeConcern
Solution
Step 1: Understand the trade-offs
w: 1is fast but less safe.w: majoritywaits for most servers, giving good safety without waiting for all.Step 2: Evaluate options
w: allwaits for every server, which can be slow.w: 0is unsafe as it doesn't wait for acknowledgment.Final Answer:
Usew: majoritybecause it waits for most servers, balancing safety and speed. -> Option CQuick Check:
Majority write concern balances safety and speed [OK]
- Choosing w:1 for safety
- Assuming w:all is always best
- Ignoring performance impact of write concern
Ready to prove it?
Pass the test and this pattern moves unknown
