Follow the Leader!
The zero-conf leader election library using Bonjour for service discovery.
npm install followtheleader -S
;console.log'Starting leader election';;elector.on'leader',;elector.on'follower',;elector.on'reelection',;elector.on'error',;elector.start;
// ES6;;// CommonJS;;
type(string, optional) - defaults to 'udp'
port(number, optional) - defaults to 9001
host(string, optional) - defaults to '0.0.0.0'
maxFailedResponses(number, optional) - defaults to 3
The option values can either by provided as arguments or as an object.
Starting election process
Starts the election process.
If the node is elected leader it will start a heartbeat monitor that binds an UDP listener to the specified
If the node is elected follower, it will ping the master node. If
maxFailedResponses is reached, the node will restart the election process.
The node has been elected leader.
The node has been elected follower.
The master node has become unavailable and the election process is restarted.
An error occurred somewhere during the election proces or in the heartbeat monitor.
Zero-conf / Bonjour / mDNS service discovery will only work for nodes that are on the same network subnet mask. If your nodes run on servers spreading multiple geographical locations, on different networks or if you are using a diffferent VLAN per server, you should look for other solutions. A good alternative would be Elector which uses ZooKeeper.
If you are running your application in Amazon Web Services ECS clusters, you might want to try configuring your tasks with the
awsvpc network mode. This will give each task their own elastic IP within the selected VPC subnet. This will only work if all ECS nodes are running in the same Availability Zone and subnet. If your cluster is running nodes in multiple Availability Zones, Zero-config/Bonjour/mDNS will not work.
If you're running Kubernetes, you should look into the Kubernetes/Contrib election solution.