How to Enhance Node JS Performance through Clustering? (original) (raw)

Last Updated : 28 Apr, 2025

In this article, we are going to learn about clustering through Node.js. Clustering is a process through which we can use multiple cores of a computer simultaneously. Like JavaScript, which is a single-threaded language, the code of JavaScript will run only on a single core if you have more than one core installed on your machine. So clustering is the process through which we can enhance the performance of our software using multiple cores of the machine at the same time.

Table of Content

Understand Clustering

Clustering is the process through which we can use multiple cores of our central processing unit at the same time with the help of Node JS, which helps to increase the performance of the software and also reduces its time load. Cluster is the module of JavaScript required to enable clustering in your program. First, we need to install this module in the required project. We can install cluster modules through the given command.

npm i cluster

Benefits of Clustering

Clustering provides the feature of improving system efficiency and enhancing system performance. Some of the main benefits of clustering are as follows:

Example Without Clustering

**Example : Write the following code in App.js file

JavaScript ``

// App.js const http = require('http');

const server = http.createServer((req, res) => { res.writeHead(200, { 'Content-Type': 'text/plain' }); res.end('Hello Geeks\n'); });

const PORT = 3000; server.listen(PORT, () => { console.log(Server Established at -> ${PORT}); });

``

**Output:

Screenshot-2023-11-29-193057

Example with Clustering

**Example : Write the following code in App.js file

JavaScript ``

// App.js const cluster = require('cluster'); const http = require('http'); const numCore = require('os').cpus().length;

if (cluster.isMaster) { for (let i = 0; i < numCore; i++) { cluster.fork(); }

cluster.on('exit', (worker, code, signal) => {
    console.log(`Worker ${worker.process.pid} died. Restarting...`);
    cluster.fork();
});

} else { const server = http.createServer((req, res) => { res.writeHead(200, { 'Content-Type': 'text/plain' }); res.end('Hello Geeks\n'); });

const PORT = 3000;
server.listen(PORT, () => {
    console.log(`Worker <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mrow><mi>p</mi><mi>r</mi><mi>o</mi><mi>c</mi><mi>e</mi><mi>s</mi><mi>s</mi><mi mathvariant="normal">.</mi><mi>p</mi><mi>i</mi><mi>d</mi></mrow><mi>e</mi><mi>s</mi><mi>t</mi><mi>a</mi><mi>b</mi><mi>l</mi><mi>i</mi><mi>s</mi><mi>h</mi><mi>e</mi><mi>d</mi><mi>o</mi><mi>n</mi><mi>P</mi><mi>O</mi><mi>R</mi><mi>T</mi><mo>−</mo><mo>&gt;</mo></mrow><annotation encoding="application/x-tex">{process.pid} established on PORT -&gt; </annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.8889em;vertical-align:-0.1944em;"></span><span class="mord"><span class="mord mathnormal">p</span><span class="mord mathnormal">rocess</span><span class="mord">.</span><span class="mord mathnormal">p</span><span class="mord mathnormal">i</span><span class="mord mathnormal">d</span></span><span class="mord mathnormal">es</span><span class="mord mathnormal">t</span><span class="mord mathnormal">ab</span><span class="mord mathnormal" style="margin-right:0.01968em;">l</span><span class="mord mathnormal">i</span><span class="mord mathnormal">s</span><span class="mord mathnormal">h</span><span class="mord mathnormal">e</span><span class="mord mathnormal">d</span><span class="mord mathnormal">o</span><span class="mord mathnormal">n</span><span class="mord mathnormal" style="margin-right:0.13889em;">PORT</span><span class="mord">−</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">&gt;</span></span></span></span>{PORT}/`);
});

}

``

**Output:

Screenshot-2023-11-29-193915

Comparing Performance of with & without Clustering

Clustering Without Clustering
Provides utilization of multiple cores, which enhances performance. Available cores are rest-free, so they will reduce performance.
Provides load balancing, which divides load among each core. Does not provide load balancing, so it burdens only one core.
Resource utilization helps to work all core that reduce time to process. Resource utilization does not exist so it will take much time to execute process.
It will take less energy because no core will be free. It will take more energy because cores will be available in rest.

Conclusion

Node JS clustering improves multiple factors of applications, like allowing developers to use more cores at the same time, which will improve the efficiency of the program, reduce the time taken for execution, reduce the energy consumption of the program, and also balance the load among each processor, which helps to fix the resource utilization of the machine.