'The right way to use worker threads in Nest.js app
I was creating a Nest.js app and wanted to take care of scalability of the app. As I found out there are basically two ways to scale: cluster or worker threads. I already asked the question about the scalability of node.js apps here "https://stackoverflow.com/questions/64340922/docker-vs-cluster-with-node-js". And found out that instead of clusters it is best to wrap the app with docker and create clones out of it. So, I have done that with Nest.js app but the question is how can we use worker threads in Nest.js to handle CPU intensive tasks? Should we just import worker threads and use it or are there special syntaxes in Nest.js allowing us to use worker threads?
Solution 1:[1]
Queues
Queues are a powerful design pattern that help you deal with common application scaling and performance challenges.
Break up monolithic tasks that may otherwise block the Node.js event loop. For example, if a user request requires CPU intensive work like audio transcoding, you can delegate this task to other processes, freeing up user-facing processes to remain responsive.
Solution 2:[2]
NestJS is a NodeJS framework, with Dependency Injection as a main draw to it. With that said, class based architecture is usually the best approach. Now, there's absolutely no reason you can't just import the module (similar to how you would in Node/Typescript) and use the library. There's no special syntax necessary, unless you decide to create a Dynamic Module, which isn't likely necessary.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Shantanu Sharma |
| Solution 2 | Jay McDoniel |
