Codementor Events

Why is node js single-threaded?

Published Jul 31, 2023

Why is node js single-threaded?

We know that dividing tasks between multiple threads maks it faster. But then why node.js is single-threaded and claimes itself faster.

How does Multithreading work?

For that, We need to understand how multi-threading works. if there is a multi-core processor machine. Then your task will get divided into these cores. allocating tasks to any core is taken care of by the operating system in which tasks will get allocated to which CORE it depends on t the availability of the core.
rD80m1qEUNKaV4ANBI5XtONbo9IrrJLRfnu2CP8eMnbI6FZcna1w21I36X1tU3L-NTOTqgOmreOMW76CWUYmsFhp2LsKT66SDJoZnSbf42HVEs-Z-zP3uNZvG2hjRffayvBIHgP1=w640-h463.png

Example

Now let’s suppose you have a dual-core processor. The dual-core name itself suggests that the processor has two cores. Now you run the programming which is having 4 tasks. if you write a multi-threaded program then your 2 tasks will get allocated to one core of the processor and 2 tasks will get to another core of the processor ideally. From the top view, it seems that if we distribute tasks between multiple cores of the processor it will execute minimum time well more or less it’s true but not all time it will give fast output because here we got to notice once thing that we are not using multiple processors it’s just once the processor i.e one physical hardware which schedules tasks and presents like it’s executing simultaneously it just complete tasks one by one.

MITj9-8b-l8UYDe7J8Au6_qoL9yIj_naV_wcaIJaFQA7sTeHDSTtoLFdI_xlsaHE_3d4m89157VWbFQUjADUmwO5KpXeoG3OhJ5e1yF8-henKpRwQn969cwF9Fn33nuGpNDRWmSq=w400-h362.png

The problem with multithreading is it requires communication between threads to share tasks between each other to avoid repeating already completed tasks. Multithreading uses shared memory for this. This is a really tricky part because if core1 updated task status after core2 taken it for execution.then the same task will get completed two times. also if there is database related operation then race condition might get creative.
Multithreading is not always easy because in the end we have only one physical processor which is taking care of the execution and it’s just mimicking that tasks getting done parallelly but in reality, it just jumps between tasks back and forth.
Having a single thread reduces the overhead of thread communication.which works faster because all tasks using the same shared memory and no need for communication. That’s the reason why node js able to process tasks faster.

Benefits of using a single thread

The single thread doesn't require communication between threads it shares the same memory that's why we can get a more quick response in a single-thread application that multi-threading.

Limitations of a single thread

Using a single core of processor for all tasks is not good because it may block our main thread which is a nightmare for all node js developers.in node js we can not perform CPU intensive tasks like video editing, high graphics the game, etc. success of the single-thread application depends on how you manage that single thread node js suggest using asynchronous requests to avoid blockage of the main thread.

How node js serve multiple requests.

In multi-threading programming languages webserver allocate a new thread to each request by a default web server like tomcat allows connecting 200 requests each time. if the request increases more than that users need to wait for the thread to get free. we can increase this limit by scaling but that's how usually the webserver works. even though node js don't use multiple threads still it servers multiple requests because it maintains a task queue and doesn't wait for the task to complete when the task complete its return result through a callback. due to this node js able to serve multiple requests same time.

Discover and read more posts from sourabh
get started
post commentsBe the first to share your opinion
Show more replies