What is a Replica Set?
A Replica Set in MongoDB is a cluster of several servers that hold the same copy of data and automatically switch over on failures, providing high availability and fault tolerance.
What a Replica Set is made of
The set usually includes:
| Node | Role |
|---|---|
| Primary | accepts all write operations and the main queries |
| Secondary | copies data from Primary and serves as a backup |
| Arbiter (optional) | takes part in voting, but holds no data |
Secondaries can also be used for reads, if allowed by the settings (readPreference), which helps take load off Primary.
How fault tolerance works
- Primary fails
- The remaining nodes run an election
- They vote and pick a new Primary
- The cluster keeps running with no manual intervention
This is called auto-failover.
Why a Replica Set is used
- High availability (HA), the database doesn't go down when a node fails
- Read scaling, read requests can be pointed at Secondary
- Data protection, several copies keep the data up to date
- The option for geo-distribution, nodes can live in different data centers
Summary
A Replica Set is a MongoDB mechanism where several nodes hold identical data and automatically maintain the system's availability and consistency, even through failures. It's the foundation of MongoDB's fault-tolerant, production clusters.
Short Answer
Interview readyPremium
A concise answer to help you respond confidently on this topic during an interview.