How does NodeJS handle multiple requests?
NodeJS
Hello Readers ,
There’s a lot of confusion out there about concurrency and parallelism. Some people use the terms interchangeably, but they actually refer to two different things.
Concurrency
Concurrency is when two or more tasks can start, run, and complete in overlapping time periods. It doesn’t necessarily mean that they’ll be running at the same time, but they can be interleaved so that at any given time, there is always one task running.
Let’s go for a example
Waiter taking order from table 1 customer and waiting until the order is prepared
When the order is prepared gives the ordered meals to customers. After that go to next table 2 customer takes order and go to kitchen waiting until prepared then givers the ordered meals to the customer .
Is this efficient ? NO Because waiter doing nothing until the order get prepared .
Now waiter goes to table 1 and takes the order and give it to the kitchen and come back to the table 2 and takes the order and give it to the kitchen .Here waiter not waiting until the order is prepared he comes to take next order until food get prepared. When the food gets ready he will serve the food to all the tables. So here we didn’t increate the number of waiters.
Like as we didn't increase the number of threads but we speed up the process by cut down the idle time. Dealing with multiple things in a same time. This is the process of concurrency.
Example : You are cooking ,while cooking you get a call then you answer the call at the same time you get a whistle sound from our cooker then you come back and off the cooker and hang the the call again.
You’re not doing something ,you are doing two things at the same time but one at a time
Parallelism
Parallelism is when two or more tasks can truly run at the same time. In order for this to happen, the tasks must be able to run on separate CPUs or cores.
Get Kumuthini Ashokkumar’s stories in your inbox
Join Medium for free to get updates from this writer.
Let’s go for a example
While your cooking phone rings your mom answer the call. Here two different things happing with two persons but at the same time.
So why is it important to understand the difference?
Concurrency is important because it allows you to structure your code in a way that makes efficient use of resources. Parallelism is important because it can dramatically improve performance by doing more than one thing at a time.
If you’re writing code that will be running on a single CPU or core, then you need to be aware of concurrency issues. If your code will be running on multiple CPUs or cores, then you need to be aware of parallelism issues.
What a Single-Threaded Process is?
A single-threaded process is the execution of programmed instructions in a single sequence. Having said that, if an application has the following set of instructions:
- Instruction A
- Instruction B
- Instruction C
If these set of instructions are executed in a single-threaded process, the execution would look like the following:
What a Multi-Threaded Process is ?
A multi-threaded process is the execution of programmed instructions in multiple sequences. Therefore, instructions won’t have to wait to execute unless multiple instructions are grouped within different sequences.
Why Node.js is Single-Threaded?
NodeJS is a single-threaded platform. This means that it can only process one request at a time.
Example : Took order from table 1 and give it to kitchen then goes to table 2 while taking the order from table 2 table 1’s food is ready but now waiter can not immediately come and give the food to table 1 , waiter must finish the order from table 1 and then hand over it to kitchen after that only he should give the prepared meals to table 1.
NodeJS Web Server maintains a limited Thread Pool to provide services to client requests. Multiple clients make multiple requests to the NodeJS server. NodeJS receives these requests and places them into the EventQueue .
NodeJS server has an internal component referred to as the EventLoop which is an infinite loop that receives requests and processes them. This EventLoop is single threaded. In other words, EventLoop is the listener for the EventQueue.
How does Node.js handle multiple requests?
The event-driven model is very efficient and allows NodeJS to handle thousands of concurrent requests with ease.
Node.js uses two concepts
- Non-blocking I/O
- Asynchronous
Whenever a client sends a request the single thread will send that request to someone else. The current thread will not be busy working with that request. There are workers working for the server. The server sends the request to the worker, the worker further sends it to the other server and waits for the response. In the meantime if there is another request the thread will send it to another worker and the worker will wait for the response from the another server.
In this way the single thread will always be available to take the requests from the client. It will not block the requests.
How is NodeJS better than traditional multithreaded request response model?
With traditional multithreaded request/response model, every client gets a different thread where as with NodeJS, the simpler request are all handled directly by the EventLoop. This is an optimization of thread pool resources and there is no overhead of creating the threads for every client request.
— — — — — — — — — — — — — — — — — — — — — — —
Give your response and stay connected for more :-)
There are more to come :-)
I hope you liked the article.
Thanks for the reading!
Follow me from LinkedIn
https://www.linkedin.com/in/kumuthini-ashokkumar-8b49542bb?utm_source=share&utm_campaign=share_via&utm_content=profile&utm_medium=ios_app
— — — — — — — — — — — — — — — — — — — — — — —










