concept cluster in category javascript

appears as: cluster, cluster, The cluster
JavaScript Application Design: A Build First Approach

This is an excerpt from Manning's book JavaScript Application Design: A Build First Approach.

To conclude, we’ll analyze a useful process scaling technique available to Node applications, known as cluster.

When it comes to release environments, production in particular, you can’t afford to have your process roll over and die with any particular exception. This can be mitigated using a native Node API called cluster that allows you to execute your application in multiple processes, dividing the load among them, and create new processes as needed. cluster takes advantage of multicore processors and the fact that Node is single-threaded, allowing you to easily spawn an array of processes that run the same web application. This has the benefit of making your app more fault tolerant; you can spawn a new process! For example, in a few lines of code, you could configure cluster to spawn a worker every time another one dies, effectively replacing it:

var cluster = require('cluster');

// triggered whenever a worker dies
cluster.on('exit', function () {
  console.log('workers are expendable, bring me another vassal!');
  cluster.fork(); // spawn a new worker
});

This doesn’t mean you should be careless about what happens inside your processes, as starting new ones can be expensive. Forking has a cost, tied to the amount of load your servers are under (requests / time), and also tied to the startup time for your process (wait period between spawning it and when it can handle HTTP requests). What cluster gives us is a way to transparently keep serving responses even if your workers die: others will come in his name.

Node.js in Practice

This is an excerpt from Manning's book Node.js in Practice.

In a single-threaded Node process, file locking is typically something you won’t need to worry about. But you may have situations where other processes are accessing the same file, or a cluster of Node processes are accessing the same file.

This makes 10,000 requests with 100 concurrent requests at any one time. Using three workers on our system gave 260 requests per second, whereas a single process version resulted in 171 requests per second. The cluster was definitely faster, but is this really working as well as our round-robin example with HAProxy or nginx?

The advantage of the cluster module is that you can script it with Node. That means your developers should be able to understand it rather than having to learn how HAProxy or nginx works for load balancing. Load balancing with an additional proxy server doesn’t have the same kind of interprocess communication options that cluster has—you can use process.send and cluster.workers[id].on('message', fn) to communicate between workers.