Saturday, December 06, 2014

How to Scale Your Node.js App - Cluster API & Express with Benchmarking Siege

Though Many Programming language emerge,Javascript Becomes more Powerful Day by Day competing from Front End to back End server and Entering into Desktop apps and Mobile Apps.And becoming popular over GitHub and trending over the world.I have been looking for scaling my Node.js App instance.However it bacame famous by one of it's reason SINGLE THREADED and ASYNCHRONOUS I/O operations.So, now lets create some master and Worker for our Node.js Instance make it run for us in available Processors in Server/Cloud to Balance our Load and Scale Your Node.js Application even Faster.Our Ultimate Aim is to use the Multiple Processor for our App and Maintain a Stable state between Master and Slave Workers and Communication between them.

How to Scale Your Node.js App - Cluster API & Express with Benchmarking Siege
How to Scale Your Node.js App - Cluster API & Express with Benchmarking Siege

Reference : Download 

Prerequisites :

I would like to go through my previous articles on Basics of Node.js with Express Framework.
Cluster API is Inbuilt Feature of Node.js ,We are just Going to test in our app and use it out.However use it with caution since it is marked as Experimental Feature till Right now.

How Does it Works ? 

Here we are creating a new Worker from master Cluster and working with the Workers,Generally you can fork process based on availability of processor.And if you like to fork even more,just Go through this Stackoverflow Thread.So,your Workers will handle your works automatically to load your balance.

Procedure :

we check it is master process or not and forking process as worker as do our specified operation to respond the client.

 var cluster = require("cluster");

 var cpu = require('os').cpus().length;

 if (cluster.isMaster) {

 cluster.on('online', function(worker) {
   console.log("Worker Started pid : "+;

 cluster.on('exit', function(worker, code, signal) {

     console.log('worker ' + + ' stopped');


Worker Process :

And with else you can write your application logic here to run with worker and respond to you can notify the master thread with IPC and cmd.however we will do some basic operation over it.

 else {
  var express = require("express");
  var app = express();


  console.log("request : "; 
  res.send("Welcome to Node.js Cluster API Demo from"+;



Test with Siege :

Now lets test with Siege tool.Download it from here appropriate operating system install it or extract and use.

use the command as below with your own options to check out the efficiency for our small app.

C:\siege-windows>siege -t1M -c100 -d10s http://localhost:3000

** SIEGE 3.0.5
** Preparing 1500 concurrent users for battle.

With Cluster API node.js 

Without Cluster/Normal Node.js Single Threaded

Further Functions:

You have lot of functions to do with worker and Events to do with Worker and Master and inter process communication is also possible to do Cluster module.Search through npm for more ready made cluster modules for your apt application in production.Check here below for reference guide

Note :

The main thing is to scale the application both Horizontally and vertically.this can be achieved not only by clusters,you can run it in seperate VM's for more scalling.I am too looking for such experience and article.

Hope you Learnt Something quite useful.For Errors/Bugs/hugs/suggestion comment below or mail to or connect with me in Facebook/Twitter/Linkedin/G+ hangouts.share is care.


Algorithm Freak, Coder, Web And Android Developer, love to develop Apps for Local Business in my Freetime. For Queries,Quotes Drop me a Mail to


michel said...

nice post shivasurya.

s.shivasurya said...

thank you! hope this would have introduced what is cluster in node.js!

Post a Comment

feel free to post your comments! Don't Spam here! Registered & Protected  Z6HO-DGFO-S2CO-JRZGCopyrights Protected (C)2013-2015 i-visionblog ~ s.shivasurya.

Designed and Maintained by S.Shivasurya | S.Shivasurya