Scaling Node.js application without any external libraries

Dev Niklesh
3 min readFeb 7, 2021
Scaling Node.js application without any external libraries — Dev Niklesh

We all face scalability issues in Node apps while growing as a start-up. This is pretty common issue and I’ll show you how this issue can be fixed here.

Some common reason for scalability issues in Node.js apps are,

  1. Huge number of HTTP request.
  2. Processes with computation intensive.
  3. Server failure (500).

Use Machine to it’s Fullest

We are living in a multi-core era and the common mistake everyone does is run entire node app in just a single core.

Use “cluster” built-in node library to spin-up more workers

Cluster creates a Master-worker like model for your app and spins-up individual worker for each core in your VM. This increases the capacity of HTTP requests handled by your server many folds (x4 times if quad-core or x8 times if octa-core).

To implement cluster is super easy.

File server.js:

const express = require('express');

module.exports = () => {
const app = express();
// Just a basic route
app.get('/', function (req, res) {
res.send('Hello World!');
});
app.listen(4000);
console.log('Application running!');
}

File index.js:

const cluster = require('cluster');
const os = require('os');
const runExpressServer = require('./server');

// Check if current process is master.
if (cluster.isMaster) {
// Get total CPU cores.
const cpuCount = os.cpus().length;

// Spawn a worker for every core.
for (let j = 0; j < cpuCount; j++) {
cluster.fork();
}
} else {
// This is not the master process, so we spawn the express server.
runExpressServer();
}

// Cluster API has a variety of events.
// Here we are creating a new process if a worker die.
cluster.on('exit', function (worker) {
console.log(`Worker ${worker.id} died'`);
console.log(`Staring a new one...`);
cluster.fork();
});

What is happening here?

  1. Require “cluster” and “os” in to the project (these are built-in libraries in node.. so no need to install them via npm)
  2. Cluster checks for the Master at the entry of the app. Master state is when no processes running while node app started via npm start or node index.js commands.
  3. If it is Master.. then it counts the number of cores (or CPUs) in your machine (or production server) and copies your entire node app codes in to all the cores.
  4. Once copied, each core tries to start their own node server. Now, they become workers and fall on the else block.
  5. Boom.. Express server started on all the cores individually.

Now you might be wondering what cluster.on('exit', does,

Cluster can spin-up a new worker when existing workers stop due to some fatal error. That’s what is happening at the bottom of the index.js file. It spins-up a new worker listening to their status event (on exit, fork() a new worker).

Conclusion

Now, you project can handle x4 more request and save you a lot of money from just buying extra server. Damn you AWS. Shoot your questions in the comments below. I’ll be happy to answer.

See you in the next article.

--

--

Dev Niklesh

MEAN Stack Developer hoping to land on moon building my own Rocket!