Search code examples
node.jsprocessnode-cluster

Node cluster; only one process being used


I'm running a clustered node app, with 8 worker processes. I'm giving output when serving requests, and the output includes the ID of the process which handled the request:

app.get('/some-url', function(req, res) {
    console.log('Request being handled by process #' + process.pid);
    res.status(200).text('yayyy');
});

When I furiously refresh /some-url, I see in the output that the same process is handling the request every time.

I used node load-test to query my app. Again, even with 8 workers available, only one of them handles every single request. This is obviously undesirable as I wish to load-test the clustered app to see the overall performance of all processes working together.

Here's how I'm initializing the app:

var cluster = require('cluster');
if (cluster.isMaster) {

    for (var i = 0; i < 8; i++) cluster.fork();

} else {

    var app = require('express')();

    // ... do all setup on `app`...

    var server = require('http').createServer(app);
    server.listen(8000);

}

How do I get all my workers working?


Solution

  • Your request does not use any ressources. I suspect that the same worker is always called, because it just finishes to handle the request before the next one comes in.

    What happens if you do some calculation inside that takes more time than the time needed to handle a request ? As it stands, the worker is never busy between accepting a request and answering it.