The problems of distributed state machines and distributed logs are essentially the same. 3 roles: leader, follower and candidate. Only 2 RPC APIs in the algorithm. RequestVotes and AppendEntries (which also serves as heartbeats from leader to followers). Terms are tied to leadership changes, not with time. There can be terms that don’t have any leader because of election failures. Log indices change with commands from clients. So, multiple indices in sequence could have the same term. Leader is always right. So, it can change old log entries on other nodes. However, during election, the algorithm tries to elect a leader that is most up to date. Invariant: if, at an index, term number is the same between two nodes, everything before it should also be so. If nodes find discrepancies during replication, they reject the AppendEntries call from the leader and it’s the latter’s responsibility to retry for smaller indices. So, just those two APIs in the system. (Followers don’t make any API calls to resolve discrepancies.) During election, node will vote for a candidate only if the latter has a more complete log, which is defined by: 1) higher term and 2) higher log index, in sequence. 2 different majorities will contain at least 1 common member. While obvious, this has an important implication. If 2 different leaders are elected in sequence, at least 1 node will be common between those that voted for them. Open questions:
...