How it works
Leases and fencing depend on something that cannot disagree with itself. Consensus provides it: several nodes agree on one sequence of decisions even when some crash. Raft, used by etcd and CockroachDB, organizes this as a replicated log. A leader appends each command and copies it to followers. An entry the leader created in its own term counts as committed once a majority stores it, and only then is it applied and acknowledged.
Time is divided into numbered terms, each with at most one leader. A node votes once per term and a candidate needs a majority, and any two majorities share a node, so two candidates cannot win the same term. Nodes reject messages from a lower term than they have seen, and a stale leader steps down on learning of a higher one. Voters also refuse a candidate whose log is behind their own.
Take five nodes with N1 leading term 7. Entry 42 reaches N1, N2, and N3, a majority, so the client gets its acknowledgment. A partition then isolates N1 and N2. N4 and N5 lack entry 42. N4 asks for votes in term 8, but N3 refuses, so N4 gets only two. N3 then stands in term 9 and wins with three votes, and entry 42 survives.
Each write costs a majority round trip and a disk flush, and writes pause during elections, often around a second. More nodes do not raise write throughput. Five nodes tolerate two failures and three tolerate one; a sixth adds no tolerance, because the majority rises to four.
A team runs three etcd nodes in one availability zone, and writes are fine. Leadership wants more resilience, and someone proposes going to seven nodes in that same zone. What do you recommend?
Seven nodes in one zone still fail together when the zone does, so this adds write cost with little protection against the risk leadership is worried about. Spread three or five nodes across three zones, so losing a zone takes out only a minority: one of three, or at most two of five split 2-2-1. The tradeoff is some extra write latency from cross-zone round trips.
Use this as a guide for self review; written answers are not graded.Keep in mind
- An entry is safe once a majority stores it, because any later majority overlaps that one.
- Terms let nodes detect and reject a stale leader.
- Even-sized groups add cost without adding fault tolerance.