Reputation: 14798
Let's say I have a nodejs/express application on production server. If I want to add a new router, I have to reboot one, right? But if I reboot my node.js server, then users could get an error.
So how should I reboot my node.js server without errors for my users?
Upvotes: 4
Views: 443
Reputation: 159145
If you proxy to your Node.js app with Nginx, you can tell your Node app to listen on a socket and then only shut down the old server if the new one starts correctly; once the old server shuts down, Nginx will route requests to the new instance of the server. This is how Unicorn, a popular Ruby server, works.
In Nginx, you would specify your upstream like this:
upstream node_server {
server unix:/path/to/socket.sock;
}
server {
...
location / {
...
proxy_pass http://node_server;
}
}
And in node, you can listen on a socket with
server.listen('/path/to/socket.sock', function() {
console.log("Listening on socket.");
});
Upvotes: 9
Reputation: 32404
I imagine the answer is do test everything in a staging server first (i.e. a exact copy of the code you plan to deploy, right next to the existing production code) and choose a low-traffic time and just switch the directory over to the new one.
If there's a problem, switch it back.
But the actual "downtime" would be only a second, and unless someone's request is currently being served, they would not get an error.
That said, I've never tried this myself, and I'm curious to know what the best answer is! Good question!
Upvotes: 0