The mongodb documentation suggests that a replica set should have an odd number of voting nodes. What is the reason for this?
Asked
Active
Viewed 8,525 times
1 Answers
25
Let's imagine that a replica set has even number of nodes (4, for example). Then an unfortunate network partition happens that splits the set in half (2 + 2). Which partition should accept writes? First? Second? Both? What should happen when network is restored? These are all hard questions.
Having odd number of nodes eliminates the questions entirely. The set can't be split exactly in half. So the bigger part will accept writes (to be exact, node must see more than half of nodes (including self) to be elected as primary. So it's 1 of 1, 2 of 3, 3 of 5, 4 of 7 and so on).

Sergio Tulentsev
- 226,338
- 43
- 373
- 367
-
Correct - MongoDB voting works on the concept of quorum http://en.wikipedia.org/wiki/Quorum_(distributed_computing) – sweaves Apr 22 '13 at 17:56
-
3By the way this is called split-brain. Search for it if you need more info. – Mike Argyriou Sep 15 '15 at 20:02
-
5Let me admit my ignorance and ask the naive question... If only one node goes down in an odd numbered replica set, than you have the 'even number of nodes' problem. It would seem like the probability of one node going down would be greater than the probability of two nodes going down. So why wouldn't you want an even number of nodes to guard against the event that has the greater probability? – JohnC Mar 23 '16 at 22:00
-
2@JohnC: one node going down is not a problem at all. The other nodes still form the majority and can perform elections, etc. Odd number of nodes requirement is to protect from network **partitions** when the cluster is split in half. If it contained even number of nodes, then majority can't be formed (as explained in the answer). – Sergio Tulentsev Mar 24 '16 at 08:54
-
1Thanks Sergio, makes sense.. Another naive question... If a network partition split does occur and the 'incumbent' primary node is alive but on the minority side of the split, I presume that the node will discontinue its primary node role? – JohnC Mar 28 '16 at 18:00
-
@JohnC: correct. The majority will reelect the new primary. – Sergio Tulentsev Mar 28 '16 at 18:54
-
I was still confused and after further research I gained a better understanding by thinking about "majority" as relative to total number of nodes, versus total number of remaining nodes. – wayofthefuture Feb 27 '17 at 13:10
-
@wayofthefuture: yep, it's majority of all nodes, of course. – Sergio Tulentsev Feb 27 '17 at 13:28
-
@wayofthefuture i am still confused, let's say we have 3 node in the starting, 1 goes down or connectivity fail. we are left with 2 nodes as majority, they will try to decide master, but if both of them elect themselves or others, how do we get away from this deadloack? – best wishes Nov 13 '19 at 04:55
-
1@bestwishes: https://en.wikipedia.org/wiki/Consensus_algorithm. And also https://www.mongodb.com/presentations/replication-election-and-consensus-algorithm-refinements-for-mongodb-3-2 – Sergio Tulentsev Nov 13 '19 at 09:55