In a Replica Set architecture where there is a primary member for write operations and secondary member which replicates operations and data from primary, there is a democratic election which takes place to determine the primary member.

Elections can take place for a wide range of reasons. This includes:

  • Mongo instance or replica set is down or bought online and is part of the replica set architecture
  • If a primary member is forced to step down and become a secondary member using the command rs.stepDown()
  • Secondary member losing connectivity to primary.

Of course, there are many factors that dictate which member becomes a primary member. We won't dive into that too much as the MongoDB documentation describes this here. However, normally each member has a vote to determine a primary member.

Diving into the main question, there is the case of no primary being elected and the last Mongo Replica becoming a secondary member. This can occur when there is no majority in votes available in an election. You could resemble this to a hung parliament or election in the real world, were no one has a majority and holds the full power to control the country. Therefore, the step down from primary to secondary member acts as failsafe mechanism and writes can only be performed when there is a majority to elect a new primary. This acts as a way to protect the database in an event of replica downtime as well as the rare case of having more than 1 primary which is mentioned here.

A majority is determined by the number of members in the replica, for example:

  • 3 Members requires a majority of 2 votes
  • 4 Members requires a majority of 3 votes
  • 5 Members requires a majority of 3 votes
  • 6 Members requires a majority of 4 votes, etc.

It follows the pattern of (number of members / 2) + 1 which is rounded down.

If a majority cannot be achieved the following would be logged in MongoDB logs to confirm this:

Not starting an election, since we are not electable due to: Not standing for election because I cannot see a majority