100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace

How Does Leader Election Work in Distributed Systems?

Learn how leader election works: lease-based coordination, consensus protocols, split-brain, and fencing tokens.

mediumQ107 of 224 in System Design Est. time: 5 minsLast updated:
Open Code Lab

Expected Interview Answer

Leader election is the process by which nodes in a distributed cluster agree on a single node to coordinate a particular task, using a protocol (often built on consensus like Raft or Paxos, or a coordination service like ZooKeeper/etcd) so that exactly one leader is active at a time and a new one is chosen automatically if the current leader fails.

Systems need a leader to avoid conflicting concurrent writes or duplicated work, for example a single primary database node, a single job scheduler, or a single Raft cluster leader. Election typically works by nodes racing to acquire a lease or lock in a coordination store (like an ephemeral znode in ZooKeeper or a lease key in etcd) that automatically expires if the holder stops renewing it via heartbeats, so a crashed leader is detected and a new election triggers. Consensus-based approaches like Raft bake election directly into the protocol using randomized timeouts and majority votes. The hard part is avoiding split-brain, where a slow or partitioned old leader still believes it is leader while a new one has been elected; this is solved with fencing tokens (a monotonically increasing number issued on each election that downstream systems must check) or lease-based mutual exclusion with clock-skew safety margins.

  • Ensures exactly one coordinator handles writes or scheduling, avoiding conflicts
  • Automatically recovers from leader failure without manual intervention
  • Combined with fencing tokens, prevents split-brain from causing data corruption
  • Decouples application logic from the complexity of consensus by using a coordination service

AI Mentor Explanation

Leader election is like a squad choosing a vice-captain who automatically becomes captain if the captain is unexpectedly ruled out, instead of the team playing without any decision-maker. The squad watches for regular sign-ins from the captain (heartbeats), and if those stop, a quick vote promotes the next candidate. To avoid two people giving conflicting field placements at once (split-brain), each new captaincy comes with a fresh authorization number that umpires check before accepting any instruction. This automatic, conflict-free hand-off is exactly what leader election provides for a distributed cluster.

Step-by-Step Explanation

  1. Step 1

    Nodes race for a lease/lock

    Each node attempts to acquire an exclusive lease or lock in a coordination store (ZooKeeper, etcd) or via a consensus protocol vote.

  2. Step 2

    Leader is established

    The winning node becomes leader and renews its lease periodically or sends heartbeats to prove it is alive.

  3. Step 3

    Failure detection

    If the leader stops renewing its lease or sending heartbeats within a timeout, followers detect the failure.

  4. Step 4

    New election and fencing

    A new leader is elected with an incremented fencing token/term; downstream systems reject any request carrying an older token to prevent split-brain.

What Interviewer Expects

  • Explains why a single leader is needed (avoiding conflicting writes/duplicated work)
  • Describes lease/lock-based election (ZooKeeper/etcd) or consensus-based election (Raft)
  • Identifies split-brain as the key risk and explains fencing tokens as the mitigation
  • Mentions heartbeats/timeouts as the failure detection mechanism

Common Mistakes

  • Assuming a crashed leader is detected instantly with zero false positives
  • Forgetting fencing tokens and how split-brain can silently corrupt data
  • Confusing leader election with simple round-robin load balancing
  • Not considering clock skew when reasoning about lease expiry

Best Answer (HR Friendly)

Leader election is how a group of servers automatically agrees on one of them to be in charge of a task, and picks a new leader if that one fails. It’s important because having exactly one leader avoids conflicting decisions, and the system needs a safe way to detect failure and hand off leadership without two servers thinking they’re both in charge at once.

Code Example

etcd-style lease-based leader election (illustrative)
election:
  key: /cluster/leader-lock
  lease:
    ttlSeconds: 10
    renewIntervalSeconds: 3
  onAcquire:
    action: becomeLeader
    fencingToken: incrementCounter(/cluster/election-term)
  onLeaseExpired:
    action: releaseLock
    trigger: newElection

downstreamWriteCheck:
  rule: reject request if request.fencingToken < currentKnownTerm
  reason: prevents a stale leader from applying writes after losing leadership

Follow-up Questions

  • What is split-brain and how do fencing tokens prevent it from causing data corruption?
  • How does leader election in Raft differ from lease-based election using ZooKeeper or etcd?
  • What happens during a network partition where the old leader can still reach a minority of nodes?
  • How do you choose an appropriate lease TTL and heartbeat interval to balance failover speed vs false positives?

MCQ Practice

1. What is the primary risk that fencing tokens are designed to prevent?

Fencing tokens are monotonically increasing identifiers that let downstream systems reject stale requests from an old leader, preventing split-brain damage.

2. How do most lease-based leader election systems detect that a leader has failed?

Leases have a time-to-live; if the leader fails to renew it via heartbeats before expiry, the lease is released and a new election is triggered.

3. Why do distributed systems typically need exactly one leader for certain operations?

A single leader serializes writes or scheduling decisions, avoiding the conflicts that would arise if multiple nodes acted independently.

Flash Cards

What is leader election?The process by which distributed nodes agree on one coordinator, automatically re-electing on failure.

What is split-brain?A state where more than one node believes it is the leader, risking conflicting writes.

What is a fencing token?A monotonically increasing number issued per election that downstream systems use to reject stale leader requests.

Name two ways leader election is implemented.Consensus protocols like Raft, or lease/lock-based coordination services like ZooKeeper or etcd.

1 / 4

Continue Learning