Unit II Leadership Election Algorithm
Unit II Leadership Election Algorithm
A Leader Election Algorithm is a distributed algorithm used in networked systems to select a single
process (leader) among multiple distributed processes. The leader is responsible for coordination,
decision-making, and resource allocation in a distributed system.
Need for Leader Election Algorithm: In distributed systems, there is no central authority, so
leader election ensures:
Avoidance of conflicts (e.g., multiple processes trying to access the same resource).
2. Election Criteria: The leader is chosen based on factors like process ID, computational
power, or priority.
1. Bully Algorithm:
2. Ring Algorithm:
Types:
1. Bully Algorithm : The Bully Algorithm is a leader election algorithm used in distributed
systems where the process with the highest ID becomes the leader. When a leader fails, a
new election is initiated by any process that detects the failure. It sends election messages
to all higher-ID processes, and if no higher process responds, it declares itself the new
leader.
Steps:
Let's assume six processes in a distributed system, each having a unique ID (1 to 6). The highest
ID process is always the leader.
Initial Setup
P1 Alive Lowest ID
Process ID Status Notes
P2 Alive -
P3 Alive -
P4 Alive -
P5 Alive -
Step-by-Step Execution
1⃣ P6 (Leader) Fails:
2⃣ P3 Starts an Election:
P3 realizes P6 is down.
3⃣ P4 and P5 Respond:
P4 and P5 (higher ID than P3) respond, meaning they take over the election.
Final State
Process ID Status Notes
P1 Alive -
P2 Alive -
P6 Failed No Response
Important points:
Imagine a group of employees in an office deciding on a team leader when their manager is
suddenly unavailable. The person with the highest experience (ID) should take over.
Participants:
Important points:
This is exactly how the Bully Algorithm works in distributed systems but applied in a real-life
office scenario.
2. Ring Algorithm : The Ring Algorithm is a leader election algorithm where processes are
arranged in a logical ring ring (each process knows its next neighbor). When a leader fails, an
election message is circulated around the ring, collecting process IDs. The process with the
highest ID is elected as the new leader and announces itself to all nodes. A leader (coordinator)
is elected to manage shared resources or coordinate tasks.
Steps:
1. Process Initiation:
o Any process can initiate the election if it detects a failure of the current leader.
o The initiator sends an ELECTION message to the next process in the ring.
o Each process appends its own ID and forwards it.
3. Leader Selection:
o When the message returns to the initiator, the process with the highest ID is the
new leader.
o The leader sends a COORDINATOR message around the ring to inform all
processes.
Example
P1 → P2 → P3 → P4 → P1 (ring structure)
Since P4 is the highest, it sends COORDINATOR (P4) message around the ring.
Pros Cons
2. Raft Algorithm: The Raft Algorithm is a consensus algorithm used in distributed systems to
elect a leader through majority voting. It works by dividing time into terms and using voting
mechanisms. If a follower does not receive heartbeats from the leader, it becomes a candidate
and requests votes. The process that receives a majority of votes becomes the new leader and
maintains control by periodically sending heartbeats. Used in distributed databases (e.g., etcd,
Consul).
Roles in Raft
Election Process
1. Term-Based Election:
2. Majority Voting:
3. Leader Announcing:
Example
Pros Cons
Important Points:
Use Bully Algorithm if you need fast elections and have a fully connected network.
Use Ring Algorithm if you have a ring topology and need simple election handling.
Use Raft Algorithm in large-scale distributed systems where fault tolerance and
automatic failover are needed (e.g., databases like etcd, Consul).