Child Processes, Clustering, and Worker Threads

For the longest time, Node’s had the ability to be multi-threaded, by using either Child ProcessesClustering, or the more recent preferred method of a module called Worker Threads.

Child processes were the initial means of creating multiple threads for your application and have been available since version 0.10. This was achieved by spawning a node process for every additional thread you wanted to be created.

Clustering, which has been a stable release since around version 4, allows us to simplify the creation and management of Child Processes. It works brilliantly when combined with PM2.

Now before we get into multithreading our app, there are a few points that you need to fully understand:

1. Multithreading already exists for I/O tasks

There is a layer of Node that’s already multithreaded and that is the libuv thread-pool. I/O tasks such as files and folder management, TCP/UDP transactions, compression, and encryption are handed off to libuv, and if not asynchronous by nature, get handled in the libuv’s thread-pool.

2. Child Processes/Worker Threads only work for synchronous JavaScript logic

Implementing multithreading using Child Processes or Worker Threads will only be effective for your synchronous JavaScript code that’s performing heavy-duty operations, such as looping, calculations, etc. If you try to offload I/O tasks to Worker Threads as an example, you will not see a performance improvement.

3. Creating one thread is easy. Managing multiple threads dynamically is hard

Creating one additional thread in your app is easy enough, as there are tons of tutorials on how to do so. However, creating threads equivalent to the number of logical cores your machine or VM is running and managing the distribution of work to these threads is way more advanced, and to code this, logic is above most of our pay grades 😎.

Thank goodness we are in a world of open source and brilliant contributions from the Node community. Meaning, there is already a module that will give us the full capability of dynamically creating and managing threads based on the CPU availability of our machine or VM.

#javascript

How to Manage Multithreaded Node JS Applications for Better Performance
1.15 GEEK