MongoDB MongoDB Replication and Replica Sets 1 — Questions and Answers
Question 1: What is the minimum recommended number of members in a MongoDB replica set for high availability?
- 1
- 2
- 3 (Correct answer)
- 5
Correct answer: 3
MongoDB recommends a minimum of 3 members for high availability. With 3 members, the replica set can tolerate the failure of one member while still maintaining a majority (2 of 3) required to elect a new primary and continue accepting writes.
Question 2: What is the role of the oplog in MongoDB replication?
- Logging slow queries for performance analysis
- A capped collection on the primary that records all write operations for secondaries to replicate (Correct answer)
- An index of all replica set member statuses
- A write-ahead log for crash recovery
Correct answer: A capped collection on the primary that records all write operations for secondaries to replicate
The oplog (operations log) is a special capped collection in the local database that records all write operations in an idempotent form. Secondary members tail the primary's oplog and replay operations to stay in sync with the primary.
Question 3: How does a MongoDB replica set elect a new primary?
- The oldest secondary automatically becomes primary
- An administrator manually promotes a secondary
- Members vote and a candidate must receive a majority of votes (Correct answer)
- The secondary with the largest oplog becomes primary
Correct answer: Members vote and a candidate must receive a majority of votes
MongoDB replica set elections use a majority voting protocol. A candidate primary must receive votes from a majority of all voting members (not just available members). This prevents split-brain scenarios in network partitions.
Question 4: What is a MongoDB arbiter in a replica set?
- A read-only secondary that serves analytics queries
- A member that holds no data but participates in elections to break ties (Correct answer)
- A delayed secondary used for backup purposes
- A hidden member that is invisible to client connections
Correct answer: A member that holds no data but participates in elections to break ties
An arbiter holds no data and cannot become primary. Its sole purpose is to vote in elections to break ties when an even number of data-bearing members are present. Arbiters use minimal resources but introduce a potential single point of failure.
Question 5: What is the write concern { w: 'majority' } in MongoDB?
- Writes are acknowledged after being written to the primary only
- Writes are acknowledged after being written to a majority of voting replica set members (Correct answer)
- Writes are replicated to all members before acknowledgment
- Writes are logged to disk but not replicated
Correct answer: Writes are acknowledged after being written to a majority of voting replica set members
{ w: 'majority' } ensures MongoDB acknowledges a write only after it has been written to a majority of the replica set's voting members and is durable. This guarantees the write survives the failure of any single member, providing strong durability guarantees.
Question 6: What happens during a replica set failover when the primary becomes unavailable?
- All writes are permanently lost
- The replica set becomes read-only until the primary recovers
- An election is held among eligible secondaries to elect a new primary (Correct answer)
- Client connections are redirected to a hidden secondary
Correct answer: An election is held among eligible secondaries to elect a new primary
When a primary becomes unavailable, eligible secondary members initiate an election. The secondary with the most up-to-date oplog and sufficient priority that can reach a majority of voting members will be elected as the new primary, typically within seconds.
What is the minimum recommended number of members in a MongoDB replica set for high availability?