<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Saurabh Gupta on Medium]]></title>
        <description><![CDATA[Stories by Saurabh Gupta on Medium]]></description>
        <link>https://medium.com/@saurabhwithhacks?source=rss-a3430c395e1c------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*1LSDvAkIh25bHXI3</url>
            <title>Stories by Saurabh Gupta on Medium</title>
            <link>https://medium.com/@saurabhwithhacks?source=rss-a3430c395e1c------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 21 Nov 2026 04:50:58 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@saurabhwithhacks/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[How to increase NodeJS performance (advance topic) Part 1]]></title>
            <link>https://medium.com/@saurabhwithhacks/how-to-increase-nodejs-performance-advance-topic-part-1-623bae87c911?source=rss-a3430c395e1c------2</link>
            <guid isPermaLink="false">https://medium.com/p/623bae87c911</guid>
            <category><![CDATA[performance]]></category>
            <category><![CDATA[clustering]]></category>
            <category><![CDATA[nodejs]]></category>
            <category><![CDATA[multithreading]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[Saurabh Gupta]]></dc:creator>
            <pubDate>Sun, 22 May 2022 11:29:15 GMT</pubDate>
            <atom:updated>2022-11-05T13:56:13.705Z</atom:updated>
            <content:encoded><![CDATA[<h3>How to increase NodeJS performance (advance topic)</h3><p>So when most of the people talks about performance, I had seen many stories or blogs which says mostly about — optimize queries to dB, cache using redis, checking time complexity of code etc.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/498/1*bYy3CgYuqhpepug0fvA2-A.gif" /></figure><p>But what If I say, In advance concepts we have two things which mainly going to address performance issue at core and these two topics is been asked in lots of NodeJS interview questions.</p><p><strong>So,<em> Here we have 2 options</em></strong></p><ol><li>Use node in <strong>cluster mode</strong>.</li><li>Use <strong>worker thread</strong>.</li></ol><p>Don&#39;t get panic, lets first understand theory then practical. I will teach you very simply about clustering so beginners can understand it easily. Just read each line carefully.</p><p>Before understanding what is clustering, lets understand why we need clustering … because having reasons makes our learning a way more fast !</p><h3><strong>What is need for clustering ?</strong></h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/480/1*dhimHG3cSQQpFMydrFJK8Q.gif" /></figure><h4>Suppose we have 2 Tasks.</h4><p>if one completes, the second one is going to execute. okay ?</p><p>So what will happen if first task takes 10 seconds and second is going to execute when first one got completed.</p><p>Obviously, It’s going to be headache, as second task is going to be late by 10 seconds.</p><p>Lets take an example :- You and your friends is going for a movie, now we all have a friend who always comes late all the time :(</p><p>And you all have to wait for him because he’s only having vehicle, so think for a second, how you can avoid this being late for movies ? give it a moment….</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/364/1*7RtWCRqYsZIwgro2DAUo7A.gif" /></figure><p>5..4..3..2..1 .. boom — Just buy your own vehicle, if you have your own vehicle, you are not going to be late .. right ?</p><p>so clustering gives a task(process) its own node instance(vehicle), so now task is not going to be late because there is no waiting… yeshhhhhh !! 😃🔥</p><p>On some serious tone 👇</p><p><em>When building a production application, you are usually on the look for ways to optimize its performance.</em></p><p><em>An instance of Node.js runs in a single thread which means that on a multi-core system (which most computers are these days), not all cores will be utilized by the app. To take advantage of the other available cores, you can launch a cluster of Node.js processes and distribute the load between them.</em></p><h3><strong>What is cluster mode ?</strong></h3><p>Serious mode : on, time for some definitions.</p><p>Cluster mode is used to start multiple copies of node that all running inside your server.</p><p>According to node documentation : — Clusters of Node.js processes can be used to run multiple instances of Node.js that can distribute workloads among their application threads. When process isolation is not needed, use the worker thread module instead, which allows running multiple application threads within a single Node.js instance.</p><p><strong>How node works in single thread ?</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/973/1*ib75zTILarULxmawVVV57g.png" /></figure><p>As shown in image, request generally comes and node process it and send back response, simple right ? cool cool !</p><p><strong>Now let’s see how node works in cluster mode …</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/655/1*MBe4DDoS8gD7OhbJ2YNSTg.png" /></figure><p>Now a good question — <strong>What is cluster manager ? what is he doing here ? Is he a manager ?</strong></p><p>so let me clear your doubts … Cluster manager works just same as normal company manager ! yayay simple ✨🤩.</p><p>It is responsible for monitoring the health of each individual instances(node instances).</p><p><strong>Cluster manager</strong> :- 1. Start instances. 2. Stop instances. 3. Restart instances. 4. Send data to instances. 5. Administrative task</p><h3><strong>How is it different than normal NodeJS work ?</strong></h3><p>Now first of all, don&#39;t get scared after seeing diagram. ok ?</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/966/1*as83r9lGmHapR7_fPBowQA.png" /></figure><p>In Normal mode, Its just simple, run node command, it will go to index.js file and run one instance of NodeJS. ooooh yeahhhh, normal mode rocks 🎉, But it doesn&#39;t rock when heavy computation of multiple request are coming.</p><p>In Cluster mode, when you are going to run node command, the very first time, our cluster manager got invoked, now to create worker instance, our cluster manager needs <em>cluster module from NodeJS standard library</em>.</p><p>From that library, <em>we are going to call a method </em>:- <strong>cluster.fork() 🍴</strong></p><p>this function goes to index.js file second time but this time with different modes, when it executes second time <em>our worker instance starts up working</em>.</p><p><strong>Diagram for your convenience:-</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/874/1*_7It0AzGyJJ-DvHblnYByw.png" /></figure><p><strong>In many interviews, you been asked, how to start cluster in node ? or how you multi-thread in NodeJS for better performance ?</strong></p><p>Just say — <em>using cluster lib</em>, In that we got a function called <strong>fork</strong>, we simply call it to create child processes, In next part we are going to implement it on code using NodeJS and going to looks at some interview question.</p><p><strong>I tried to keep everything simple, that&#39;s why didn&#39;t mentioned every single detail and complexity.</strong></p><p>Feedback much appreciated, Thanks for reading my first blog.</p><p>My <a href="https://www.linkedin.com/in/saurabh-gupta-818a641a0/">linkedIn</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=623bae87c911" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>