MongoDB: two nodes & primary election

5,175

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.

Share:
5,175

Related videos on Youtube

petermolnar
Author by

petermolnar

Dreamer, who believes in optimization.

Updated on September 18, 2022

Comments

  • petermolnar
    petermolnar over 1 year

    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?

  • petermolnar
    petermolnar about 11 years
    This is OK, and thank you for the explanation, but I still don't have an answer why is it not a problem to elect from two nodes when one fails in a three-node setup and is when I do a two-node setup :)
  • Adam C
    Adam C about 11 years
    actually, it does answer the question - 2/3 = majority, 2/2 = majority. The difference is that in a 3 node set if you lose a node you still have a fully functioning primary and a secondary, in a 2 node set if you lose a single node you are read only (no primary). You can run with 2 nodes, it just doesn't get you anything from a redundancy perspective - in fact you have made your system more likely to fail.
  • user2980702
    user2980702 about 7 years
    I know this is an old question, but in case anyone is still confused, in a 3-node set, if you lose a node, it doesn't become a 2-node set. It's still a 3-node set of which 2/3 (more than half) are operational. In a 2-node set, if one goes down, it's a 2-node set with 1/2 operational (not a majority). It took me a while to understand that it's not the fact that there are 2 nodes up, it's the fact that it's 2/3 vs 2/2.