2

I am wanting to scale on a multi-core machine. Using the cluster library is an option, but I would like to know if I can do this a more simple way: by simply running multiple servers.

So what I want to know is this: does Node.js automatically spread servers over multiple cores. So if I have 5 cores, and 5 servers, would each server run on each core.

If so, would this be a viable way of scaling on a multi-core machine?

Luke
  • 2,038
  • 3
  • 22
  • 46
  • 1
    Two things worth looking into: (1) Do you understand system processes, and how the OS schedules process execution across the available cores? (2) Have you tried your own hypothesis of running multiple servers of the same application? – E_net4 Nov 04 '16 at 22:59
  • Check out : [Node.js on multi core machines](http://stackoverflow.com/questions/2387724/node-js-on-multi-core-machines?rq=1). Second answer. – 108 Nov 04 '16 at 23:00
  • @108 From the second answer: "you should run multiple Node.js servers on one box, one per core". Does this mean that it's automatic? Or do you have do something in particular for this to happen? (Also, is it version specific?) – Luke Nov 04 '16 at 23:07
  • It would seem that this approach requires virtualization and assigning 1 dedicated core per virtual machine. The alternative is clustering. Found this [article](http://mobilenext.net/scaling-node-js-multi-core-systems/). However, by running multiple servers you're already running multiple processes so they can utilize the separate core better. – 108 Nov 04 '16 at 23:16
  • 1
    Indeed, just running multiple instances of a node app, will use multiple processes. But one note, the cluster option has one slight advantage, and that's if your creating something that listens on a TcpIP port, the cluster can share a single IP port,.. eg. If say you was running a webserver on port 80, you could use all cores that run on port 80.. What I tend to do though, is run a reverse proxy on Port 80, and have other processes running on different ports. IOW: The cluster option is great for creating a reverse proxy.. :) – Keith Nov 04 '16 at 23:17
  • @108 Awesome! I don't need all of them to use the same port, so it looks like running multiple servers is the best option for me. You (or Keith) post an answer and I'll accept it :). – Luke Nov 05 '16 at 03:32

1 Answers1

2

Indeed, just running multiple instances of a node app, will use multiple processes.

But one note, the cluster option has one slight advantage, and that's if your creating something that listens on a TcpIP port, the cluster can share a single IP port,.. eg. If say you was running a webserver on port 80, you could use all cores that run on port 80..

What I tend to do though, is run a reverse proxy on Port 80, and have other processes running on different ports. IOW: The cluster option is great for creating a reverse proxy.. :)

The advantage of the above is that a reverse proxy does not really need to keep state, you can have it do the SSL too all using cluster. This leaves your node apps, so it can keep state, eg. Cached responses in memory etc. Big advantage of node is that of in-process requests, no special ipc needed here.

Keith
  • 22,005
  • 2
  • 27
  • 44