Reloading node with no downtime

I wrote a blog post about Unix signals and Graceful shutdown in node.js applications five months ago. In this article I will explain how to reload a node.js application with no downtime.

One of the things that I like about nginx is how it handles configuration changes Controlling nginx. The master process “reload the configuration” by creating new worker process when it receives the SIGHUP signal.

Node.js comes with a cluster module that allows us to do very powerful things.

For this example I will use one worker but it can be extended to use as many workers as you want.


var cluster = require('cluster');

console.log('started master with ' +;

//fork the first process

process.on('SIGHUP', function () {
  var new_worker = cluster.fork();
  new_worker.once('listening', function () {
    //stop all other workers
    for(var id in cluster.workers) {
      if (id === continue;

The master process start the first worker and then listen to the SIGHUP signal. Then when it receives a SIGHUP signal it fork a new worker and wait the worker until is listening on the IPC channel, once the worker process is listening it kill the other workers.

This works out of the box because the cluster module allows several worker process to listen on the same address.


var cluster = require('cluster');

if (cluster.isMaster) {

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

app.get('/', function (req, res) {
  res.send('ha fsdgfds gfds gfd!');

http.createServer(app).listen(8080, function () {

This is the entry point for the application, it is a simple express application with the exception of the first part.

You can test this as follows:

I’ve uploaded a more complete example to github.

blog comments powered by Disqus