Elección de Líder

Lease · Mandato · Failover

Escrito por Darjus Kazakevic · revisado el

One leader per term, and a term check that refuses the old one.

Some work must have exactly one owner: the job scheduler, the writer of a replication log, the one process a vendor allows to connect. Leader election lets a group of identical nodes agree which node that is. In practice the leader holds a lease (a lock that expires) in a consensus store such as etcd or ZooKeeper, and renews it every few seconds.

Choosing a leader is the easy part. The hard part is the old leader. A leader that is cut off by the network, or paused by garbage collection, does not know it was replaced. So each leadership gets a term number, every command carries it, and receivers reject any term lower than the highest they have seen.

En esencia — Agree on one leader for a numbered term, keep it as a lease that must be renewed, and have every receiver reject commands from an older term.

Cuándo recurrir a él

Recurre a él cuando

  • One node must own a job, such as a scheduler or a replication log
  • A vendor or protocol allows only one active connection or writer
  • Standby nodes should take over in seconds, without a person

Reconsidéralo cuando

  • The work splits by key, so each node can own its own share
  • Your database or platform already elects a primary for you
  • The job is safe to run twice; make it idempotent instead

Conceptos clave

Lease
Leadership that expires unless the leader renews it. If the leader crashes, the lease runs out and another node takes over. Kubernetes controllers use a 15-second lease and try to renew it every 2 seconds.
Term (Epoch · generation)
A number that goes up with each new leader. Raft calls it a term, ZooKeeper an epoch, Kafka the controller epoch. It works as a fencing token (a number receivers use to reject old senders) for a role.
Split brain
Two nodes act as leader at once. It happens when the old leader is paused or cut off past its lease while a new one is elected. Terms do not prevent split brain; they make the old leader's writes harmless.
Clock skew
Leases are measured in time, and clocks on different machines drift and jump. A leader whose clock runs slow thinks its lease is still valid. Stop acting well before the lease ends to leave a safety margin.
Where elections live
etcd and Consul offer election APIs on Raft. In ZooKeeper, each candidate creates an ephemeral sequential node and the lowest number leads. Patroni keeps the Postgres leader key in etcd, Consul, ZooKeeper or Kubernetes (30 s TTL).

Reglas de diseño

Stamp every command with its term
An old leader keeps acting until it notices it lost. Send the term with every command and write, and have every receiver reject a term lower than the highest it has seen.
Elect with a proven store
Use etcd, ZooKeeper, Consul or the Kubernetes Lease API. A hand-written election usually fails during the network partitions it exists to survive.
Stop work before the lease ends
Clocks drift and processes pause. If you cannot renew before a deadline shorter than the lease, for example 10 seconds for a 15-second lease, stop acting as leader.
Split the work if you can
If jobs divide by key, let each node own a shard instead. One leader is a bottleneck and a failover pause; many owners scale and fail one shard at a time.

Errores habituales

  • No term on commands. After a long GC pause, the old leader finishes its batch and overwrites the new leader's work.
  • A lease shorter than normal pauses. A 2-second lease with 3-second GC pauses makes leadership flap between nodes all day.
  • Electing through a key on a single Redis instance. When Redis fails over to a replica that missed the key, two nodes lead.
  • A leader that does all the work. It should coordinate; if it also processes every job, it becomes the bottleneck.