Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to cluster Node.js app in multiple machines

I am using Express js and Node-cluster for taking the advantage of clustering I am also using PM2 for process and memory management. For a single machine, it is working fine, but my machine having 2 cores and I want to make available more cores. So I decided to join 3 more machines and now all 4 machines are connected using LAN. I am able to access the other machines using IP address in web browser also.

Now I want to connect all the machines and want to share their cores so that I will finally have 2 + 6 = 8 cores for my application. How can it possible? Is there any node module available to achieve this? Thanks.

like image 413
Arpit Kumar Avatar asked Feb 10 '17 09:02

Arpit Kumar


People also ask

Is it possible to cluster multiple node processes?

A cluster module executes the same Node. js process multiple times. Therefore, the first thing you need to do is to identify what portion of the code is for the master process and what portion is for the workers.

How do you cluster in node JS?

Node. js runs single threaded programming, which is very memory efficient, but to take advantage of computers multi-core systems, the Cluster module allows you to easily create child processes that each runs on their own single thread, to handle the load.

Is node a single machine or multiple machines?

Essentially, the nodes share resources with each other and act as a single powerful machine -- the cluster.

How many concurrent connections can Nodejs handle?

To address these issues, Node. JS uses a single thread with an event-loop. In this way, Node can handle 1000s of concurrent connections without any of the traditional detriments associated with threads.


1 Answers

Node-cluster is good for taking advantage of multi core processors, but when it comes to horizontal scaling(adding more machines), you'll need to use load balancers or reverse proxy. For reverse proxy you can use any web server like Apache or nginx. If you want to rely on node and npm, there is a module by nodejitsu: http-proxy. Here is an example for http proxy for 3 machines running your node app.

  1. create a new node project.
  2. Install http-proxy module.

New version:

npm install --save http-proxy

If you prefer older version:

npm install --save [email protected]

  1. Create a new js file (server.js or anything you like).

For version 1.x.x (New)

server.js

var http = require('http'),
httpProxy = require('http-proxy');

var addresses = [
  {
    host: "localhost",
    port: 8081
  },
  {
    host: "localhost",
    port: 8082
  },
  {
    host: "localhost",
    port: 8083
  }
];

//Create a set of proxy servers
var proxyServers = addresses.map(function (target) {
  return new httpProxy.createProxyServer({
    target: target
  });
});

var server = http.createServer(function (req, res) {
  var proxy = proxyServers.shift();

  proxy.web(req, res);

  proxyServers.push(proxy);
});

server.listen(8080);

for version 0.x.x (Old)

server.js

var proxyServer = require('http-proxy');

var servers = [
  {
    host: "localhost",
    port: 8081
  },
  {
    host: "localhost",
    port: 8082
  },
  {
    host: "localhost",
    port: 8083
  }
];

proxyServer.createServer(function (req, res, proxy) {
  var target = servers.shift();

  proxy.proxyRequest(req, res, target);
  servers.push(target);
}).listen(8080);
  1. Now run this file.
  2. Request made to localhost:8080 will be routed to 8081, 8082 or 8083
  3. You can change the localhosts to IP addresses of your machines(and port numbers).

Clients making request to 8080 port are unaware of existence of servers at 8081, 8082 and 8083. They make requests to 8080 as if it is the only server and get response from it.

Now, one of the machines in your cluster will work as node balancer and application is hosted on other three machines. IP address of load balancer can be used as public IP.

like image 186
Sandesh K Avatar answered Oct 11 '22 08:10

Sandesh K