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:
- Understanding routers in express 4.0 - Visually
- Unit test your Nodejs RESTful API using mocha
- Error: can not find module 'underscore'
- Understanding semver versioning for your nodejs packages