Skip to main content

Command Palette

Search for a command to run...

How Node.js Handles Multiple Requests with a Single Thread

Updated
4 min read

Introduction

Hey there folks! Hope you are doing great in your life, and enjoying every bit of it, enjoyin life is also an important thing, never forget that

So, today we'll be talking about the following topics:

  1. Single-threaded nature of Node.js

  2. Event loop role in concurrency

  3. Delegating tasks to background workers

  4. Handling multiple client requests

  5. Why Node.js scales well

Gather your focus here and bear with me till the very end of it, I promise you'll understand every bit of it!


How Node.js Handles Multiple Requests with a Single Thread

Single-Threaded Nature of NodeJS

  • As you know it has only a single main thread right?

  • Unlike other traditional servers, who create a new thread for each request, It doesn't. That's why it is more memory efficient!

  • Now, the question comes into the picture, how does it handles so many cuncurrent requests, despite having a single thread?

  • And now our whole blog will be to answer this particular question, so without wasting any further time, let's jump onto the next section!


Event Loop Role In Concurrency

What is Event Loop

  • Now, i am going to give you a high level idea of what event loop is, for a deeper dive you can click here.

  • So, basically is somewhat like a Manager for NodeJS who decided what should be done next?

The Flow of Handling Multiple Requests

  • It offloads the asynchronous operations to the system kernel or thread pool.

  • Since, it offloads the tasks, the main thread can continue to work without stopping.

  • When the asynchronous task gets completed, it takes the callback functions and places that in the queue to be executed by call stack.


Delegating Tasks To The Background Workers

How the Work Gets Done?

Now, have you wondered that when event loops offloads the task to the system or thread pool, what happens? Who does the job done?

This section will answer the exact same questions

  • Till now we only know that async operations get offloaded.

  • Now, the LibUV library manages 4 worker threads by deafult (you can increase or decrease but i ain't telling you how, search about it, I'm not spoon feeding here).

  • These worker threads are a standalone thread in themselves which does the job.

Methods of Delegation

  • Automatic: See, most of the async tasks automatically gets offloaded by the runtime.

  • Manual: For your own CPU-intensive tasks, you must manually have to delegate the work.

Now, your job is to research how to do it manually?


Why NodeJS Scales Well?

  • Reduced Resource Overhead: By using a single thread, Node.js avoids the significant memory consumption.

  • Asynchronous I/O: Whenever someone requests something that is CPU-Intensive (like DB queries, File Handling, Password Hashing, etc), NodeJS initiates the task and immediately moves to the next client. It doesn't waits for the slow operation to finish, which maximizes CPU utilisation.

  • Event-Driven Design: The runtime reacts to events (like a database returning data) only when they are ready. This makes it ideal for real-time applications such as chat platforms, streaming services, and IoT dashboards where small interactions happen frequently.

  • Lightweight Nature: Because it is built on Google's V8 engine and has a low memory footprint, Node.js is naturally suited for horizontal scaling in containerised environments like Docker and Kubernetes.


Wrap Up!

Today, we didn't just learnt about NodeJS,

We understood how it works internally right?

This is the right place where we end this blog, and congratulations, this was the last theory blog, from the next blog we'll start building backend applications using ExpressJS.

So be ready because you are coming to my domain Explaining Code

That was it for this blog, let's end it right here, Hope you enjoyed it!

I'll catch you up in the next one, until then keep coding and start enjoying life if you aren't 💝

NodeJS Backend

Part 6 of 15

This is an ongoing series that will most likely cover the following topics - Async Code in Node.js: Callbacks and Promises - Sessions vs JWT vs Cookies: Understanding Authentication Approaches - Storing Uploaded Files and Serving Them in Express - URL Parameters vs Query Strings in Express.js - How Node.js Handles Multiple Requests with a Single Thread - Setting Up Your First Node.js Application Step-by-Step - Creating Routes and Handling Requests with Express - JWT Authentication in Node.js Explained Simply - What is Node.js? JavaScript on the Server Explained - Handling File Uploads in Express with Multer - What is Middleware in Express and How It Works - Why Node.js is Perfect for Building Fast Web Applications - REST API Design Made Simple with Express.js - Blocking vs Non-Blocking Code in Node.js - The Node.js Event Loop Explained

Up next

Setting Up Your First Node.js Application Step-by-Step

What are We Gonna Study? Hey there folks, hope you are doing great in your life and enjoying every bit of it! So, now since we have finished all the theory part of NodeJS, let's move to the actual cod

More from this blog

L

Learning Tech

61 posts

A Blog with dozens of articles on different langauges as well as frameworks, This isn't just for you to learn, i crafted it thinking that this could be my future reference as well

It covers Networking, Web Development, Mobile Developement and very soon GenAI too!