Consensus & Leader Election
Consensus lets a group of nodes agree on a value or a sequence of commands despite failures — the basis of leader election, replicated logs, distributed locks and configuration stores (etcd, ZooKeeper, Consul).
- Raft in one breath: nodes are followers, candidates or the leader; a follower that hears no heartbeat within a randomized timeout starts an election for a new term and wins with a majority of votes; the leader appends client commands to its log and replicates them; an entry is committed once stored on a majority and is then applied in order.
- 2f + 1 nodes tolerate f failures (3 → 1, 5 → 2). Every write needs a majority round trip, so keep clusters small and use them for metadata and coordination, not bulk data.
- Split brain: an old leader cut off by a partition may still believe it leads → leases plus fencing tokens (monotonic epoch numbers checked by the storage layer).
- Paxos is the classic equivalent; ZooKeeper uses ZAB; etcd, Consul, CockroachDB, TiKV and Kafka's KRaft use Raft.