CodePark/ questions / Runtime, V8 & libuv
START PRACTICINGPRACTICE
Backend question · Node.js › Runtime, V8 & libuv

Is Node.js single-threaded? How does it handle many concurrent requests?

Your JavaScript code runs on one thread, but the Node process uses more threads. Your code has one main thread, one call stack and one event loop. So only one piece of JavaScript runs at a time.

The Node process itself is not single-threaded. It has other threads:

  • libuv (the C library that gives Node its event loop and async I/O) keeps a thread pool. The pool runs work like file I/O, dns.lookup, crypto.pbkdf2 and zlib.
  • V8 (the JavaScript engine) uses extra threads for garbage collection and compilation.

Node handles many requests at once with non-blocking I/O. This means Node starts an I/O operation and does not wait for it to finish. Node hands network sockets to the operating system's async tools (epoll, kqueue, IOCP). Because of this, one thread can wait on thousands of connections. When an operation finishes, Node puts its callback in a queue, and the event loop runs it.

This model works very well for I/O-bound servers. These are servers that mostly wait for the network, files or databases. CPU-heavy work on the main thread blocks every other request. So put that work in worker threads or in another service.

  • Your JavaScript runs on one main thread with one event loop.
  • The process has more threads: the libuv thread pool and V8 helper threads.
  • Network I/O uses the operating system's async APIs, not the thread pool.
  • When an operation finishes, its callback goes into a queue, and the event loop runs it.
  • Node is great for I/O-bound work, but CPU-bound work blocks every request.