Question #60 of 60•Infrastructure & Messaging•Hard• ~3 min read

How do you design a real-time distributed leader election mechanism using Apache ZooKeeper or etcd?

Executive Takeaway

Implement highly available active-passive worker failover: Ephemeral Sequential znodes in ZooKeeper, Leases and revision watches in etcd, and avoiding split-brain brain-freeze.

Architectural Breakdown & AnalysisFAANG Aligned

In distributed microservice clusters with background workers (e.g., scheduled cron runners, payment reconciliation workers, partition managers), running multiple concurrent active instances causes race conditions and data corruption. An automated Leader Election mechanism guarantees that exactly one active master executes work, with instant failover if the master crashes.\n\n### 1. Leader Election via Apache ZooKeeper (Ephemeral Sequential Nodes)\nZooKeeper maintains a hierarchical in-memory tree of nodes called znodes:\n\n1. Create Sequential Nodes: All candidate worker processes attempt to create an Ephemeral Sequential znode under a common parent path /election:\n /election/node-0000000001, /election/node-0000000002, /election/node-0000000003...\n2. Elect the Smallest Sequence: Each candidate queries children of /election. The node with the lowest sequence number is elected Leader.\n3. Avoid the Herd Effect (Watch Predecessor Only):\n * If node 2 is not the leader, it does *not* watch the leader node 1!\n * Instead, node 2 sets a Watch on its immediate predecessor: node 1.\n * Node 3 sets a Watch on node 2.\n * If the leader (node 1) dies, its ephemeral znode vanishes. Only node 2 is notified, promoting node 2 to leader without waking up all other hundred candidate nodes.\n\n### 2. Leader Election via etcd (Leases & Revision Watches)\netcd simplifies this using TTL Leases and atomic Key revisions:\n1. Grant Lease: Worker acquires an etcd lease with a 5-second TTL: etcdctl lease grant 5.\n2. Atomic Put with Lease: Worker attempts to create /services/leader attached to the lease using a transaction (Txn).\n3. Success: If successful, worker is Leader and continuously sends KeepAlive heartbeats to maintain the lease.\n4. Failover: If the leader crashes, heartbeats stop. After 5 seconds, etcd revokes the lease and deletes the key, triggering a Watch event on standby workers to elect a new leader.\n\n### Key Architectural Comparison\n| Coordination Engine | Node Primitive | Watch Model | Consensus Protocol | Production Tooling |

:---:---:---:---:---
etcdLeases with atomic Compare-and-SwapRevision stream watchesRaft consensusKubernetes Control Plane
HashiCorp ConsulSessions with Key LocksLong-polling HTTP blocking queriesRaft consensusNomad, Vault

| Redis (Redlock) | Mutex keys with TTL (SET NX EX) | Polling / PubSub | Single-instance lock (No strict consensus) | Lightweight web application locks |\n

Previous QuestionHow do you eliminate the Thundering Herd / Cache Stampede problem under massive concurrent load?

Continue Learning on Being SDE

Explore deep-dive system architecture modules, low-level object-oriented designs, and interview cheat sheets.