Ways to implement CPU bound tasks in nodejs

2.8k Views Asked by At

I am using nodejs for a web server which decodes the GET params and returns data in some encoded format. The decode/encode are done using the crypto module of nodejs, which seems to be synchronous. While the time taken to serve a single request is fast enough, blocking the event loop has made the service perform poorly with concurrency.

My requirement is simple, make the encode/decode functionality outside of the event loop.

  1. Separate Process (child_process or cluster)

This can either be a separate process solely for this purpose, but since the encode/decode will be blocking in the child process this will stop the child process to receive new messages i.e. there will never be a situation when two strings are encoded as child process will also be single threaded.

  1. Separate Thread for each request (threads-a-gogo or fiber or node-webworker)

Create a separate thread for each request to carry out the encode/decode operation, but none of the modules seems to work as expected, i.e threads-a-gogo doesn't install through npm, fiber didn't create a separate thread on run(), node-webworker not working.

Has someone faced a similar problem or is there some way to easily create threads in nodejs with simple message passing.

1

There are 1 best solutions below

0
On

This is built into node's child processes. Docs here:

http://nodejs.org/api/child_process.html#child_process_child_send_message_sendhandle

You could also use cluster:

http://nodejs.org/api/cluster.html#cluster_cluster_fork_env

With cluster, it would work something like:

if (cluster.isMaster) {
  var worker = cluster.fork();
  worker.send('encodeThisString');

} else if (cluster.isWorker) {
  process.on('message', function(msg) {
    var encrypted = crypto.doSomeEncryption(msg);
    process.send(encrypted);
  });
}