Node.js: Single-Threaded but Multi-Threaded Under the Hood

A lot of people say “Node.js is single-threaded” — and that’s true, but it’s not the full picture. Node.js runs JavaScript on a single thread. If you write synchronous code, it blocks that thread. Nothing surprising there. The interesting part starts when Node.js hits something asynchronous, like file system operations, crypto work, or DNS. At that point, Node doesn’t do the heavy work itself. It hands it off to libuv. libuv manages a thread pool. By default, that pool has 4 threads. So when you make an fs call, Node sends the task to libuv. libuv puts it into one of those threads, and that thread talks to the OS. While this is happening, the main Node.js thread stays free to handle other requests. If you send more than 4 such tasks at the same time, the extra ones don’t run immediately. They wait in a queue until a thread becomes available. You can change the size of this thread pool using: process.env.UV_THREADPOOL_SIZE This is why Node.js feels single-threaded when you write JavaScript, but behaves like a multi-threaded system under the hood when dealing with I/O. Understanding this difference makes a huge impact on performance and scalability. #NodeJS #BackendEngineering #JavaScript #libuv #EventLoop #Scalability

To view or add a comment, sign in

Explore content categories