18

The mongodb documentation suggests that a replica set should have an odd number of voting nodes. What is the reason for this?

0

1 Answer 1

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).

10
  • 3
    By the way this is called split-brain. Search for it if you need more info. Sep 15, 2015 at 20:02
  • 5
    Let 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, 2016 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). Mar 24, 2016 at 8:54
  • 1
    Thanks 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, 2016 at 18:00
  • 1

Not the answer you're looking for? Browse other questions tagged or ask your own question.