2.0.9 • Published 5 years ago

followtheleader v2.0.9

Weekly downloads
2
License
MIT
Repository
github
Last release
5 years ago

Follow the Leader!

The zero-conf leader election library using Bonjour for service discovery.

CircleCI

Installation

npm install followtheleader -S

Usage

import { elect, Elector } from 'followtheleader';

console.log('Starting leader election');
const elector: Elector = elect("MyServiceName");
elector.on('leader', () => {
  console.log('This node was elected as leader');
});
elector.on('follower', () => {
  console.log('this node was elected as follower');
});
elector.on('reelection', () => {
  console.log('master node is unavailable, restarting election process');
});
elector.on('error', (error) => {
  console.log(error);
});
elector.start();

API

Initializing

// ES6
import { elect, Elector } from 'followtheleader';
const elector = elect(options));

// CommonJS
const ftl = require('followtheleader');
const elector = ftl.elect(options);

options are:

  • name (string, required)
  • 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

elector.start()

Starts the election process. If the node is elected leader it will start a heartbeat monitor that binds an UDP listener to the specified port and host. If the node is elected follower, it will ping the master node. If maxFailedResponses is reached, the node will restart the election process.

Event: leader

The node has been elected leader.

Event: follower

The node has been elected follower.

Event: reelection

The master node has become unavailable and the election process is restarted.

Event: error

An error occurred somewhere during the election proces or in the heartbeat monitor.

Caveats

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.

License

MIT

2.0.9

5 years ago

2.0.8

6 years ago

2.0.7

6 years ago

2.0.6

6 years ago

2.0.5

6 years ago

2.0.4

6 years ago

2.0.3

6 years ago

2.0.2

6 years ago

2.0.1

6 years ago

2.0.0

6 years ago

1.0.5

6 years ago

1.0.4

6 years ago

1.0.3

6 years ago

1.0.2

6 years ago

1.0.1

6 years ago

1.0.0

6 years ago