What are Replication and Replica Sets?
MongoDB Replication & Replica Sets
Replication provides fault tolerance and high availability across infrastructure failures. A Replica Set consists of multiple mongod instances hosting identical data sets.
Replica Set Member Roles
| Member Role | Accepts Writes? | Accepts Reads? | Maintains Oplog? | Voting Rights? |
|---|---|---|---|---|
| Primary | Yes (Single writer) | Yes (Default) | Yes (Generates oplog entries) | Yes (1 vote) |
| Secondary | No | Yes (With read preferences: secondaryPreferred) |
Yes (Replicates from primary) | Yes (Can be elected primary) |
| Arbiter | No | No | No (Holds zero data) | Yes (Breaks election ties only) |
| Hidden / Delayed | No | Hidden: No (reporting only); Delayed: disaster recovery | Yes | Can vote (priority = 0) |
Automatic Failover & Raft-Like Consensus
- Heartbeats are exchanged between all members every 2 seconds.
- If the primary fails to respond within 10 seconds, eligible secondaries initiate an election.
- The candidate secondary with the most up-to-date oplog and a majority vote becomes the new primary.