How to elect a master node among the nodes running in a cluster?
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
In distributed systems, particularly those utilizing a cluster of nodes, the election of a master node is a critical function. The master node is responsible for managerial tasks such as orchestrating processes, managing data distribution, and coordinating actions among other nodes. In this article, we will explore various methods to elect a master node in a cluster, delving into technical explanations and examples to illustrate each method.
Understanding Master Node Elections
Why Elect a Master Node?
- Coordination: The master node coordinates tasks and processes.
- Resource Allocation: It manages resources, ensuring optimal distribution.
- Consistency: Ensures data consistency across the cluster.
- Failure Recovery: Aids in recovery by re-electing a new master during failures.
Challenges in Electing a Master Node
- Consensus: Achieving a consensus among distributed nodes.
- Latency: Minimizing network latency during the election process.
- Reliability: Ensuring a reliable and fault-tolerant election process.
- Duplicate Masters: Preventing the emergence of multiple master nodes due to network partitions.
Methods for Electing a Master Node
1. Bully Algorithm
The Bully algorithm is a classic method used to elect a master node in a distributed system. Here's how it works:
- Initialization: All nodes in the cluster have a unique numerical ID.
- Election Trigger: When a node realizes the current master node has failed or is unreachable, it initiates an election.
- Election Message: Higher ID nodes send an election message to other nodes with higher IDs.
- Victory Message: The highest ID node that doesn't receive any higher ID election messages declares itself as the master and sends a victory message to all nodes.
Pros:
- Simple and easy to implement.
Cons:
- High network traffic during the election process.
- Not efficient in large clusters due to numerous messages.
2. Raft Consensus Algorithm
The Raft algorithm is designed to solve the consensus problem, making it ideal for electing a master node:
- Term-Based Voting: A server is elected based on obtaining votes for the current term.
- Leader Election: Nodes start as followers. When a node doesn't hear from a leader, it becomes a candidate and solicits votes from other nodes.
- Majority Wins: A node becomes a leader by gaining majority support.
Pros:
- Easy to understand compared to other consensus algorithms.
- Provides a clear structure for managing node failure and re-election.
Cons:
- Requires a strict majority, limiting operation if many nodes are down.
3. ZooKeeper's Leader Election
ZooKeeper, a distributed coordination service, includes a built-in leader election protocol:
- ZNodes: Nodes create ephemeral sequential ZNodes.
- Lowest ZNode: The node with the smallest ZNode ID becomes the leader.
- Watch Mechanism: Non-leader nodes watch nearby nodes for failure to trigger re-election.
Pros:
- Highly reliable and adaptable to failure.
- Simplifies complex coordination tasks.
Cons:
- Relies heavily on ZooKeeper's infrastructure, requiring extra overhead.
4. Paxos Algorithm
The Paxos algorithm, although complex, provides a robust method for reaching consensus:
- Proposals: Nodes propose values and use acceptors to agree on one.
- Quorum: Requires a majority to accept a proposal, ensuring consensus.
Pros:
- Guarantees consistency even in the face of failures.
Cons:
- Complex to implement and understand.
- Requires careful handling of node failures to prevent deadlock.
Summary Table
| Algorithm | Pros | Cons |
| Bully | Simple implementation | High network traffic; inefficient in large clusters |
| Raft | Easy to understand; structured failure handling | Requires majority, limiting operations if many nodes are down |
| ZooKeeper | Highly reliable; simplifies coordination tasks | Relies on ZooKeeper infrastructure, introducing overhead |
| Paxos | Guarantees consistency amid failures | Complex implementation; risk of deadlock if not handled properly |
Conclusion
Electing a master node is a fundamental task for maintaining the efficacy and stability of a cluster. The choice of algorithm depends on the specific requirements of the system, including cluster size, network reliability, and tolerance for complexity. Understanding each method's strengths and weaknesses ensures a thoughtful and informed decision for your distributed system's architecture.
Further Reading
To deepen your understanding of these algorithms, consider reviewing the following resources:
- Distributed Algorithms by Nancy A. Lynch
- The Art of Multiprocessor Programming by Maurice Herlihy and Nir Shavit
- Research papers detailing specific algorithms, available through academic databases like IEEE and ACM.
Each method brings unique benefits and challenges, and further study will enable you to tailor the most suitable solution for your distributed system's needs.
Related reading
- How to enable HTTP response caching in Spring Boot
- How to enable streaming replication in PostgreSQL running in kubernetes pods?
- How to ensure data consistency in Cassandra on different tables?
- How to establish a consistent hash ring with 300 million virtual nodes within 10 seconds with C++
- How To Estimate the total time to complete the request In UDP and TCP ( Distributed Systems)
- how to evict hazelcast caches manually or on a future timestamp
- How to exclude some tables from RDS Mysql replication
- How to externalize adapter artifact in Hexagonal Architecture?

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseSystem Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.