How do elections work?
All of the servers in a replica set maintain regular communication with every other member via a heartbeat. The heartbeat is a small packet that’s regularly sent to verify that all members are operating normally.
Secondary members also communicate with the primary to get the latest updates from the oplog and apply them to their own data.
Note
The information here refers to the latest replication election protocol, version 1, which is the latest as of MongoDB v6.0.
Schematically, we can see how this works.
When the primary member goes down, all of the secondaries will miss a heartbeat or more. They will be waiting up until the settings.electionTimeoutMillis
time passes (the default is 10 seconds), and then the secondaries will start one or more rounds of elections to find the new primary.
For a server to be elected as primary from the secondaries, it must have the following two properties:
- Belong in a group of voters...