How to organize complex computations in NodeJS?

We need to organize a build angular app on the server (ssr). The task itself is resource intensive. There are other complicated calculations which I would not like to run in the main thread.

There are options, but all with drawbacks:
- MQ Message broker (Kafka, RabbitMQ). Of the minuses, overhead messaging, I would immediately give the answer.
- Node Cluster. Here it is not clear how many threads to create? What to do if all threads are busy, and the result is necessary?
- Worker Threads. Minus - all in a single thread, the computation normally paralleled. How this will affect the main thread?

Of all these variants, serious experience not available anywhere else. I would like to hear the opinions of others. Share your experience, how do you solve the problem of complex computation in NodeJS?
April 7th 20 at 15:42
1 answer
April 7th 20 at 15:44
SSR is not such a difficult task. If it is very long - there are already questions to optimize your application.
Generally, first and foremost, you need to answer two questions:
1. On what kind of load do you expect? How many rps is expected?
2. Have you already some load testing? Are you sure now you don't have a single instance node?
If the number of queries is large and a single instance can not cope - I would use raised a few instances in the node using, for example, pm2, which will act as a load balancer.
Generally, in the case of web development with node, not so often in the main thread are some really complex calculations, most delays have on IO/transaction database and other asynchronous stuff. The rest of your complex calculations really clog the main thread? In this case, Yes, it is better to use workers, they're in separate thread run.
The load is large, as much business logic on the client and the server. Problem in creating instance of the application, as in the creation of workarou in one instance, the difficulty of the point to load balance provided that everything is smoked in kubernetes. I would like to start a new instance only for the downloaded transactions. For example, if not enough capacity for ssr, to launch another instance for ssr not perepadnaya all applications.

But in General, I tend to the same solution. Think more about unix sockets to exchange messages. Thanks for the reply. - barney.Kun commented on April 7th 20 at 15:47
@doyle_Haag, there are results of some measurements? How much time an operation is performed in the main thread? Don't forget about the async node, an asynchronous operation main thread free to handle other tasks.
I still think you are somewhat exaggerating the problem, and you need to scale horizontally, i.e. to raise several instances of the nodes and to load balance the same kubernetes.
The problem is that the nodes will be idle, too, don't see, they don't waste resources while it is idle. And if they are no load too often - I think it only means that with your load and one Noda cope. - Jazmyne_Berge commented on April 7th 20 at 15:50

Find more questions by tags JavaScriptNode.js