Be the first user to complete this post
|
Add to List |
Scaling a basic nodejs application using clusters
Nodejs derives its power from the asynchronous nature of javascript. However, the language also has one major caveat - its single threaded nature. This directly impacts the scalability in nodejs applications because if some of your requests consume considerable processor time then your thread is blocked and wont be able to process other incoming requests. If you have a machine with multiple cores, you can take advantage of this architecture by spawning multiple nodejs processes upto as many cpu's as are available to you such that each of these node processes listen on the same port. Such an arrangement is known as a nodejs cluster. On a linux machine, you can simply run the following command to check how many CPU's are available to you
nproc
Initial setup
Lets first setup our package.json file. We will use expressjs to handle our requests.{
"name": "node-cluster-example",
"version": "0.0.1"
}
npm install express --save
Master and worker threads
Before we write our app.js file, there are 4 important things that you need to know.- The process that runs as a result of running the node server from the command line becomes the master thread.
- The nodejs
cluster
module can be used within your code to distinguish between the master thread and worker threads. - If the master thread is killed, so are the worker threads.
- When a worker thread exits unexpectedly, it fires an 'exit' event. You can listen to this event to spawn another thread if needed.
var cluster = require('cluster');
var express = require('express');
var app = express();
var port = 3000;
var cpus;
var i;
if (cluster.isMaster) {
// The master thread is assigned the sole responsibility
// of spawning child threads
cpus = require('os').cpus().length;
for (i = 0; i < cpus; i += 1) {
// Here we create one worker thread per CPU
cluster.fork();
}
cluster.on('exit', function (worker) {
console.log('Worker ' + worker.id + ' exited');
cluster.fork();
});
} else {
// Our app is initialized only on worker threads
app = express();
app.get('/', function (req, res) {
res.send('Hello!, I am Worker #' + cluster.worker.id);
});
app.listen();
console.log('App listening on port : ' + port);
}
node app
App is listening on port
messages as there are CPU's on your machine.
Also Read:
- Debugging nodejs applications using node-inspector and Chrome Dev Tools
- Testing promise sequence using mocha, chai, chai-as-promised, sinon
- Access the request body of a post request in your nodejs - expressjs app.
- Understanding nodejs module exports and require
- Dynamic module loading with require