NodeJS has a very nice built-in support for clusters through its native cluster module: by using it and realizing we wanted to achieve zero-downtime deployments (ZDD) on our node apps we decided to kick in.
This afternoon Lucio came up to me and asked me a simple question:
Let’s do some NodeJS today?
and my answer was clear and simple: we need a robust solution for ZDD, let’s do this.
Before jumping with our usual
git init and
we searched a bit and found a few alternatives, which
weren’t exactly fitting our scenarios:
- learnboost/cluster, which is quite old, discontinued and hasn’t been updated in a couple of years
- isaacs/cluster-master, which seems to have a whole lot of great things but we wanted to have the startup script as a global command rather than having to write a script on our own (but we might wrap it, as it has a lot of cool features, like resizing your cluster)
At the end we decided to go for a simple solution after reading this post from Jax that explains how to achieve ZDD on NodeJS.
In a few, very productive hours, Lucio was able to write ClusterJS and we decided to release it to the public, as it might be interesting out there (and because we really like to receive feedbacks!).
Suppose you have a typical ExpressJS
app that runs through an
app.js script, in order to run it through
ClusterJS you simply have to:
At this point you should be able to see something like this:
Cool! You have just put your app in a cluster with four workers!
By default, ClusterJS will boot as many workers as CPUs in your system; if you wanna change it, simply:
How to achieve ZDD? Simply issue a SIGUSR2 command to your master process, after updating the files in your app:
If you want to change the signal used to reload the cluster, you can do it with a simple option:
That’s (basically) it! Kudos to Lucio for his work and…
…if you wanna play with ClusterJS or suggest any improvement, feel free to have a look around, as ClusterJS is on GitHub.