Bully Algorithm in Distributed System (original) (raw)

Last Updated : 23 Jul, 2025

The Bully Algorithm is a popular method for electing a coordinator in distributed systems. When nodes or processes in a system need a leader to manage tasks or make decisions, this algorithm helps them choose one, even if some nodes fail. The process involves nodes "bullying" each other by checking who has the highest ID, and the one with the highest ID becomes the coordinator.

Bully-Algorithm-in-Distributed-System

Bully Algorithm in Distributed System

Table of Content

What is the Leader Election Algorithm?

A crucial procedure in distributed systems is leader election, in which nodes (computers or processes) choose a leader from among themselves. Frequently, the group's leader takes on duties including decision-making, resource distribution, and coordination.

**The election algorithm is based on the following assumptions:

What is the Bully Algorithm for Leader Node Election?

The Bully algorithm is a popular technique for choosing a leader in distributed networks that uses the greater priority concept. It operates as follows:

Messages in Bully Algorithm for Leader Node Election

There can be three types of messages that processes exchange with each other in the bully algorithm:

Steps Involved in Bully Algorithm for Leader Node Election

Lets understand the steps involved in bully algorithm for leader Node Election through an example. The example follows all the assumptions discussed above in the Election Algorithm. Let's say there are 6 Processes P0, P1, P2, P3, P4, P5 written in ascending order of their Process ID (i.e.,0, 1,2,3,4,5).

Bully Algorithm Implementation

Below is the code of bully algorithm in java:

Java `

import java.util.ArrayList; import java.util.List;

// Class representing a node in the distributed system class Node { private int nodeId; private boolean isCoordinator;

public Node(int nodeId) {
    this.nodeId = nodeId;
    this.isCoordinator = false;
}

public int getNodeId() {
    return nodeId;
}

public boolean isCoordinator() {
    return isCoordinator;
}

public void setCoordinator(boolean coordinator) {
    isCoordinator = coordinator;
}

// Method to initiate an election
public void initiateElection(List<Node> nodes) {
    System.out.println("Node " + nodeId + " initiates election.");

    for (Node node : nodes) {
        if (node.getNodeId() > this.nodeId) {
            // Send election message to higher priority nodes
            node.receiveElectionMessage(this);
        }
    }

    // Assume election process completes after initiating
    becomeCoordinator();
}

// Method to receive election message from another node
public void receiveElectionMessage(Node sender) {
    System.out.println("Node " + nodeId + " receives election message from Node " + sender.getNodeId());

    // Respond if current node has higher priority
    if (this.nodeId > sender.getNodeId()) {
        System.out.println("Node " + nodeId + " responds to Node " + sender.getNodeId());
        sender.receiveResponse(this);
    }
}

// Method to receive response and acknowledge as coordinator
public void receiveResponse(Node sender) {
    System.out.println("Node " + nodeId + " receives response from Node " + sender.getNodeId());
}

// Method to become the coordinator
public void becomeCoordinator() {
    System.out.println("Node " + nodeId + " becomes the coordinator.");
    this.isCoordinator = true;
}

}

public class BullyAlgorithmExample {

public static void main(String[] args) {
    // Create nodes
    Node node1 = new Node(1);
    Node node2 = new Node(2);
    Node node3 = new Node(3);
    Node node4 = new Node(4);
    Node node5 = new Node(5);

    // List of nodes in the distributed system
    List<Node> nodes = new ArrayList<>();
    nodes.add(node1);
    nodes.add(node2);
    nodes.add(node3);
    nodes.add(node4);
    nodes.add(node5);

    // Simulate failure of current coordinator (Node 3)
    node3.setCoordinator(false);

    // Assume Node 3 detects coordinator failure and initiates election
    node3.initiateElection(nodes);
}

}

`

Explanation of the Code:

This implementation is simplified and assumes a basic scenario. In practice, you would need to handle network communication, message exchange protocols, and potentially more complex failure scenarios to make the algorithm robust for real-world distributed systems.

Practical Applications of Bully Algorithm for Leader Election

Despite its simplicity, the Bully Algorithm finds useful applications in a variety of distributed systems situations where the election of a leader is essential to preserving system coordination and functionality. Here are a few real-world applications:

Benefits of the Bully Algorithm for Leader Election

Below are the benefits of bully algorithm:

Challenges of the Bully Algorithm for Leader Election

Below are the challenges of Bully Algorithm:

Conclusion

In distributed systems, the Bully Algorithm is a simple and fault-tolerant leader election technique. It performs well in small- to medium-sized networks, maintaining order in the event of a leader failure. However, it lacks preemption capabilities and its efficiency can drop in larger networks.