Saturday 15 April 2017

load balancing nodejs server using cluster module

I am currently trying to load balancing a Nodejs server using customized load balancing algorithm with cluster module. However I encounter some issues when trying to pass request from master to worker.

Here is the idea of my code when doing cluster.fork:

cluster = require('cluster');
numCpu = require('os').cpus().length;
if (cluster.isMaster) {
    var workers = [];
    for (var i = 0; i < numCPU; ++i) {
        workers[i] = cluster.fork();
    }

    var server = http.createServer(function (req, res) {
        // based on req info, I choose the appropriate worker, let's say worker0
        // forward request to server_worker of worker0
        // send response using the response given by server_worker of worker0
    }).listen(8080);
} else {
    var server_worker = http.createServer(function (req, res) {
        // do something
        // there are some global variables which are slightly different between different workers
    }).listen(8081);
}

In short the idea is that the load balancer (master) receive the incoming request, which will choose the server of appropriate worker to process it. It is a little bit like ip sticky, however, I would rather it is 'request sticky', meaning it choose appropriate server based on the content of incoming request.



via Chen Chen

No comments:

Post a Comment