Replica Set Topology: Primary, Secondary & Arbiter Nodes
A MongoDB Replica Set is an automated high-availability cluster consisting of one Primary node that accepts all writes and multiple Secondary nodes that replicate data.
Replica Set Raft Consensus & Heartbeat Election Lab
Simulate 3-node replica set heartbeats, failovers, oplog replication, and write concern guarantees.
1. Formal Definition & Architecture Intent
A Replica Set is a group of mongod instances that maintain the same data set. It provides redundancy and high availability. Exactly one node acts as the Primary, accepting all write operations. All other voting members act as Secondaries, asynchronously replicating the Primary's oplog. An Arbiter participates in elections to break ties but stores no data.
2. Explain Like I'm 5 (Mental Model)
Think of a corporate department with 1 Boss (Primary) and 2 Assistants (Secondaries). The Boss does all the decision-making and hands copies of every memo to the assistants. If the Boss gets sick, the assistants immediately vote one of themselves to become the new Boss.
3. Real-World Architecture Example
A standard production 3-node replica set deployed across 3 availability zones.
{
"set": "rs0",
"members": [
{
"name": "mongo-1:27017",
"stateStr": "PRIMARY",
"health": 1
},
{
"name": "mongo-2:27017",
"stateStr": "SECONDARY",
"health": 1
},
{
"name": "mongo-3:27017",
"stateStr": "SECONDARY",
"health": 1
}
]
}Three data-bearing nodes guarantee that any one node can fail without loss of read/write availability.
4. MQL Implementation Steps
rs.initiate({
_id: "rs0",
members: [
{ _id: 0, host: "mongo-1:27017", priority: 2 },
{ _id: 1, host: "mongo-2:27017", priority: 1 },
{ _id: 2, host: "mongo-3:27017", priority: 1 }
]
});rs.status();
5. Critical Engineering Rules
| Rule / Constraint | Underlying Reason & Impact |
|---|---|
| Odd Number of Votes | Always maintain an odd number of voting members (e.g. 3, 5, 7) to prevent split-brain election deadlocks. |
| Arbiter Caution | Arbiters do not hold data; they can break election ties but cannot be read from or become primary. |
6. Deep Dive: Why Arbiters Are Discouraged in Modern MongoDB Clusters
In MongoDB 4.0+, features like multi-document ACID transactions and Read Concern "majority" require majority data-bearing nodes. An Arbiter has no data and cannot acknowledge majority writes. A 2-data-node + 1-arbiter cluster loses majority write availability as soon as a single data node goes down! Modern architectures deploy 3 full data-bearing nodes.
// Check voting member configuration:
rs.conf().members.map(m => ({ host: m.host, votes: m.votes, priority: m.priority }));7. Architectural Trade-offs
- •Automatic failover: if primary fails, secondaries elect a new primary in under 3 seconds
- •Data redundancy across independent physical machines and failure zones
- •Read scaling: read preferences allow offloading analytical queries to secondaries
- •Replication is asynchronous by default; un-replicated writes can be rolled back on failover
- •Triples storage hardware requirements (3 data-bearing nodes)
8. Technical Interview Questions
An Arbiter is a mongod instance that participates in replica set elections by casting a vote, but holds no data. Its use is discouraged in modern deployments because an Arbiter cannot participate in { w: "majority" } write acknowledgments. In a cluster with 2 data nodes and 1 arbiter, losing 1 data node permanently blocks all majority writes.
9. Staff-Level Ready Answer
"A MongoDB replica set provides high availability with 1 Primary and multiple Secondaries. The primary processes writes and logs them to the oplog, which secondaries replicate asynchronously. Clusters should have an odd number of voting nodes (typically 3 or 5 data-bearing nodes) to prevent split-brain election ties."
10. Quick Knowledge Check
What is the minimum recommended number of voting members in a production MongoDB replica set to avoid election ties?