Leader Election is the process by which a distributed system designates a single "coordinator" node to manage tasks, maintain order, and ensure data consistency across the cluster.
Simulates nodes electing a leader based on the highest ID. Click a node to crash/recover it.
In a distributed system where many computers work together, chaos is the default state. Without a coordinator, you face the "Split-Brain" problem where two parts of a system fight for control, leading to data corruption.
The leader acts as the single source of truth for write operations, ensuring all followers have the same data order.
Instead of every node trying to schedule tasks (causing conflicts), the leader assigns work to available nodes.
Uses Raft to elect a leader that manages the entire cluster state.
Elects a Controller Broker to manage partition leaders and replicas.
Replica Sets elect a Primary node to handle all write operations.
This is a simplified implementation of the Bully Algorithm. Click on the code lines to understand exactly what's happening at each step.
Click any line in the code block to see a detailed explanation of its logic.
class Node:def __init__(self, node_id, peers):self.id = node_idself.peers = peersself.state = 'FOLLOWER'def check_leader(self):if not self.receive_heartbeat():self.start_election()def start_election(self):self.state = 'CANDIDATE'higher_nodes = [p for p in self.peers if p.id > self.id]if not higher_nodes:self.become_leader()else:for p in higher_nodes:p.send_message("ELECTION")def receive_message(self, type, sender_id):if type == "ELECTION" and self.id > sender_id:self.send_message(sender_id, "OK")self.start_election() # I am bigger, I take overelif type == "VICTORY":self.current_leader = sender_idself.state = 'FOLLOWER'