I have a Node.js web app where I need to do some heavy computations on large matrices. Since Node.js is event driven, I anticipate that this will result in poor performance of my web app. What is the best way to handle CPU heavy tasks with Node?
Could I offload the computations to another server built in something like Python?
Since Node. js is single-threaded and non-blocking, you can achieve higher concurrency with the same resources". And when you read about what it's bad at it usually goes like this: "Since Node. js is single-threaded, CPU-intensive tasks will block all requests from completing, until the task is completed.
js being single-threaded. The single-threaded implementation makes Node a bad choice for CPU-intensive programs. When a time-consuming task is running in the program it blocks the event loop from moving forward for a longer period.
HEAVY SERVER-SIDE COMPUTATION/PROCESSING When it comes to heavy computation, Node. js is not the best platform around.
What you would really like to do in this case is an Addon.
Addons as described in the Node.js documentation are dynamically linked shared objects. They can provide glue to C and C++ libraries.
So you can write your heavy computation in a lower level language (C/C++) and do it much more efficiently than with JavaScript, no matter how powerful V8 is.
Read the docs on how to use Addons and I believe you'll find it a fantastic feature of NodeJS.
Yes - you could offload to another server. Just use your normal request methods to post over the data you need to have 'computed'. Since requests are async, it won't block the normal flow of Node. When the request comes back, you've got the data you need and can do any last minute alterations before pumping it up to the client.
You could then have a 'server farm' that handles these things - for example on EC2 you can create a load balancer, and then just have node make the request to that balancer. No need to do anything fancy inside of node like remembering which server was the last one hit or anything like that.
Alternatively, if this was more of a command line thing and doesn't need to be terribly scalable, you can use node's exec to make a system call, then once again the callback will be called with whatever the return is.
Whatever you do, do not do those computations inside of node :) - your anticipations are correct. It'll bomb your performance, as I learned the hard way a couple of times. Async is your friend.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With