Introduction
Scaling NodeJS is easier than you might think, but let's first understand how it works, you might've heard that NodeJS is an event-driven single threaded runtime for javascript, but trust me it's nothing to scoff at, thanks to the single-threaded nature of node it's exceptional at managing concurrent requests, while that may sound good on paper even that has it's limitation, and in this article we will discuss the cluster module.
Clusters
In order to fully utilize our CPU, we need to implement the cluster module, it's a module which allows us to create multiple 'workers' and each worker runs on one thread, if we have a CPI containing 4 cores we will 8 threads at our disposal, which means by implementing the cluster module our application can handle 8x more requests.
Now let's create a application!
// server.js
const express = require('express');
const app = express();
app.get('/', (req, res) => {
res.send('Hello, World!');
});
module.exports = app
We'll start by creating a basic express server with a route which sends 'Hello World!' as the response.
const cluster = require('cluster')
const os = require('os')
const app = require('./server.js')
const port = 3000
if(cluster.isMaster){
let cpus = os.cpus().length
for(let i=0; i<cpus; i++){
cluster.fork();
}
} else {
app.listen(port, () => {
console.log(`Server is running on http://localhost:${port}`);
});
}
and now we're gonna add another file cluster.js
, this will be the entry point for application
node ./cluster.js
before ending this article there's one more thing we need to take care of, let's say one of our threads crash for some reason, in that case we need to fork another worker, which is actually quite simple to do.
// cluster.js
...
cluster.on('exit', (worker) => {
console.log(`Worker ${worker.id} died`)
cluster.fork()
})
...
With this block, whenever a process dies, we'll fork a new one.
Top comments (0)