How to make a distributed node.js application?

MaiaVictor picture MaiaVictor · Mar 15, 2013 · Viewed 21.8k times · Source

Creating a node.js application is simple enough.

var app = require('express')();
app.get('/',function(req,res){
    res.send("Hello world!");
});

But suppose people became obsessed with your Hello World! application and exhausted your resources. How could this example be scaled up on practice? I don't understand it, because yes, you could open several node.js instance in different computers - but when someone access http://your_site.com/ it aims directly that specific machine, that specific port, that specific node process. So how?

Answer

Pascal Belloncle picture Pascal Belloncle · Mar 15, 2013

There are many many ways to deal with this, but it boils down to 2 things:

  1. being able to use more cores per server
  2. being able to scale beyond more than one server.

node-cluster

For the first option, you can user node-cluster or the same solution as for the seconde option. node-cluster (http://nodejs.org/api/cluster.html) essentially is a built in way to fork the node process into one master and multiple workers. Typically, you'd want 1 master and n-1 to n workers (n being your number of available cores).

load balancers

The second option is to use a load balancer that distributes the requests amongst multiple workers (on the same server, or across servers).

Here you have multiple options as well. Here are a few:

One more thing, once you start having multiple processes serving requests, you can no longer use memory to store state, you need an additional service to store shared states, Redis (http://redis.io) is a popular choice, but by no means the only one.

If you use services such as cloudfoundry, heroku, and others, they set it up for you so you only have to worry about your app's logic (and using a service to deal with shared state)