Node.js Multithreading: Worker Thread with Examples

Node.js Multithreading: Worker Thread with Examples

There was a time when you could print a Word document, and the app would freeze until the job was done.

Worst, there was no way to stop this process once it started.

Microsoft later resolved this issue by pushing the process to a different thread.

What does pushing the process on another thread mean?

In simple words, it means dividing the tasks among original team members.

This was a quick example.

Let's learn how Node.js helps with multi-threading, despite JS being a single-threaded language.

What is Node.js multithreading?

 

Source

To understand multi-threading in Node.js, it's essential to comprehend Multi-threading, In layperson's terms. Simply multi-threading is multitasking.

Multi-threading gives apps the power to run several processes simultaneously without blocking the app for further use.

Several benefits to Node.js multithreading:

  1. Better performance:

There’s nothing more frustrating for a user than when the app crashes during a critical task or freezes until work is done. Multi-threading combats this issue and helps keep the app responsive.

  1. Better Resource Utilization:

With multi-threading, Node.js can use multiple CPU cores, leading to better resource utilization and scalability of the application.

Worker Thread's first task is to reduce the burden on the machine.

How? Removing heavy tasks like complex calculations and image processing from an event loop.

Worker Threads provide two core benefits:

  1. The Worker Thread works in isolation from the parent and other threads. This way, no thread interrupts with another line. Parent thread and worker thread communicate through a messaging channel.

  2. A Worker Thread’s other benefit is that we can break down the CPU-intensive tasks into several parts and thus improve the app's performance.

 

Use of Worker Threads:

  1. CPU-intensive tasks - These threads can offload CPU-intensive tasks, such as mathematical calculations, image processing, and video transcoding, to separate cables to prevent blocking off the main line and improve performance.
  2. Network I/O - Can handle network I/O operations, such as making HTTP requests or sending emails, simultaneously allowing the main thread to continue processing other tasks while waiting for the I/O to complete.
  3. Handle reading and writing files-Similar to network I/O, Worker Threads can handle file I/O operations, such as reading or writing extensive files, in parallel, preventing blocking off the main thread and improving performance.
  4. Improve Scalability- These threads can execute database queries in parallel, allowing the application to handle multiple requests simultaneously and improving scalability.
  5. Machine learning - Last but not least, Worker Threads can perform machine learning tasks, such as training models or making predictions at once. Thus, improving performance and reducing the time required to process large datasets.

In short, ‌the primary aim of Worker Threads is to improve app performance even while running heavy tasks.

But does that mean Worker Threads are all good? Unfortunately, no. Like everything in programming, worker threads have some downsides you must know before applying them.

Downsides of Worker Threads

  1. Too many Worker Threads can quickly get expensive: Unlike other languages with in-built multi-threading, Node.js multi-threads create a separate instance every time you run them. If one thread uses 1 MB of data on your server, then running 100 lines can guzzle up 100 MB in just one operation. So, they are best reserved only for CPU-intensive tasks.

  2. Worker Threads for I/O operations are a waste: The essential nature of Worker Threads is to run CPU-intensive tasks. If you want to read over the file, it’s better not to use them.

    In the Worker Threads documentation, it is recommended not to use Worker Threads for I/O tasks. This is because of the possibilities of redundant code, more expenses, and poor performance.

Let’s look at this use case of how Wix leveraged Node.js Worker Threads to cut costs.

How Wix Applied Node.js Multithreading to Expand their Operations

Wix faced a significant issue when they reached 1 million RPM. A single Node.js thread handled all their tasks. Meaning their entire program depended on one process.

Because of this, they were consuming Kubernetes pods much faster than necessary. Unfortunately, this also increased their infrastructure cost.

Fortunately for Wix, Node.js released Worker Threads just at the right time. As a result, the Wix team could run multiple JavaScript codes simultaneously without consuming Kubernetes pods. The results were astonishing.

Result:

Wix cut the number of pods by over 70%! It also made the entire system more stable and resilient.

  • The pod count dropped by 70%
  • RPM per pod improved by 153%.
  • Response time reduced by 11%
  • Error rate decreased and got 10X better
  • There was a 21% decrease in computing costs.

Conclusion

Worker Threads have been a game changer owing to their multi-threading capacity. So, if you want to switch to Node.js but are worried about its single-threaded nature, now is the time to act and develop apps. Hire Node.js developers from Clarion Technologies to create the next-generation apps without worries.

Author

Author Bio: Binny is a seasoned content marketer in the tech field. With a strong passion for storytelling, she understands digital marketing through creative content strategies. She deeply understands the customer journey and knows how to craft thoughtful content that inspires action. Binny continues to thrive in her role, using her skills to help demystify complex tech concepts for a broader audience.

Talk To Our Experts