How to make a distributed node.js application?

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:

  • a node based option: Load balancing with node.js using http-proxy
  • nginx: Node.js + Nginx – What now? (using more than one upstream server)
  • apache: (no clearly helpful link I could use, but a valid option)

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)

Leave a Comment