DEV Community

Cover image for Node.js is Not Single-Threaded

Node.js is Not Single-Threaded

Evgenii Tkachenko on July 08, 2024

Node.js is known as a blazingly fast server platform with its revolutionary single-thread architecture, utilizing server resources more efficiently...
Collapse
 
derekmurawsky profile image
Derek Murawsky

Don't forget that the main event loop in Node can still get backlogged. This can result in skipping garbage collection which can appear like a memory leak and even shut everything down if you're not careful. So while you can federate out the work, the central thread can still get bogged down.

Collapse
 
tmlr profile image
Tony Miller

There’s no “main event loop” in Node. There’s default event loop in libuv.

Collapse
 
derekmurawsky profile image
Derek Murawsky • Edited

I mean, there are many folks that would argue with that. Example
I will freely admit that I don't know these intricacies well, especially since this came from a debugging session for work many years ago for a crypto service written in JS/Node. It received so many requests that it appeared garbage collection could not run reliably and that it appeared like we had a memory leak when we did not. We had several devs look at it during this time as it was high profile, and that was the conclusion we came to, and our monitoring support it.

Thread Thread
 
tmlr profile image
Tony Miller

“It received so many requests”.

This is the key, than means that the stage of libvuv event loop that calls handlers (C level handlers that is) was running all of them and there were so many, that the event loop didn’t progress to further stages.

I didn’t dig further into when node is triggering GC. Would be interesting to see if GC is tied to certain stages.

Collapse
 
tmlr profile image
Tony Miller

Great article!
The only nuance is that diagrams look like libuv call into V8 directly, which is not the case. Node sets up the handlers and those handlers prepare stack and state for V8 to run and then run V8.

The reason this is important is because a lot of people have a notion of having V8 run continuously which is not the case. We enter there and exit from there though Node’s handlers for various libuv stages.

Collapse
 
adaptive-shield-matrix profile image
Adaptive Shield Matrix

That about Bun?
It feels like nodejs is completely superseded by Bun,
at least it has in all of my use cases.

Collapse
 
cookiemonsterdev profile image
Mykhailo Toporkov 🇺🇦

It would be worth to mention that UV_THREADPOOL_SIZE is actually limited for extention by your CPU logic threads amount.

Collapse
 
evgenytk profile image
Evgenii Tkachenko

That's a good one, thanks!

Collapse
 
perisicnikola37 profile image
Nikola Perišić

Wow, great article. Thank you for sharing!

Collapse
 
aladinyo profile image
Aladinyo

Amazing article, also if we have CPU extensive tasks to do, then we can simply run them with C++ as NodeJS is very well integrated with C++

Collapse
 
martinbaun profile image
Martin Baun

It is a good thing we have threads now so that node can be a little more versatile when cpu intensive tasks are needed, great writeup Tkachenko :)