codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Subjects
🏗️

System Design

24 chapters

1System Design Basics2Vertical vs Horizontal Scaling3CAP Theorem4Load Balancers & Algorithms5Proxy Servers (Forward & Reverse)6Caching Strategies & Eviction7Content Delivery Networks (CDNs)8Database Replication9Database Sharding & Partitioning10Database Scaling & Sharding11Consistent Hashing12Choosing Databases (SQL vs NoSQL)13Message Queues (Kafka, RabbitMQ)14Microservices Architecture15API Gateways16Rate Limiting Algorithms17Long Polling vs WebSockets vs SSE18Heartbeat & Health Checks19Bloom Filters & Probabilistic Data Structures20Leader Election in Distributed Systems21Event-Driven Architecture22Distributed Locking23Circuit Breaker Pattern24Case Study: Design URL Shortener
SubjectsSystem Design

Leader Election in Distributed Systems

Updated 2026-04-21
3 min read

Leader Election in Distributed Systems

In many distributed systems, certain tasks (like writing to a shared resource, coordinating a workflow, or assigning work to other nodes) must be performed by exactly one node at any given time. This designated node is called the Leader. The process of choosing this leader from a group of candidate nodes is called Leader Election.

1. Why Leader Election?

  • Database Replication: In a Master-Slave setup, only the Master accepts writes. If the Master dies, a Slave must be automatically elected as the new Master.
  • Distributed Locking: A leader can serve as the authority for granting and revoking distributed locks.
  • Task Scheduling: A leader can assign tasks to worker nodes, ensuring no task is assigned twice.

2. The Bully Algorithm

One of the simplest leader election algorithms. Each node has a unique numerical ID.

  1. When a node detects that the current leader has failed, it initiates an election.
  2. It sends an "Election" message to all nodes with a higher ID.
  3. If no higher-ID node responds, the initiating node declares itself the leader and broadcasts a "Victory" message.
  4. If a higher-ID node responds, it takes over the election process. The node with the highest ID always wins (the "bully").

3. The Raft Consensus Algorithm

Raft is a modern, widely-used consensus algorithm designed for understandability. It ensures that a cluster of servers agrees on a leader and that the leader's log of operations is replicated correctly.

Roles:

  • Leader: Handles all client requests and replicates log entries to followers.
  • Follower: Passive nodes that respond to requests from the leader.
  • Candidate: A follower that has not heard from the leader recently and initiates an election.

Election Process:

  1. Followers have a randomized election timeout. If a follower doesn't receive a heartbeat from the leader before its timeout expires, it becomes a Candidate.
  2. The Candidate increments its term number, votes for itself, and sends "RequestVote" RPCs to all other nodes.
  3. If a Candidate receives votes from a majority of nodes, it becomes the new Leader.
  4. The new Leader immediately sends heartbeats to all followers to establish its authority and prevent new elections.

4. Using External Coordination Services

In practice, many systems delegate leader election to a dedicated coordination service rather than implementing it from scratch.

  • Apache ZooKeeper: Uses ephemeral sequential nodes. The node that creates the smallest sequence number becomes the leader. If the leader dies, its ephemeral node is automatically deleted, and the next node in sequence becomes the leader.
  • etcd: A distributed key-value store that provides built-in leader election primitives. Used heavily by Kubernetes.
  • Consul: Provides a simple leader election API using session-based locking.


PreviousBloom Filters & Probabilistic Data StructuresNextEvent-Driven Architecture

Recommended Gear

Bloom Filters & Probabilistic Data StructuresEvent-Driven Architecture