MongoDB replica sets with odd and even number of members
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
Introduction
MongoDB replica sets are an essential part of MongoDB's redundancy and high availability strategy. A replica set in MongoDB is a group of mongod instances that maintain the same dataset. Replica sets provide data redundancy, availability, and automatic failover. Replica sets involve configuring multiple MongoDB servers to ensure data is replicated consistently across servers, thus providing a backup if one server fails.
Odd vs Even Number of Members
The design of a MongoDB replica set is critical, especially concerning the number of members (nodes) in the set. MongoDB’s replica sets always require a majority of members to acknowledge a write operation to be considered successful. This approach provides durability and consistency of replicated data across nodes.
Odd Number of Members
Using an odd number of members in a replica set is generally advised. This configuration ensures that there will always be a majority of nodes available to maintain a consensus about the state of the set, facilitating automatic failover.
Example:
Consider a replica set of 3 members: A primary and two secondaries.
- Primary Member: Handles all write and read operations (by default).
- Secondary Members: Replicate data from the primary and can take over if the primary fails.
- Arbiter: An optional member that participates in elections but does not maintain a copy of the data.
In the event of a failure:
- If the primary fails, the remaining members can vote to elect a new primary.
- Majority (2 out of 3) is always achievable, enabling successful elections and operations.
Even Number of Members
While it is possible to have an even number of members, it introduces complexities regarding the maintenance of quorum, primarily when an arbiter is not involved.
- Without an arbiter, achieving quorum (majority voting) can be challenging.
- Network partitions or node failures might result in the absence of a clear majority, leading to a stale state.
Example:
Consider a replica set of 4 members:
- If a network partition occurs and two nodes are isolated, neither side can achieve a majority, resulting in failure to elect a new primary, thus halting writes.
To counter these issues, an additional arbiter is typically added to form an odd number of voters, enhancing failover efficiency.
Key Technical Considerations
- Election Process:
- MongoDB uses an election mechanism to determine which member is the primary.
- A new election is triggered if the primary becomes unreachable.
- Arbiters:
- Arbiters do not store a copy of the data but participate in the elections.
- They are beneficial in setups with even numbers, ensuring an odd number of election votes.
- Latency and Read/Write Concerns:
- Primary preference affects read latency since all writes happen on the primary.
- With a secondary read preference, applications might receive slightly stale data but reduce load on the primary.
- Automatic Failover:
- If the elected primary becomes unavailable, the secondary members will automatically conduct an election to select a new primary.
Best Practices
- Always aim for an odd number of members, even if it means using an arbiter.
- Consider the network latency and configuration of all nodes.
- Measure operation time-outs appropriately to handle failovers seamlessly.
- Regularly test failover mechanisms to ensure high availability goals are met.
- Monitor replica set status and perform regular backups for disaster recovery.
Summary Table
| Configuration | Characteristics | Pros | Cons |
| Odd Number Members | Achieves quorum easily with natural majority (3, 5, 7, etc.) | Simplifies failover decisions Increases reliability | May require more physical servers |
| Even Number Members | Requires arbiter for reliable quorum (4, 6, etc.) | Reduces configuration complexity in smaller setups Saves resource, as arbiter is lightweight | Without arbiter, may face stalemates Increased complexity if issues occur |
Conclusion
Designing a MongoDB replica set involves understanding the interplay between node numbers and MongoDB’s consensus mechanisms. Selecting an odd number of voting nodes enhances reliability with straightforward decision-making processes during failovers, thus maintaining high availability of your application’s data. By carefully planning your replica set members' configuration, considering the geographic zones, and regularly monitoring the performance, you can ensure robust and efficient database operations.
Related reading
- More classes in config than trained on tensorflow object detection API
- Most scalable way for using generators with tf.data ? tf.data guide says from_generator has limited scalability
- Multi-class classification in libsvm
- Multi-Class Logistic Regression in SciKit Learn
- mongodb, replicates and error err not master and slaveOkfalse, code 13435
- MongoDB replication error NetworkInterfaceExceededTimeLimit and MaxTimeMSExpired - Causes and Fix
- MongoDB ReplicaSet in K8S -- can't connect via port forward
- MongoDB Schema Design - Real-time Chat

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.