Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Building an high performance node.js application with cluster and node-webworker

I'm not a node.js master, so I'd like to have more points of view about this.

I'm creating an HTTP node.js web server that must handle not only lots of concurrent connections but also long running jobs. By default node.js runs on one process, and if there's a piece of code that takes a long time to execute any subsequent connection must wait until the code ends what it's doing on the previous connection.

For example:

var http = require('http');
http.createServer(function (req, res) {

  doSomething(); // This takes a long time to execute

  // Return a response
}).listen(1337, "127.0.0.1");

So I was thinking to run all the long running jobs in separate threads using the node-webworker library:

var http = require('http');
var sys = require('sys');
var Worker = require('webworker');
http.createServer(function (req, res) {

  var w = new Worker('doSomething.js'); // This takes a long time to execute

  // Return a response
}).listen(1337, "127.0.0.1");

And to make the whole thing more performant, I thought to also use cluster to create a new node process for each CPU core.

In this way I expect to balance the client connections through different processes with cluster (let's say 4 node processes if I run it on a quad-core), and then execute the long running job on separate threads with node-webworker.

Is there something wrong with this configuration?

like image 756
Mark Avatar asked Oct 11 '22 01:10

Mark


1 Answers

I see that this post is a few months old, but I wanted to provide a comment to this in the event that someone comes along.

"By default node.js runs on one process, and if there's a piece of code that takes a long time to execute any subsequent connection must wait until the code ends what it's doing on the previous connection."

^-- This is not entirely true. If doSomething(); is required to complete before you send back the response, then yes, but if it isn't, you can make use of the Asynchronous functionality available to you in the core of Node.js, and return immediately, while this item processes in the background.

A quick example of what I'm explaining can be seen by adding the following code in your server:

setTimeout(function(){
    console.log("Done with 5 second item");
}, 5000);

If you hit the server a few times, you will get an immediate response on the client side, and eventually see the console fill with the messages seconds after the response was sent.

like image 70
Matthew C Rice Avatar answered Oct 13 '22 01:10

Matthew C Rice