In this post we are going to discuss about configuring cluster as load balancer in NodeJS. In our previous post we have described the way of doing load balancing in NodeJS, in which one way was using cluster. Cluster module in Node.js allows you to run seperate processes which will share same server port.
In simple terms you can say that cluster is a pool of similar workers running under a parent Node process. Workers will share work load among themselves when work load comes on parent Node. The master process is the manager of workers. It is the master process who will initiate workers and control them, as well as assign load to them.
Example : In the below code we have set up a simple server that responds to all incoming requests with a message containing the worker process id. The master process will fork the workers and we will be listening to 8095 for incoming requests from those processes. When a worker process dies we are creating a new worker process so that our application should not suffer.
var cluster = require('cluster');
var http = require('http');
var cpus = require('os').cpus().length;
if(cluster.isMaster) {
for(var i = 0; i< cpus; i++) {
cluster.fork();
}
cluster.on('online',function (worker) {
console.log('The id of worker is ' + worker.process.pid);
});
cluster.on('exit',function (worker) {
console.log('Worker is ' + worker.process.pid + ' dead');
console.log('Starting new process');
cluster.fork();
});
} else {
http.createServer(function(req, res) {
res.writeHead(200);
res.end('From process ' + process.pid)
}).listen(8095);
}
Now when you go to http://127.0.0.1:8095/ you will see the something like "From process SomeID", which means that our the server is been served from that child process. When we start http://127.0.0.1:8095/ on different browser then each browser might have different pid which confirms that cluster is working perfectly. That actually means that you have 4 (depends on the cores) copy of your program running and listening on http://127.0.0.1:8095/ and the requests will be divided into the worker process.
Please Like and Share CodingDefined.com Blog, if you find it interesting and helpful.
In simple terms you can say that cluster is a pool of similar workers running under a parent Node process. Workers will share work load among themselves when work load comes on parent Node. The master process is the manager of workers. It is the master process who will initiate workers and control them, as well as assign load to them.
Example : In the below code we have set up a simple server that responds to all incoming requests with a message containing the worker process id. The master process will fork the workers and we will be listening to 8095 for incoming requests from those processes. When a worker process dies we are creating a new worker process so that our application should not suffer.
var cluster = require('cluster');
var http = require('http');
var cpus = require('os').cpus().length;
if(cluster.isMaster) {
for(var i = 0; i< cpus; i++) {
cluster.fork();
}
cluster.on('online',function (worker) {
console.log('The id of worker is ' + worker.process.pid);
});
cluster.on('exit',function (worker) {
console.log('Worker is ' + worker.process.pid + ' dead');
console.log('Starting new process');
cluster.fork();
});
} else {
http.createServer(function(req, res) {
res.writeHead(200);
res.end('From process ' + process.pid)
}).listen(8095);
}
Now when you go to http://127.0.0.1:8095/ you will see the something like "From process SomeID", which means that our the server is been served from that child process. When we start http://127.0.0.1:8095/ on different browser then each browser might have different pid which confirms that cluster is working perfectly. That actually means that you have 4 (depends on the cores) copy of your program running and listening on http://127.0.0.1:8095/ and the requests will be divided into the worker process.
Please Like and Share CodingDefined.com Blog, if you find it interesting and helpful.
No comments:
Post a Comment