Joe Volcano
Joe Volcano

Reputation: 783

Nodejs Expressjs Cluster Proper way to cluster app.js

Does anyone know which is the correct way to cluster a nodejs express application and why?

Option A: The app instance is created once and the listen is called on each fork that is created.

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

        if (cluster.isMaster) {
        //fork once per cpu
        for (var i = 0; i < numCPUs; i++) {
            cluster.fork();
        }

        //if a worker is online
        cluster.on('online', function WorkerOnline(worker) {
            console.log('%s Worker is running on %s pid: ', new Date().toISOString(), worker.process.pid);
        });
        //if a worker dies, log that and create a new one
        cluster.on('exit', function workerDed(worker, code, signal) {
            cluster.fork();
        });
    } else {
        //non-master forks listen on the ip/port specified in the config
        app.listen(config, function () {
            console.log('%s Express server listening on ip %s:%s ', new Date().toISOString(), config.ip, config.port);
        });
    }

Option B: The app is created and listen is called every time there is a fork.

     if (cluster.isMaster) {
        //fork once per cpu
        for (var i = 0; i < numCPUs; i++) {
            cluster.fork();
        }

        //if a worker is online
        cluster.on('online', function workeronline(worker) {
            console.log('%s Worker is running on %s pid: ', new Date().toISOString(), worker.process.pid);
        });
        //if a worker dies, log that and create a new one
        cluster.on('exit', function workeronline(worker, code, signal) {
            cluster.fork();
        });
    } else {
        //non-master forks listen on the ip/port specified in the config
        var app = require('express')();

        app.listen(8000, function() {
        console.log('Process ' + process.pid + ' is listening to all incoming requests');});
    }

Upvotes: 2

Views: 2039

Answers (1)

Kenny Worden
Kenny Worden

Reputation: 4572

Either is fine. When Node.js calls cluster.fork(), it runs a new Node instance and calls your app.js file all over again. Therefore, the line var app = express(); can be called anywhere and you can guarantee that it will be different from the Express object as instantiated on other instances (i.e. your master and slave processes do not share the app variable).

However, Option B makes it clearer that you are creating a new Express instance every time you fork. In addition, in Option A, you create an Express object on the master and slave processes, but the master process does not use the Express object you created.

Note this block:

} else {
    app.listen(config, function () {
        console.log('%s Express server listening on ip %s:%s ',
        new Date().toISOString(), config.ip, config.port);
    });
}

You only make the Express object listen on a port iff it's a child process; calling var app = express(); outside of the else block in Option A is pointless as you create an Express object but it is not used in the master process. This draws into question why you would want to use Option A at all.

Upvotes: 1

Related Questions