Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Horizontal scaling of node.js server instances on a single machine

Running a web server on node.js is a simple thing to do (as seen by its excellent examples and documentation) but I wonder how you can fully use the CPU resources of a dedicated server?

Since node.js is single-threaded the only way to take advantage of multiple processors is via multiple processes. Of course, only one process can bind to a port so it seems there would have to be a master/worker pattern wherein the master forks children, binds to the incoming port, and delegates incoming connections (and the actual processing work) to the children. (Perhaps via a hungry-consumer pattern?)

Is this the best way to scale a web server running node.js? If so, are there libraries to simplify the master/worker pattern? If not, what patterns or deployment setups are recommended to best use the entire resources of a dedicated machine?

(Is this a better question for ServerFault?)

like image 389
maerics Avatar asked Jun 15 '11 17:06

maerics


2 Answers

Multi-node is a library that provides the master/worker pattern.

If the server processes don't need to be able to talk to each other, and you aren't using Socket.IO, a simple option would be to just start one process/core, bind to local ports, and use something like nginx or HAProxy to load balance between them.

like image 57
James Socol Avatar answered Sep 16 '22 11:09

James Socol


If you're using express, I'd use tj's Cluster: http://learnboost.github.com/cluster/

It provides 'transparent' cpu based load balancing, which is nice because you can use your existing express app, and it scales it across cores relatively painlessly.

like image 45
Josh Avatar answered Sep 19 '22 11:09

Josh