Course contentsShow
← Cloud and Infrastructure
Lesson 2471 of 2,98953. High Availability PatternsFree lesson

Leader Election and Consensus

Understand leader election patterns for coordinating stateful services and ensuring single-writer consistency.

Leader Election and Consensus

You'll learn: How distributed systems choose a single coordinator to ensure consistency and avoid split-brain scenarios in stateful services.

Why Leader Election Matters

When you have multiple replicas of a stateful service running across zones or regions, they need to coordinate who performs writes or critical operations. Without a clear leader, two nodes might simultaneously update the same data, causing conflicts. Leader election is the democratic process that lets distributed nodes agree on one "leader" to coordinate activities, while others stand by as followers.

How Consensus Works

Think of leader election like a classroom choosing a class president. Students (nodes) vote, and one wins based on agreed rules. In distributed systems, consensus algorithms like Raft or Paxos enable this:

  • Nodes propose themselves as candidates
  • A quorum (majority) must agree on the leader
  • The leader sends regular "heartbeats" to prove it's alive
  • If the leader fails, followers detect timeout and trigger a new election

This pattern appears in databases (MongoDB replica sets), message queues (Kafka brokers), and orchestration systems (Kubernetes control plane).

Coordination Services

Rather than implementing consensus from scratch, use proven coordination services:

  • etcd (used by Kubernetes)
  • ZooKeeper (used by Kafka)
  • Consul (HashiCorp)

These maintain distributed locks and leader leases—short-lived tokens proving leadership—that expire if the leader crashes.

Single-Writer Consistency

Once elected, only the leader accepts writes, guaranteeing consistency. Followers replicate its decisions. This prevents the "split-brain" scenario where isolated network partitions create multiple leaders making conflicting decisions.

Key Takeaway: Leader election uses consensus algorithms to designate one coordinator in a distributed stateful system, ensuring single-writer consistency and preventing data conflicts across replicas.