Leader Election using hazelcast

Anil Kurmi
Microservices Architecture
3 min readApr 8, 2023

Hazelcast uses the Raft consensus algorithm to implement leader election in its distributed systems. In the Raft protocol, each node in the cluster can be in one of three states: leader, follower, or candidate.

The leader is responsible for managing the distributed system and making decisions, while the followers and candidates participate in the election process. When a leader fails, the remaining nodes in the cluster initiate a new leader election process.

In Hazelcast, the Raft protocol is used to elect a new leader for a partition when the current leader fails. Each partition in the cluster has a Raft group that is responsible for managing the partition’s state and electing a leader for the partition.

When a leader fails, the Raft group initiates a new leader election process. The process starts with a follower becoming a candidate and requesting votes from the other nodes in the cluster. The candidate must receive votes from a majority of the nodes in the cluster to become the new leader.

Once the candidate becomes the leader, it updates the state of the partition and begins serving requests from clients. The new leader also sends a notification to all the nodes in the cluster, informing them of the change in leadership.

Overall, the Raft consensus algorithm provides a fault-tolerant and reliable way to elect a new leader in Hazelcast’s distributed systems. By using Raft, Hazelcast ensures that the distributed system can continue to operate even when some nodes fail, without sacrificing data consistency or system availability.

Leader Election using Lock Service

public class LeaderElectionExample {
private static final String LOCK_NAME = "leader-lock";

public static void main(String[] args) {
Config config = new Config();
HazelcastInstance hazelcastInstance = Hazelcast.newHazelcastInstance(config);

FencedLockConfig lockConfig = new FencedLockConfig();
lockConfig.setName(LOCK_NAME);
FencedLockService lockService = hazelcastInstance.getCPSubsystem().getLockService();
FencedLock lock = lockService.createFencedLock(lockConfig);

try {
lock.lock();

// This node is now the leader, do leader-specific tasks here
// Get data from database etc

} finally {
lock.unlock();
}
}
}

Leader election using oldest member


public class LeaderElectionExample implements MembershipListener {
private HazelcastInstance hazelcastInstance;
private String leaderAddress;

public static void main(String[] args) {
LeaderElectionExample example = new LeaderElectionExample();
example.start();
}

public void start() {
hazelcastInstance = Hazelcast.newHazelcastInstance();
hazelcastInstance.getCluster().addMembershipListener(this);
leaderAddress = hazelcastInstance.getCluster().getMembers().iterator().next().getSocketAddress().toString();

if (hazelcastInstance.getCluster().getLocalMember().getSocketAddress().toString().equals(leaderAddress)) {
// This node is the leader, do leader-specific tasks here
}
}

@Override
public void memberAdded(MembershipEvent membershipEvent) {
if (membershipEvent.getMember().getSocketAddress().toString().compareTo(leaderAddress) < 0) {
// A new member has joined that has an older address, it becomes the new leader
leaderAddress = membershipEvent.getMember().getSocketAddress().toString();
if (hazelcastInstance.getCluster().getLocalMember().getSocketAddress().toString().equals(leaderAddress)) {
// This node is the new leader, do leader-specific tasks here
}
}
}

@Override
public void memberRemoved(MembershipEvent membershipEvent) {
if (membershipEvent.getMember().getSocketAddress().toString().equals(leaderAddress)) {
// The leader has left the cluster, a new leader will be elected
leaderAddress = hazelcastInstance.getCluster().getMembers().iterator().next().getSocketAddress().toString();
if (hazelcastInstance.getCluster().getLocalMember().getSocketAddress().toString().equals(leaderAddress)) {
// This node is the new leader, do leader-specific tasks here
}
}
}
}

--

--