What is a replica set in MongoDB and how does it provide high availability?
A MongoDB replica set keeps copies of your data across servers with a primary, secondaries, oplog replication and automatic failover for high availability.
Expected Interview Answer
A replica set is a group of MongoDB servers that maintain the same data, with one primary that accepts all writes and one or more secondaries that copy the primary's changes, providing redundancy and automatic failover.
The primary records every write in its operation log (oplog), and secondaries continuously replay that oplog to stay in sync. Members exchange heartbeats; if the primary becomes unreachable, the remaining members hold an election and a secondary is voted in as the new primary, so the cluster keeps serving writes without manual intervention. Applications can also route read queries to secondaries using a read preference to spread read load.
- Automatic failover with no manual promotion
- Data redundancy across multiple servers
- Read scaling by directing reads to secondaries
- Zero-downtime maintenance via rolling restarts
- Protection against single-node hardware failure
AI Mentor Explanation
A team never fields without a captain, but it also names a vice-captain and reserves ready to step up. If the captain is injured mid-match, the players quickly agree on a replacement so play continues. A replica set works this way: the primary is the captain making all the calls, secondaries are the vice-captain and reserves copying every decision, and if the captain leaves, an election picks a new one so the game never stops.
Step-by-Step Explanation
Step 1
Deploy members
Start three or more mongod instances configured with the same replica-set name.
Step 2
Initiate the set
Run rs.initiate() with the member hosts so MongoDB forms the replica set.
Step 3
Elect a primary
Members vote; the elected primary accepts all writes and records them in its oplog.
Step 4
Replicate the oplog
Secondaries continuously pull and replay the primary's oplog to mirror its data.
Step 5
Heartbeat and failover
Members exchange heartbeats; if the primary is unreachable, a new election promotes a secondary automatically.
What Interviewer Expects
- Definition of primary and secondary roles
- Understanding of the oplog and how replication works
- How elections and automatic failover happen
- Read preferences for directing reads to secondaries
- Why an odd number of voting members avoids ties
Common Mistakes
- Confusing replication (redundancy) with sharding (partitioning)
- Thinking secondaries can accept writes
- Assuming failover needs manual intervention
- Ignoring the role of the oplog in staying in sync
- Deploying an even number of voting members and risking tie votes
Best Answer (HR Friendly)
“A replica set is a group of MongoDB servers that all keep a copy of the same data. One server handles the writing and the others copy it, so if the main server fails, another automatically takes over and the application keeps running without downtime.”
Code Example
rs.initiate({
_id: 'rs0',
members: [
{ _id: 0, host: 'mongo1:27017' },
{ _id: 1, host: 'mongo2:27017' },
{ _id: 2, host: 'mongo3:27017' }
]
})
// Check roles and health
rs.status()
// Allow reads from secondaries in the shell
db.getMongo().setReadPref('secondaryPreferred')Follow-up Questions
- What is the oplog and how do secondaries use it?
- How does a replica-set election choose a new primary?
- What read preferences does MongoDB support?
- Why should a replica set have an odd number of voting members?
- What is an arbiter and when would you use one?
MCQ Practice
1. Which member of a replica set accepts write operations?
Only the primary accepts writes; secondaries replicate those writes from the primary's oplog.
2. What triggers a new primary election?
When members lose heartbeat contact with the primary, they hold an election to promote a secondary.
3. What does an arbiter contribute to a replica set?
An arbiter participates in elections to break ties but holds no data, useful for reaching an odd voting count cheaply.
Flash Cards
What is the primary in a replica set? — The single member that accepts all write operations and records them in the oplog.
What is the oplog? — A capped collection of every write operation that secondaries replay to stay in sync with the primary.
What is automatic failover? — When the primary fails, remaining members elect a new primary with no manual action.
What is an arbiter? — A voting-only member that helps elect a primary but stores no data.