Load Balancers for Node
![NPM version](https://img.shields.io/npm/v/arrow-function-load-balancer.svg?style=flat)
Installation
npm install --save arrow-function-load-balancer
Comparison of Load Balancers
- The Random Balancer is a bit chaotic; it doesn't distribute requests as evenly as one would think because there's no such thing as perfect randomness.
- The Power of Two Choices (P2c) Balancer comes very close to the ideal load balancer. Use the P2c balancer over the random balancer.
![Comparison of load balancing algorithms](https://raw.githubusercontent.com/paulborza/arrow-function-load-balancer/master/docs/errors.png)
The chart above depicts 10,000 requests routed to five proxies (exactly like in the following code sample).
Then the numer of requests are normalized to 100%. Since there are five proxies, each proxy should receive 20% of the traffic.
But notice that's not the case with the random load balancing algorithm.
That's why the power of two choices (P2c) load balancing algorithm is recommended over the random load balancing algorithm.
Usage
import {
P2cBalancer,
RandomBalancer,
} from 'arrow-function-load-balancer';
const proxies = [
'http://proxy1.arrowfunction.com/',
'http://proxy2.arrowfunction.com/',
'http://proxy3.arrowfunction.com/',
'http://proxy4.arrowfunction.com/',
'http://proxy5.arrowfunction.com/',
];
const balancer = new P2cBalancer(proxies.length);
for (let i = 0; i < 10000; i++) {
const proxy = proxies[balancer.pick()];
console.log(proxy);
}
Contributing
Got a new load balancing algorithm you'd like to see implemented in this package?
Please go ahead and create a work item for me; or better yet, send a pull request and I'll be sure to take a look at it within 24 hours. Thanks!