Load Balancing in Nodejs

2019-05-06 22:48发布

问题:

I recently started with node and I have been reading a lot about its limitation of it being single threaded and how it does not utilise your cores and then I read this

http://bit.ly/1n2YW68 (which talk about the new cluster module of nodejs for loadbalancing)

Now I'm not sure I completely agree to it :) because the first thing that I thought of before starting with node on how to make it utilise cores with proper load balancing is via web-server some like upstream module like nginx

like doing something like this

 upstream domain1 {
   server http://nodeapp1;
   server http://nodeapp2;
   server http://nodeapp3;
 }

So my question is there an advantage to use such cluster module for load balancing to utilise the cores does it has any significant advantage over web server load balancing or is blog post too far from real use.

Note: I'm ain't concerned about load balancing handle by various app server like passenger(passenger has nodejs support as well but something that I'm not looking for answer :)) which I already know since I'm mostly a ruby programmer

回答1:

One other option you can use to cluster NodeJs applications is to deploy the app using PM2.

Clustering is just easy as this, You don't need to implement clustering by hand

pm2 start app.js -i max

PM2 is an expert to auto detect the number of available CPUs and run as many processes as possible

Read about PM2 cluster mode here http://pm2.keymetrics.io/docs/usage/cluster-mode/

For controlling the load of IO operations, I wrote a library called QueueP using the memoization concept. You can even customize the memoization logic and gain speedup values of more than 10, sometimes

https://www.npmjs.com/package/queuep



回答2:

As far as I know, the built in node cluster is not a good solution yet (load is not evenly distributed across cores). Until v0.12: http://strongloop.com/strongblog/whats-new-in-node-js-v0-12-cluster-round-robin-load-balancing/

So you should use nginx until then. After that we will see some benchmarks comparing both options and see if the built in cluster module is a good choice.