Debunking Common NodeJS Misconceptions

Manik Mudholkar
3 min readJan 2, 2024

--

Some misconceptions

  • Nodejs is single threaded: Yes and No. Yes when you think of Event loop, V8 and No when you’re think of a thread pool.. The javascript code i.e. execution context is single threaded. So if you block by running some js intensive operation it might block the event loop.
  • Nodejs used thread pool for all IO: Nodejs doesn’t rely on thread pool for most tasks. It only utilizes thread pool when there’s a lack of async sys call in the OS for non-blocking IO. Thread pool used mainly for
    1. DNS resolver (since many operating systems only offer synchronous API for this purpose)
    2. File system API (as it becomes complicated to handle asynchronously across different platforms)
    3. Crypto (due to its utilization of the CPU)
    4. Zlib (used for zip compression)
  • One thread per connection: It’s easy to get misdirected into thinking that libuv just spins up new thread for each request. But that’s not at all the case. How Nodejs managed to server serve multiple connections is by doing non-blocking operation on socket.
  • Nodejs is fast in every aspects: It depends. Nodejs is highly efficient in handling IO intensive tasks, but it may not be as effective when it comes to managing CPU intensive tasks. To illustrate this, we can compare worker threads to waiters in a restaurant. If a table takes a long time to place an order, it keeps the waiter occupied for a longer duration, which reduces overall efficiency.
  • Thread-pool is used for network scaling: Node.js runs network I/O operations on the main thread. Although node.js spawns four additional threads, none of them are utilized for network I/O tasks like database operations. It is important to rely only on information from the official node.js project itself, rather than external sources like YouTube or Medium articles that claim node I/O uses thread pools. These sources may not have accurate knowledge on the subject. If you wish to distribute the workload across multiple processors, you can utilize clustering. This can be achieved by either writing your own clustering code or utilizing the clustering mode of process managers like pm2 to distribute connections to your processes.
  • Just Bump up the default thread pool size for more scaling: Spawning more threads than that of your number of cores will not really give you much of gain in performance as cpu utilization will waste in context switching itself. That’s why they say use thread pool carefully.

These were really the eye openers for me as these were some of the core understandings for me.

Before you go!

  • Stay tuned for more insights! Follow and subscribe.
  • Did you see what happens when you click and hold the clap 👏 button?

--

--