I'm evaluating nodejs for small portion of our web application where it seems like it would be a good fit. I know that node is young and moving fast, but it seems like it has finally gotten to the "ready for production category". However, googling around, most information I see on production deployments are a year old and still warn about how fragile node can be and can error out unexpectedly, followed by some solutions to restart. This doesn't scare me away by itself, but there seems to be lack of official word on "the right way" to put node out there reliably.
cluster
seems to be a good option, although depending on your OS it might have poor load-balancing performance. A very simple version would be something like this:
var cluster = require('cluster')
if(cluster.isMaster) {
var i, worker, workers;
for(i = 0;i < numWorkers;i++) {
worker = cluster.fork();
workers[worker.process.pid] = worker;
}
cluster.on("exit", function(deadWorker) {
delete workers[deadWorker.process.pid];
worker = cluster.fork();
workers[worker.process.pid] = worker;
});
}
else {
//be a real server process
}
This is a nice option because it both gives you some stability by restarting dead processes, and gives you multiple processes that share the load. Note that cluster
basically modifies server.listen so that workers are all listening to events coming from the master, which is doing the listening. This is where the "free" load-balancing comes from.
Cluster documentation can be found here: http://nodejs.org/api/cluster.html
It may also be worth having the master process handle a couple of signals if you want to be able to trigger certain events, such as killing and restarting all the processes, or killing all the processes and shutting down.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With