Let’s say I have 5 nodes replica set.
3 of them are dead, 2 alive.
How would the majority will be calculated? 5 nodes majority would be 5/2+1=3 but if 3 nodes are dead, will it still look for 3 votes? There are only 2 alive so only 2 votes… or the majority will be 2/2+1=2 which is still confusing because what will happen at the elections, and does read/write concerns has any affect to any of these…
Basically the number n in n/2+1 to from majority indicates the whole replica set memebers length (of members that can vote dead or alive) or the length of only alive nodes?
I tried manually killing 3 nodes in a 5 node replica set (4 plus arbiter, killed also the arbiter) and the 2 left nodes both became secondaries and I couldn’t reach the db.
And I tried the same thing in a 3 nodes replica set (without an arbiter) and killed 1 node, so it’s the same as the first test where only 2 data nodes are alive and it did elect a primary and the Db worked.
I’m trying to understand what’s difference between both of them.
2
Answers
When your replica set has 5 members (i.e. 5 voting members) then the majority is 3 which means at least 3 nodes have to up and reachable in order to elect a PRIMARY. Otherwise the nodes remains in SECONDARY state.
Have a look at MongoClient to connect to multiple hosts to handle failover?
Assuming you dont change priority, hidden or votes in the nodes configuration (e.g. you use default replicaSet settings ) and you dont have ARBITER , here is what happen:
1) How many members replicaSet allow to be down at same time?
3x members replicaSet allow only 1 from 3 members to be down at same time and replicaSet will be still writable ( e.g. PRIMARY will be elected )
5x members replicaSet allow only 2 from 5 members to be down at same time and replicaSet will be still writable ( e.g. PRIMARY will be elected )
7x members replicaSet allow only 3 from 7 members to be down at same time and replicaSet will be still writable ( e.g. PRIMARY will be elected )
2) What happen if more members are down than allowed?
If in 3x members replicaSet there is 2x members down , election will be triggered and the member that is still alive will not receive majority of members votes , so if it was PRIMARY will switch to SECONDARY , if it was SECONDARY it will stay SECONDARY. ReplicaSet will be still readable but writes will be not possible , the same happen with any other replicaSet when less them majority of members are available.