MongoDB MongoDB Replication and Sharding 1 — Questions and Answers
Question 1: What is a replica set in MongoDB?
- A set of identical database schema templates
- A group of mongod instances maintaining the same data set for redundancy (Correct answer)
- A set of backup files taken at the same time
- A collection of replicated index definitions
Correct answer: A group of mongod instances maintaining the same data set for redundancy
A replica set is a group of MongoDB instances that maintain the same data, providing high availability through automatic failover.
Question 2: What is the role of the primary node in a MongoDB replica set?
- Stores the primary (most important) documents
- Receives all write operations and records changes to the oplog (Correct answer)
- Handles only read operations from the application
- Acts as the configuration server for sharding
Correct answer: Receives all write operations and records changes to the oplog
The primary is the only member that receives write operations; it records all changes in its oplog, which secondaries replicate.
Question 3: How many votes are needed for a primary election in a MongoDB replica set?
- A unanimous vote from all members
- A simple majority of all voting members (Correct answer)
- At least two members must vote
- The member with the highest priority wins automatically
Correct answer: A simple majority of all voting members
A replica set member wins a primary election by receiving votes from a majority of all voting members, preventing split-brain scenarios.
Question 4: What is an arbiter in a MongoDB replica set?
- A member that arbitrates between conflicting writes
- A voting member that holds no data and only participates in elections (Correct answer)
- A read-only member for reporting purposes
- A member that arbitrates between multiple primaries
Correct answer: A voting member that holds no data and only participates in elections
An arbiter participates in primary elections to maintain an odd number of votes but does not replicate data, using minimal resources.
Question 5: What is the oplog in MongoDB replication?
- An operations performance log for monitoring
- A capped collection that records all data-modifying operations on the primary (Correct answer)
- An optimizer log for query plans
- An operational alert log for system events
Correct answer: A capped collection that records all data-modifying operations on the primary
The oplog (operations log) is a special capped collection on the primary that records every write operation; secondaries replicate these entries to stay synchronized.
Question 6: What write concern { w: 'majority' } ensures in MongoDB?
- Only the primary acknowledges the write
- A majority of voting replica set members have acknowledged the write (Correct answer)
- All replica set members have written the data to disk
- The write is replicated to at least two members
Correct answer: A majority of voting replica set members have acknowledged the write
w: 'majority' means the write is acknowledged only after a majority of voting members have applied it, ensuring durability even if the primary fails.
What is a replica set in MongoDB?