Server selection is not performed when replica has two members

i’m using mongoose

configured a replica set of 4 instances

i started inserting data into the replica set with interval 1 second

while the data is being inserted, i stepped down the primary server
after that a ‘disconnected’ event was fired and ‘reconnected’ is fired … after that the replica set has 3 servers it elects one of the 3 remaining to be the primary and the data continued to be inserted into the database
when the new primary is stepped down now i have 2 working secondaries servers in the replica set it doesn’t elect a new primary of the two secondaries to be the new primary instead of that it throws MongooseServerSelectionError

why not one of the two remaining servers is selected as primary?

Hi Ahmed,

It’s because you have an even number of nodes in the replica set. This is not a recommended setup (see Replica Set Deployment Architectures). You should have an odd number of nodes in a replica set.

A replica set elects a new primary based on simple majority voting between the nodes. Offline nodes is considered an abstain. So in your scenario of 4 nodes:

  • 1 node offline: there are 3 out of 4 nodes that can vote, so a new primary is elected
  • 2 nodes offline: there are 2 out of 4 nodes that can vote, and they cannot form a simple majority anymore, so a new primary is not elected.

This scenario is outlined in Consider Fault Tolerance, where a 4 node replica set can tolerate only 1 node offline. More than this, then they cannot elect a new primary.

See Replica Set Elections for more details about election procedures in a replica set.

Best regards,
Kevin

2 Likes