Leader Election in Distributed Systems.

Photo by Jake Hill on Unsplash

Leader Election in Distributed Systems.

An Introduction to leader election.

The first time I came across leader election in distributed systems, I found it interesting; the idea that machines have a leader, got me thinking about the Terminator movies. I was invested, so I did some research to know what it was about. Now, I am able to share a bit of that knowledge here.

The scope of this article is to provide a basic understanding of leader election and leader election algorithms, In this article, we will be briefly talking about what leader election is, when to use it and some of the widely used algorithms to achieve leader election in distributed systems.

What is a distributed system?

A distributed system is simply a network of interconnected computers that work together to achieve a common goal. They appear as a single coherent system to the users. Distributed systems are all around you, some examples include social media networks, online gaming systems, financial systems, and content delivery networks amongst others.

Distributed systems play a major role in modern technology, they are the backbone for a lot of services provided online today.

What is leader election?

In simple terms, leader election in distributed systems is the selection of one node among other potential nodes to be the leader of the group of nodes. Once a leader is elected, it has special responsibilities such as coordinating actions across the nodes, assigning tasks and making decisions for the group.

Leader election can help improve performance and efficiency in distributed systems.

There are multiple mechanisms by which a new leader is chosen, in this post we are not going to delve deeply into those but give an easy-to-understand explanation.

When to use Leader election?

Leader election is a good approach, however, it's not always the best fit for your solution. You need to carefully review your usecase and be sure if you need to use certain technologies. Leader election is not always the answer.

Consider using Leader election when you are designing a distributed system with distributed writes, this can pose a problem if you aim to achieve consistent data and avoid multiple writes of the same data.

We can also use leader election in processing distributed tasks, you may need a single node that is responsible for delegating tasks to other nodes and keeping tabs on the progress of the tasks shared across the nodes.

For example, if on your distributed system, you are building a subscription service that periodically charges credit cards, it's possible to have multiple nodes trying to charge the same card at the same time, which is wrong, the customer would probably be livid if this happens. With leader election, we can avoid this by appointing a single node with the responsibility of charging the cards.

When do we need to elect a new leader?

If for some reason our leader is not able to function, we would need a new leader (we can't go on without a leader right? There would be chaos. LOL), and someone has to step up to assume the role.

The leader might suddenly be unavailable due to a network issue or the node itself crashes among a host of other possible causes for the leader to go down, a new leader would have to be selected from the available followers.

How do we know when a leader is unavailable?

The elected leader periodically sends out a 'heartbeat' to its followers to indicate its liveness, the heartbeat is sent at intervals, and the interval can be configurable. For example, if the interval is every 2 seconds, all other nodes know to expect a heartbeat every 2 seconds, if a heartbeat is not received then the other nodes know that something is wrong, and the leader is not available, so it's time to elect a new leader.

The leader election method should be resilient enough to handle network issues and process failures gracefully and promptly. As the system scales, the election process should remain efficient as well.

Leader election algorithms.

We already established that a new leader would be selected whenever the current leader is unavailable, but how do we actually elect these leaders, I'll briefly explain a few of the most common approaches to doing this, there are other techniques and even a lot of ongoing research on innovative ways of selecting a leader.

Bully algorithm

With the Bully algorithm, each node has a unique (integer) identifier, sort of like a rank, no two nodes can have the same rank. Each node starts out assuming it is the leader, so they broadcast their rank to all the other available nodes, if a node receives a message from a higher-ranking node, the lower-ranking node backs down. After this process is completed, a single node emerges as the leader. If the current leader goes down, the election process is carried out again and at the end of the process, the next highest-ranking available node takes over.

Imagine if these nodes are in the army and the current leader of the platoon is a lieutenant. If, for some reason, the lieutenant is incapable of performing his duties, then the next highest-ranking officer takes charge. At ease, soldier.

Ring algorithm

The Ring algorithm is a little similar to the Bully algorithm in the sense that each node is also assigned a unique identifier, and that the highest-ranking node is appointed the leader.

The nodes are arranged in a logical ring, where each node is linked to the next, if the current leader goes down, another node detects the failure and initiates the election process. The current node communicates its rank via a message to its neighbouring node, the neighbour node then compares its rank with that of the initiating node, and whichever rank is greater is appended to the message and sent to the next available node in the ring. This process continues throughout the ring until the message circles back to the initiating node, by then we would already know the highest-ranking node, and it then is appointed the leader.

The Bully and Ring algorithms are very similar, the major difference is their approach to communicating ranks, Bully uses a broadcast system, while Ring only sends messages to the next node(s) linked to it.

Paxos algorithm

In simple terms, the Paxos algorithm is a consensus algorithm, meaning the nodes have to agree as to which of the nodes is elected leader. This consensus is reached after a series of processes/phases including the preparation phase, the proposal phase and the acceptance phase.

The Paxos algorithm is known for being fault-tolerant, efficient and consistent in distributed systems.

Raft algorithm

The Raft algorithm is also a consensus algorithm similar to Paxos, although it's newer and simpler to implement

Raft uses a term-based approach to leader election, when a leader's term has expired, a new election is held. To elect a new leader, candidates (nodes that wish to become the leader) send vote requests to the other nodes. Through a series of message exchanges, a leader is elected.

Raft is also very fault-tolerant, it is gaining more ground due to its ease of understanding and implementation.

Etcd (pronounced etsy-d) is a popular key-value store that uses the Raft algorithm internally.

The choice of algorithm to use depends on the specific requirements of the distributed system in question, each of the above has its strengths and weaknesses. Factors such as performance, system size, complexity, and fault-tolerant requirements among other factors need to be considered before selecting an algorithm to use.

Conclusion

In summary, Leader election is a process in distributed systems to select a node to act as a leader to the other nodes, the leader node is responsible for handling specific tasks and coordinating the other nodes, a leader is typically in 'power' until it becomes unavailable, then a new leader is selected. Leaders are selected using leader election algorithms, examples of these include Bully, Paxos, Raft etc.

I hope this article is able to give you a basic understanding of what leader election is in distributed systems, please do some further research online if you need some more information not included here. Cheers.