MongoDB: two nodes & primary election

mongodbreplication

According to Mongo documentation in order to safely deploy a replication set, you need at least two active and one arbiter because election of the a primary needs the majorities of the votes.

Let's say I'm able to have three machines, therefore deploy three full-blown mongo instances, no arbiter.

If the elected primary fails I end up with TWO nodes, where both of them has the same "power" level: for me this seems to be the must-aviod situation described at the deploy.

Can someone unfold why is it not a problem to elect a primary in this situation when it is if the initial setup is the same?

Best Answer

Electing a primary in a two node set isn't a problem as long as both nodes are available. The rule is that the majority of the set needs to be up to successfully elect a primary.

In a two node set:

  • If both nodes are up, it can elect a primary
  • If only one node is up, it cannot see a majority and will remain read only

In a three node set:

  • If three nodes are up, one can become primary
  • If two nodes are up, one can become primary
  • If only one node is up, it's read only

A two node + arbiter set behaves just like a three node set, so if any one node (including the arbiter) fails, it can elect a primary.

It's important to realize that a set with only two voting nodes is running without write redundancy. You wouldn't want to create a set that always runs like that (hence, the arbiter), and you also want to recover a third node as soon as practically possible in the event of an outage.

Related Topic