What is a replica set in MongoDB?
Why Interviewers Ask This
Interviewers use this question to quickly assess whether a candidate has the foundational knowledge required for MongoDB development. It reveals whether you understand the building blocks that more complex concepts rely on.
Answer
A replica set is a group of MongoDB servers that maintain the same dataset, providing redundancy and high availability. A replica set consists of: (1) Primary: the single node that receives all write operations; (2) Secondaries (1+): maintain copies of the primary's data by asynchronously applying operations from the primary's oplog; (3) Arbiter (optional): participates in elections but holds no data — used when you want an odd number of voting members for elections without adding a full data node. Oplog (Operations Log): a special capped collection on the primary that records all data modification operations. Secondaries tail the oplog to stay in sync. Automatic failover: if the primary becomes unreachable, the remaining replica set members hold an election to choose a new primary. The election uses the Raft-based protocol; the new primary is chosen within ~10-30 seconds. Read preferences: by default, reads go to the primary. Configure read preference to use secondaries: primary, primaryPreferred, secondary, secondaryPreferred, nearest. Secondary reads may be slightly stale (replication lag). Write concern: w: "majority" — write acknowledged by majority of replica set members. Ensures durability even if primary fails after the write. Minimum recommended replica set: 3 members (one primary, two secondaries) — tolerates one failure and can elect a new primary with the remaining two (majority of 3). Replica sets are the foundation for MongoDB Atlas clusters.
Pro Tip
Back up your answer with a specific project or situation. Saying 'In my last MongoDB project, I used this when...' immediately makes your answer more credible and memorable.