What are the best methods/algorithms available to select a leading node in a cluster of nodes, knowing that nodes come up and down at anytime? If implementations are available in Java, it is a plus.
-
What do you mean by `leading` ? And can you give more details on what you mean by `node` ? – Andre Holzner Apr 28 '11 at 20:44
-
Isn't this heavily dependent on your usage scenario? I mean what is meant by leader in your use case? The one in the middle of a cluster? The largest? The youngest? – pintxo Apr 28 '11 at 20:45
-
In multi-node networking, the leader is what node is going to have the authoritative data. I.e. their data is better than yours. For example, if you submit data to node A, it has to send that data to nodes B and C. If I (1 nano second later) submit data to node D, before it has a chance to reach B and C from A, there needs to be a way to know that my data is better. In this case, D is the leader node because it has the latest data write. – corsiKa Apr 28 '11 at 20:49
-
Some data shared and replicated across nodes can be modified by multiple nodes. In order to avoid incoherent data versions across the cluster, one solution is to have it modified by one node having the authority and replicate as necessary. – Jérôme Verstrynge Apr 28 '11 at 20:57
3 Answers
I've implemented the Paxos algorith before in Java. It's very useful, and fairly simple. (It took about 16 hours to put together a demo of it, using Threads to simulate servers. I was also much worse at threading then!)
It won't help you select the leader exactly... but what it will do is allow the various nodes to agree on a leader. So you have this leader selection algorithm, but because each node is going to pick it's own node to lead, you may find a "civil war" among your nodes. The Paxos algorithm allows you to say which picked leader is the real leader.

- 81,495
- 25
- 153
- 204
-
Thanks. I have questions about Paxos implementation, but I'll create a separate question. – Jérôme Verstrynge May 01 '11 at 17:41
You can look through the sources of JGroups. (keyword: "coordinator", check out chapter 7 in jGroups manual)

- 8,956
- 5
- 47
- 47
Some options:
- Hazelcast - Cluster leader election with Spring Integration and Hazelcast.
- JGroups - Cluster leader election with JGroups
- Apache ZooKeeper - Cluster leader election with ZooKeeper.
I have personally implemented it with Hazelcast and JGroups and say that both were fairly straight-forward and simple. For a new project, I'd go with Hazelcast.

- 8,956
- 5
- 47
- 47