<?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 Sagar Shetty on Medium]]></title>
        <description><![CDATA[Stories by Sagar Shetty on Medium]]></description>
        <link>https://medium.com/@sagar.shetty381?source=rss-91e01673a874------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*xcW4whgUP8E-43otaLiLBA.jpeg</url>
            <title>Stories by Sagar Shetty on Medium</title>
            <link>https://medium.com/@sagar.shetty381?source=rss-91e01673a874------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 05 Dec 2026 17:31:14 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@sagar.shetty381/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[Nodejs architechture in 4 minutes]]></title>
            <link>https://medium.com/@sagar.shetty381/how-does-nodejs-actually-works-aee7c53fb520?source=rss-91e01673a874------2</link>
            <guid isPermaLink="false">https://medium.com/p/aee7c53fb520</guid>
            <category><![CDATA[asynchronous-programming]]></category>
            <category><![CDATA[nodejs-architecture]]></category>
            <category><![CDATA[nodejs-event-loop]]></category>
            <category><![CDATA[thread-pool]]></category>
            <category><![CDATA[nodejs]]></category>
            <dc:creator><![CDATA[Sagar Shetty]]></dc:creator>
            <pubDate>Sun, 18 Feb 2024 07:51:51 GMT</pubDate>
            <atom:updated>2026-01-09T10:50:38.196Z</atom:updated>
            <content:encoded><![CDATA[<h3>How does NodeJS actually works</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1000/1*s1c6ab-aATQ90b46kWbZwg.png" /></figure><p>Let’s start from the basics, JavaScript is a <strong>synchronous, blocking and a single threaded language.</strong></p><p>So what makes NodeJS, a <strong>asynchronous, non- blocking I/O model</strong>. It is because the JavaScript engine works with a additional features called the <strong>Web APIs</strong> (setTimeout, setInterval, etc.) in the web browser, which allows JavaScript to behave asynchronously. Unlike synchronous operations, an asynchronous operation does not block the next task from commencing even if the current task isn’t complete yet.</p><p>So it consist of 2 main parts that handles the asynchronous non-blocking I/O nature.</p><ol><li>V8 Engine</li><li>Libuv</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Bn7aFFmJHKcTDeGIDb9ddQ.png" /><figcaption>NodeJS architecture</figcaption></figure><p>Now referring the above diagram lets just understand the V8 Engine.</p><p>This is where all the codes are executed line by line. The line to be executed is pushed to the <strong>call stack </strong>and popped after the execution. It uses the <strong>heap memory</strong> to save required variables and objects declared.</p><p>Whenever a asynchronous operation is encountered in the call stack, it is popped out and sent to the Web APIs where it is executed somewhere in the memory. By then the V8 Engine moves on to execute other following codes. Thus not blocking the main thread and thus serving the other requests as well.</p><p>The<strong> libuv </strong>library is the one that handles the execution of the asynchronous functions. After the execution of the asynchronous code in the web APIs, its callback function is then pushed to the<strong> callback queue </strong>which are ready to be executed in the callstack<strong>. </strong>The CPU intensive task such as file operation, cryptography, etc. is given to the <strong>threadpool. </strong>There are 4 worker threads in the thread pool by default. So this means at a time 4 such operations can be performed by the threadpool simultaneously. This can be increased upto 128 threads programatically.</p><p>Now comes the <strong>Event loop. </strong>It continuously monitors the callback queue and the call stack.</p><p>So is the callback functions executed as soon as it is put to the callback queue? <strong><em>No!</em></strong> the event loop checks whether the call stack is empty or not i.e. all the synchronous code execution is completed or not. If the call stack is empty, it checks the callback queue and moves the callback functions to the callstack to get them executed.</p><p>So is the callback functions executed serially one by one, once the call stack is empty?<strong><em> No! </em></strong>It is upto to the event loop to decide the priorities. We will explain the priorities further.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Bn7aFFmJHKcTDeGIDb9ddQ.png" /></figure><p><strong>Overall working till now:</strong></p><ol><li>When you start the NodeJS server, the server is initialized, code present in the outerscope/global scope is executed (which is not inside a function/block), libraries are imported and the event loop is started.</li><li>Multiple request hits the NodeJS server. The request is processed one by one by the server. Unlike synchronous operations, an asynchronous operation does not block the next task from commencing even if the current task isn’t complete yet (<em>to be noted: </em>it is up to the programmer that he/she has written the codes such that asynchronous functions are called such that other request doesn’t have to wait much).</li><li>The code execution is done by the V8 engine and the asynchronous part is handled by the libuv library and the event loop.</li></ol><p>Now lets understand the priorities by which the event loop decides which callback function to execute from the callback queue.</p><p>The callback queue can be divided into 2 parts -&gt; main/task queue and microtask queue. The callbacks associated with the <strong>Promises and process.nextTick() </strong>are placed in the microtask queue. Please note that all the callbacks of process.nextTick() will take the preference over the promises callbacks.</p><p>As shown in the diagram, each box is referred to as a phase. Before transitioning to any phase the microtask queue is checked and the callbacks are executed if any.</p><p>So this is how to sequence of execution of the callbacks works in the event loop:</p><ol><li>All the callbacks present in the <strong>microtask queue</strong> is executed.</li><li>All the callbacks of <strong>setTimeout()</strong> and<strong> setInterval()</strong> is executed.</li><li>All the callbacks present in the <strong>microtask queue</strong> is executed.</li><li><strong>I/O </strong>callbacks executed.</li><li>All the callbacks present in the <strong>microtask queue</strong> is executed.</li><li>All the callbacks of <strong>setImmediate()</strong> is executed.</li><li>All the callbacks present in the <strong>microtask queue</strong> is executed.</li><li>All the callbacks associated with the <strong>close events</strong> executed (socket.destroy(), etc, …).</li><li>All the callbacks present in the <strong>microtask queue</strong> is executed.</li><li>If the execution of any callbacks are pending then this cycle is continued, if not then the event loop exists.</li></ol><p>Thank you !!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=aee7c53fb520" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>