Why does Raft need a majority of nodes to elect a leader and to commit an entry? What happens with three nodes if one dies, and if two die?
Sketch your own answer first, then compare it with the one below.
A strong answer
Any two majorities of the same set overlap in at least one node. That overlap is the whole trick. If an entry is on a majority, then any future majority that elects a leader includes a node that has the entry, and the election restriction stops a candidate without it from winning. So a committed entry is never lost.
With three nodes a majority is two. Lose one node and the other two can still elect and commit, so the cluster keeps working. Lose two and the last node cannot form a majority. It keeps raising its term and asking for votes, but it will never become leader and nothing commits until a second node returns.
The cluster does not drift into a wrong answer in that state. It stops. Raft chooses to be unavailable rather than inconsistent.
Sources
- Diego Ongaro, John Ousterhout (2014). In Search of an Understandable Consensus Algorithm.