Why are Raft election timeouts randomised, and how should they relate to the heartbeat interval and the network delay?
Sketch your own answer first, then compare it with the one below.
A strong answer
If every follower used the same timeout, they would all time out together after a leader died, all become candidates for the same term, and split the vote. Nobody would get a majority, they would all time out again at the same moment, and the cluster could loop. Drawing each timeout at random from a range means one node usually fires first, wins before the others time out, and its heartbeats stop them.
The ordering is heartbeat interval, well below election timeout, well below the typical time between failures. Heartbeats must arrive several times per election timeout so that normal jitter does not trigger elections. The election timeout, and especially the width of its random range, must be large compared with the message delay, so that a candidate's votes come back before a second node times out.
When the range is tight relative to delay, split votes become common. Each adds a full timeout of leaderless time. So the timeout is a tradeoff too: longer means fewer split votes and more time without a leader after a crash.
Sources
- Diego Ongaro, John Ousterhout (2014). In Search of an Understandable Consensus Algorithm.