ETCD Raft Consensus
One of three ETCD machines suddenly goes down, yet the cluster soon goes on accepting writes just as before. How do the remaining two stay consistent, and what happens if a second machine fails too?
Play the animation 6 steps, animated, plays on a computer or a tablet
ETCD Raft Consensus, step by step
-
The API issues a write for a new Pod, and it should be the only component that talks to ETCD. Every write goes through the Leader, the single point that orders all changes. A write that lands on a Follower is forwarded to the Leader. A linearizable read waits for the Leader to confirm it still holds a quorum, so no read sees a split view.
Play from step 1 -
The Leader appends the write as entry 9 in its own log, right after the 8 entries already stored. For now the entry lives on a single replica and stays uncommitted, so commitIndex is still 8 and the new Pod is invisible to readers. Nothing becomes durable until a majority holds it, and the Leader counts as one.
Play from step 2 -
The Leader sends an AppendEntries RPC carrying entry 9 to both Followers at once. Each Follower rejects a stale term, and before accepting the entry it checks that its entry 8 has the term the Leader sent for it. So an accepted log matches the Leader through entry 9. After writing entry 9 to its own log, each Follower returns an ack to the Leader.
Play from step 3 -
The Leader needs a majority rather than every replica: itself plus the first Follower to ack already makes 2 of 3, which meets quorum. With a majority persisted, entry 9 is committed and can no longer be lost, so the Leader advances commitIndex to 9 and reports the write back to the API as durable.
Play from step 4 -
The Leader also sends the new commitIndex to the Followers in an empty AppendEntries, signalling that entry 9 is safe to apply. Each Follower applies entry 9 to its state machine, the key-value view that clients read from. All three replicas now hold the Pod at index 9, and a linearizable read returns it from any member.
Play from step 5 -
Both Followers go silent, so the Leader holds one vote of three and quorum is lost. Entry 10 appends but cannot commit. Within two election timeouts the Leader steps down, and the write fails with etcdserver: request timed out. Linearizable reads stop, while serializable reads answer locally and may be stale until a majority returns.
Play from step 6