MongoDB MongoDB Replication and Replica Sets 2 — Questions and Answers
Question 1: What is a hidden replica set member used for?
- Breaking election ties with an extra vote
- Serving as a dedicated reporting or analytics node invisible to client applications (Correct answer)
- Providing geographic redundancy as a primary candidate
- Storing only indexes, not document data
Correct answer: Serving as a dedicated reporting or analytics node invisible to client applications
Hidden members maintain a copy of the primary's data but are invisible to client read preferences. They can hold votes but have priority 0 (cannot become primary). Common use cases include dedicated reporting, analytics, or backup nodes that must not receive application traffic.
Question 2: What is replication lag in a MongoDB replica set?
- The time difference between oplog entries on the primary and their application on a secondary (Correct answer)
- The delay in write acknowledgment due to network latency
- The time taken to complete an election
- The difference in document counts between primary and secondary
Correct answer: The time difference between oplog entries on the primary and their application on a secondary
Replication lag is the time delay between when an operation is written to the primary's oplog and when it is applied on a secondary. High replication lag can cause stale reads from secondaries and risks the secondary falling off the oplog if lag exceeds the oplog window.
Question 3: What read preference should be used to always read from the primary in MongoDB?
- primaryPreferred
- secondary
- primary (Correct answer)
- nearest
Correct answer: primary
The 'primary' read preference (the default) directs all read operations to the primary member, ensuring the most up-to-date data. It is the only read preference that guarantees reading the latest committed writes at the cost of increased primary load.
Question 4: A delayed replica set member with a delay of 1 hour is useful for:
- Improving read performance in disaster recovery scenarios
- Providing a point-in-time recovery window to recover from accidental data deletions (Correct answer)
- Serving as a tiebreaker in elections
- Reducing primary write latency
Correct answer: Providing a point-in-time recovery window to recover from accidental data deletions
A delayed secondary applies oplog entries with a configurable delay (e.g., 1 hour). This creates a rolling backup window. If a developer accidentally drops a collection, the delayed member provides a recovery point before the destructive operation was applied.
Question 5: What does rs.status() return in the MongoDB shell?
- A list of all databases in the replica set
- The current status of all replica set members including health, state, and replication lag (Correct answer)
- The oplog size and usage statistics
- A summary of active connections to each member
Correct answer: The current status of all replica set members including health, state, and replication lag
rs.status() returns detailed information about each replica set member, including its state (PRIMARY, SECONDARY, ARBITER), health, uptime, last heartbeat time, and optionally replication lag (optimeDate difference from primary). It is the primary diagnostic command for replica sets.
Question 6: Which write concern provides the lowest durability but the highest write throughput in MongoDB?
- { w: 'majority' }
- { w: 1 }
- { w: 0 } (Correct answer)
- { w: 'all' }
Correct answer: { w: 0 }
{ w: 0 } is 'fire and forget' — MongoDB does not wait for any acknowledgment before returning to the client. This gives maximum throughput but offers no durability guarantee. The write may be lost if the server crashes before it is persisted.
What is a hidden replica set member used for?