Advanced22 min readยท Topic 10.1

Coordination and leader election

ZooKeeper, etcd, leader election algorithms, distributed locks and fencing tokens

๐Ÿ‘‘Key Takeaways

  • 1
    Leader election: one node coordinates work โ€” all others are followers. If the leader fails, a new one is elected.
  • 2
    ZooKeeper/etcd provide distributed coordination primitives: locks, leader election, configuration
  • 3
    Fencing tokens prevent 'split brain' โ€” a token monotonically increasing ensures stale leaders can't write
  • 4
    Distributed locks are inherently dangerous โ€” prefer lease-based locks with TTL and fencing tokens

Coordinating Distributed Nodes

Many distributed systems need a single coordinator: one node that assigns work, holds a lock, or acts as the single writer. Leader election algorithms ensure exactly one leader exists, and that a new leader is elected quickly if the current one fails.

Coordination Services

ServiceConsensusUse CaseAPI Style
ZooKeeperZab (Paxos variant)Leader election, config, distributed locksHierarchical znodes, watchers
etcdRaftK8s config store, leader election, service discoveryKey-value with watch, lease, transactions
ConsulRaftService discovery, health checks, KV, leader electionHTTP API, DNS interface
โš ๏ธThe Distributed Lock Problem
A lock acquired from Redis/etcd can fail silently: network partition causes the lock holder to lose connectivity while still believing it holds the lock. Always use fencing tokens โ€” a monotonically increasing number attached to each lock acquisition. Resource servers reject requests with tokens older than the latest seen.

Advantages

  • โ€ขLeader election provides a clear coordination point
  • โ€ขZooKeeper/etcd are battle-tested for coordination
  • โ€ขFencing tokens prevent split-brain writes

Disadvantages

  • โ€ขLeader is a bottleneck and SPOF (need fast re-election)
  • โ€ขDistributed locks are complex to use correctly
  • โ€ขCoordination services add infrastructure overhead

๐Ÿงช Test Your Understanding

Knowledge Check1/1

What is a fencing token used for?