Scaling Node.js application without any external libraries

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.

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)

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.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Dev Niklesh

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