If you run your single Node server on specific port, it runs on single thread. It take advantage of your Single core only and as far as my knowledge every server out there consist of more than one cores.
So is your Node application taking advantages of those cores and utilizing your resources ? Well if you are not not running it with cluster, then probably you are wasting your hardware capabilities.
In this tutorial i am going to explain one of the awesome feature of Node.js which made it different from rest of backend technologies. It’s “Clustering”.
Clustering in Node.js allows you to create separate processes which can share same server port. For example, if we run one HTTP server on Port 3000, it is one Server running on Single thread on single core of processor.
But i want to take advantage of all core available in my machine. So i will cluster my application and run them on all cores. So if i run one server on Port 3000 by having 4 core of processor then actually i am running 4 server all are listening to Port 3000.
So if one server goes down then other is there to take the place of it, also in peak load of traffic, Node will automatically allocate the worker to particular process so basically it does internal load balancing very efficiently.
Code shown below allow you to cluster your application. This code is official code represented by Node.js.
var cluster = require('cluster');
var numCPUs = require('os').cpus().length;
if (cluster.isMaster) {
// Fork workers.
for (var i = 0; i < numCPUs; i++) {
cluster.fork();
}
Object.keys(cluster.workers).forEach(function(id) {
console.log("I am running with ID : "+cluster.workers[id].process.pid);
});
cluster.on('exit', function(worker, code, signal) {
console.log('worker ' + worker.process.pid + ' died');
});
} else {
//Do further processing.
}
If you observe the code, we are loading Cluster module in first line. Then we are counting how many cores do we have in our machine. Then in “if” condition , we are checking that if its Master process then create the copy of same process by number of core time.
If it is not the Master machine then basically run our normal Node program. So there would be one process which is master which in turn create processes number of core time.
In my case, i have Intel i5 consist of 4 core processor. If i run this code, i will get this output in terminal.
I have covered Express so far as default HTTP server. So for demo purpose i am going to build simple Express program using clustering.
------node_modules
|---express
--- app.js
--- cluster.js
--- package.json
So here is package.json.
package.json
{
"name": "cluster-demo",
"version": "0.0.1",
"dependencies": {
"express": "^4.10.6"
}
}
Install Express by running.
npm install
Here is our express code.
app.js
var express=require("express");
var app=express();
app.get('/',function(req,res){
res.end("Hello world !");
});
app.listen(3000,function(){
console.log("Running at PORT 3000");
});
Here is main file. In order to use this file in your production server, just change the last line.
cluster.js
var cluster = require('cluster');
var numCPUs = require('os').cpus().length;
if (cluster.isMaster) {
for (var i = 0; i < numCPUs; i++) {
cluster.fork();
}
cluster.on('exit', function(worker, code, signal) {
console.log('worker ' + worker.process.pid + ' died');
});
} else {
//change this line to Your Node.js app entry point.
require("./app.js");
}
So now to run your server type,
node cluster.js
You should see this output with above code.
If we tweak above code little bit and add line to show Process ID which serve the request, we will get following response if we try to access localhost:3000 in different browser or client.
Notice anything in process IDs, Since i am running this code on Localhost most i can find to prove clustering is this only, although if we run this code on production workload we may get different response.
For example, suppose you are getting 1000 request in a minute to Port 8000. Before clustering, all request will be serve by this instance of program.
After clustering , you have 4 copy of your program running and listening on Port 3000. So just for example, in one minute 1000 request will be divided to 250 for each core. So together performance will increase.
This is just for example, internal load balancing is done by node itself.
In local environment you may not be able to judge exact performance improvement but i believe in production workload this technique will help you a lot to improve throughput of your System and utilize resources at top.
This is all about clustering in node.js. Share your views in comments.
☞ The Complete Node JS Developer Course
☞ Facebook Authorization with Node.JS
☞ Learn Node.js API’s Fast and Simple
☞ Learn How To Deploy Node.Js App on Google Compute Engine
☞ Node.js for Beginners - Learn Node.js from Scratch (Step by Step)
☞ Learn Node.js from scratch: From Beginner to Advanced
☞ Node.js MongoDB Tutorial | Building CRUD App with Node.js Express & MongoDB