Consenso

Raft · Paxos

Escrito por Darjus Kazakevic · revisado el

One ordered log that a majority of nodes has agreed to.

Consensus is how several nodes agree on a value, or on a sequence of values, when any of them may crash or lose the network. The usual form is a replicated log: every node applies the same commands in the same order, so every node ends in the same state. Raft and Paxos are the two well-known protocols.

The rule that makes it safe is the majority. An entry is committed once more than half of the nodes have stored it, and any two majorities share at least one node. The cost is a round trip to a majority on every write, and no writes at all while a majority is down.

En esencia — A leader appends each command to a log and commits it once a majority stores it. Any two majorities overlap, so a committed entry is never lost.

Cuándo recurrir a él

Recurre a él cuando

  • Several nodes must agree on one leader, lock owner or config value
  • You need a strongly consistent store for small, critical metadata
  • A replicated service must never accept two conflicting writes

Reconsidéralo cuando

  • Large or high-volume data: consensus is for metadata, not bulk data
  • Stale reads are fine and staying writable matters more: use quorums
  • One database already gives you the guarantee you need

Conceptos clave

Replicated log (State machine replication)
An ordered list of commands that every node stores. Each node applies the entries in log order, so the same log always produces the same state on every node.
Leader and term
Raft elects one leader per term (a numbered election round). Only the leader accepts writes and sends new entries to the followers. A follower that hears nothing for an election timeout starts a new term and asks for votes.
Majority quorum (2f + 1)
An entry is committed when a majority of nodes stores it. To survive f failed nodes you need 2f + 1 nodes. Any two majorities share a node, so every election reaches a node that holds each committed entry.
One failure, step by step
Three nodes, A, B and C. A leads in term 4 and sends x = 5. B stores it, so A and B make 2 of 3, a majority: committed. Then A crashes before C gets it.
What you actually run
etcd (the store behind Kubernetes), Consul and CockroachDB use Raft. ZooKeeper uses ZAB, a close relative. Kafka 4.0 dropped ZooKeeper and runs its own Raft, called KRaft. Google Spanner uses Paxos.

Reglas de diseño

Use a proven implementation
Raft fits in a short paper, but correct code takes years of testing under failures. Run etcd, ZooKeeper or Consul, or use a tested library such as etcd's raft package.
Run 3 or 5 nodes, never an even number
You need 2f + 1 nodes to survive f failures. Four nodes survive one failure, like three, but every write waits for three votes. Above seven nodes, writes slow down for little gain.
Spread the nodes across failure zones
Put each node in a different availability zone. If two of three nodes share a rack, one rack failure removes the majority and the cluster stops accepting writes.
Keep values small and disks fast
Every commit waits for a disk sync on a majority. Use SSDs, keep values small (etcd limits a request to 1.5 MiB) and store pointers to data, not the data.

Errores habituales

  • Running two or four nodes. Two nodes cannot survive any failure, and four survive only one while every write needs three votes.
  • Storing application data in etcd or ZooKeeper. The log and the watch traffic grow until the cluster slows down or hits its quota (2 GiB by default in etcd).
  • Reading one node's local copy, such as etcd's --consistency=s, and treating it as current. That node may be behind the leader.
  • Stretching one cluster across distant regions. Every write then pays a cross-region round trip, often 70–150 ms, to reach a majority.