How to perform intensive node.js computations

2019-07-14 01:22发布

问题:

I am working on an e-commerce app that has to re-calculate pricing for products every time products are sent from the server to the client (up to thousands of products are sent each time). The calculation piece itself is fairly intensive since it requires multiple database queries to calculate. My naive solution is to abstract the calculation part onto another node.js app/server that is solely dedicated to calculations. Does anyone know of a better or more scalable/optimal way to do this?

Thanks in advance!

回答1:

Defer the computation-heavy task to a dedicated node app is a good idea but beware that this app event loop will be blocked as well and won't be able to serve multiple request in parallel neither.

You will still have to spawn a dedicated process for the computation with either using cluster or child_process.spawn()).

There is a fairly old but still good article about the different possible approaches.