Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Load balancing since Node v0.12.2 - cluster, pm2 or nginx

With Node v0.12.2, the cluster module supports Round-Robin (RR) load balancing, which ensures load is more evenly distributed than the previous OS-level load balancing.

So now we are spoilt for choice:

  1. Use the cluster module
  2. Use pm2 which uses the cluster module under the hood
  3. Use nginx
  4. Use HAProxy

I am aware of this excellent post as well as other answers here on SO, but none have addressed the newer Cluster module with RR mode. So the question boils down to:

Judging only on their load balancing capabilities, should I use pm2 or nginx?

like image 431
dayuloli Avatar asked Apr 09 '15 16:04

dayuloli


People also ask

Does PM2 do load balancing?

PM2 is a production process manager for Node. js applications with a built-in load balancer. It allows you to keep applications alive forever, to reload them without downtime and to facilitate common system admin tasks.

Does PM2 use nginx?

This is a common method to use NGINX as a HTTP proxy front of PM2. NGINX will allow to serve static files rapidly, manage the SSL protocol and redirect the traffic to your Node.

What is node load balancing?

Each load balancer node distributes its share of the traffic across the registered targets in its scope. If cross-zone load balancing is enabled, each of the 10 targets receives 10% of the traffic. This is because each load balancer node can route its 50% of the client traffic to all 10 targets.


1 Answers

TL;DR

If it's just pm2 vs. nginx go for nginx. Better: both. Best: a even broader setup.

If you want the most mature load balancing features use HAProxy. It's do one thing best. You'll get SSL-termination, ACLs and it being very lightweight. I can't prove with numbers, but I feel it has the lowest hit on http requests. A good read is this.

If you also need to serve (at least some) static content your goto option is nginx for its superior capabilities in this field. Also of your list it's the only one to provide such a feature. Except for node itself, but doing it very poorly.

pm2 feels very heavy-weight IMO and tends to break more often. It's capabilities of process load balancing are very good and secures uptime of your node process. It abstracts cluster.

cluster feels just 'being fixed' to a bare minimum. In the past and maybe still the os-level support lead to different behaviours on different platforms. For example, bias to single processes.

My current setup is:

  • HAProxy for cluster wide loadbalancing, including balancing several instances of a process per machine
  • CDN for static content (e.g. Cloudinary)
  • pm2 for process load balancing
like image 144
eljefedelrodeodeljefe Avatar answered Sep 22 '22 01:09

eljefedelrodeodeljefe