Consensus
Placeholder. This lesson is drafted but not written yet.
Replication needs a leader. Choosing one is harder than it sounds, because the machines doing the choosing can crash, and the network between them can drop messages.
Consensus is the problem of getting a group of machines to agree on one value even while some of them are failing.
Agreement needs a majority
A single machine declaring itself leader is not agreement. A majority is:
const votes = await Promise.all(peers.map((p) => p.requestVote(term)));
if (votes.filter(Boolean).length > peers.length / 2) {
becomeLeader(term);
}
Requiring more than half means two different leaders can never be elected in the same term, because two majorities of the same group must overlap by at least one machine.
What is still to write
- Terms, and how they order elections
- Why a split vote is safe but wasteful
- The log, and what it means for an entry to be committed
- What consensus costs, and why it is not used for every write