Skip to content
LESSON 41 / 50Intermediatedistributed

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.

Interactive Engine Simulator

Replica Set Raft Consensus & Heartbeat Election Lab

Simulate 3-node replica set heartbeats, failovers, oplog replication, and write concern guarantees.

Consensus Term
Term 1
Monotonically increasing
Voting Quorum
2 / 3 Majority
Strict floor(N/2) + 1
Heartbeat Interval
2000 ms
10s election timeout
Write Concern
node-1PRIMARY
Host:mongo-node-1:27017
Oplog Sequence:#1045
Voted Term:Term 1
Accepts all driver write and read traffic (ReadPreference: primary).
node-2SECONDARY
Host:mongo-node-2:27017
Oplog Sequence:#1045
Voted Term:Term 1
Tails primary local.oplog.rs in background thread.
node-3SECONDARY
Host:mongo-node-3:27017
Oplog Sequence:#1045
Voted Term:Term 1
Tails primary local.oplog.rs in background thread.
REPL_LOG:Replica set stable on Term 1. Primary node-1 actively serving writes.

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.

Sample BSON Documents:
Collection: db.rs.status()
{
  "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

Step 1: Initiate a 3-node replica set
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 }
  ]
});
Priority 2 makes mongo-1 the preferred primary candidate.
Step 2: Inspect replica set status and health
rs.status();
Monitors member states, heartbeat latencies, and sync sources.

5. Critical Engineering Rules

Rule / ConstraintUnderlying Reason & Impact
Odd Number of VotesAlways maintain an odd number of voting members (e.g. 3, 5, 7) to prevent split-brain election deadlocks.
Arbiter CautionArbiters 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 }));
Production Warning: Never run an arbiter on the same physical host as a data-bearing node.

7. Architectural Trade-offs

Advantages
  • •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
Disadvantages & Gotchas
  • •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

Q1: What is an Arbiter in a MongoDB replica set, and why is its use generally discouraged in modern production deployments?

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?