The Journey of 100DaysofCode aka 100DaysofMongoDB (@Aasawari_24)

Day10 as #100DaysofMongoDB as 100daysofcode

Taking the replication concept ahead, will be discussing a few of Election concepts in the Replica set.

There are a few conditions when an election is initiated in a replica set:

  1. There has been no primary for an election timeout.
  2. If a member of replica set which is secondary but realises it has higher priority than Primary.( Priority Takeover)
  3. I f secondary considers itself to be more updated data, it initiates the election.
  4. If a primary steps down with replSetStepDown command. (Election Handoff)

Lets understand the Election concept from a voter as well as candidate perspective.

When an election is initiated, the candidate runs replSetRequestVotes towards all the replica sets. The candidate first votes for itself and then sends replSetRequestVotes towards all the members of the replica sets.
Now when the message is received by the voter, it evaluated the candidate on various parameters:

  1. The configuration do not match.
  2. The replica set name does not match.
  3. The last applied OpTime that comes in the vote request is older than the voter’s last applied OpTime.

When a voter casts its vote, it records in local.replset.election collection.

The Stepping down of a primary can be conditional or unconditional.
In Conditional Stepdown, if

force is true and now > waitUntil deadline
At least one of the updated secondaries, runs for a candidate.

For Unconditional StepDown

As long as primary is connected to all the secondaries, it will remain as Primary and will not be eligible for step down.

In both the conditions, RSTL lock is acquired and and allow secondaries to catch up without new writes coming in.

If you have any thoughts on Replication Election, Will be more than happy to discuss.

Thanks
Aasawari

Share on twitter: https://twitter.com/Aasawari_24

3 Likes