MongoDB replica set: Why the last remaining server becomes secondary

mongodb

I am experimenting with MongoDB replica sets to test the high availability scenarios. I have set up a 3 server replica set. When I stop the primary server, one of the remaining 2 servers becomes primary. After that, when I stop the new primary the last remaining server shows it's status as secondary.

Shouldn't the last server automatically become the primary? Or do I need to configure some setting for this to happen?

Best Answer

Each MongoDB replica set has at most a single primary which is elected on a quorum basis. A strict majority of replica set members must vote for the primary (i.e. n/2+1 votes, where n is the number of configured replica set members).

If you have a single surviving member in a 3-member replica set, that member cannot be elected (or remain) primary as there is only a single vote available.

The requirement for a majority of votes is to avoid having multiple primaries in cases such as network partitions. For example, if all 3 replica set members are technically "up" but cannot communicate with each other due to network issues, only a partition with a majority of replica set members will be able to maintain a primary.

For more information, see Replica Set Elections in the MongoDB manual.