In a nutshell: Is Nodejs single threaded? Parallelism vs Concurrency
You’ll find many different “opinions” online which necessitates that some of them are wrong! I’ve even had interviewers ask me such questions and deem my answer as wrong, when I was actually right!
So, long story short, first we need to know the difference between concurrency and parallelism.
For the sake of this “short” article, let’s define the following:
- concurrency: threads are competing (managed by cpu, or by the software itself) for CPU time; the CPU is only processing one thread at a time, but is alternating between threads (possibly at a high frequency), creating the illusion of parallelism, definitely not waiting for a whole task to finish before executing the next.
- parallelism: threads running on different cores of the CPU; the CPU is running both threads at the same time and, thus, they are running simultaneously.
Figure 1 shows how CPU 1 Core 1 is alternating between task 1 and task 2 (concurrency), while task 1 and task 3 are running in parallel each on a different core (for the common duration). The same is true for Tasks 3 and 4 for their common duration for example.
That being said, nodejs uses a javascript event loop (single threaded, same as javascript, but concurrent) to manage requests, but single threaded-ness only applies to the manager of the requests, which does not necessarily apply in the case of fulfilling of each request.
How can underlying functionality be parallel if javascript itself, and the event loop are single-threaded? Two reasons:
1- Libuv library which runs io operations in parallel using c++ based code
2- Worker threads, which were introduced in node v10, and basically spawn a v8 engine for each thread; meaning that we have multiple threads, each with its own event loop, and they can run possibly simultaneously.
The important take away here is that using a concurrent model for queuing requests (such as using the event loop in nodejs) has proven to be more efficient at scale than parallel queuing of requests, (such as spawning a new process per request, eg. PHP) due to the lower memory and thread management overhead per request.
Now that we know that javascript is single-threaded but nodejs is multi-threaded; is Python single or multi-threaded? Let me know in the comments section, maybe we’ll have another “in a nutshell” quick read about it!
Further reading:
- excellent hands-on example with a benchmark (nodejs):
https://medium.com/softup-technologies/node-js-internals-not-everything-happens-on-the-thread-pool-a14d0a286efb - the event loop:
https://www.freecodecamp.org/news/javascript-concurrency-model-and-event-loop/ - nodejs worker threads:
https://www.simplilearn.com/tutorials/nodejs-tutorial/nodejs-worker-threads - php process/thread per request:
https://stackoverflow.com/questions/5171639/creation-of-new-process-for-each-request-of-web-page - Threads picture:
https://www.hunarcourses.com/blog/wp-content/uploads/2019/04/5-Types-Of-Threads-Used-In-Garment-Making.jpg
Special thanks to Mahmoud Dafer for helping edit this post!