Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to update running Node.js servers?

As far as I understand, a Node.js server is basically a JavaScript file, run by the node executable. Is it possible to update this file without stopping request handling?

I guess the node executable must be stopped for this, so I think I should use a reverse proxy. For example:

  1. Original server version listens on localhost:50001
  2. Proxy server listens to requests on webinterface:80 and forwards them to localhost:50001
  3. New server version should be started on localhost:50002
  4. Proxy server forwarding target should be changed to localhost:50002
  5. Original server version should be stopped

Is this a valid approach? How can such a version update be done automatically on multiple server machines (accessible from the same LAN)?

like image 603
kol Avatar asked Nov 13 '12 08:11

kol


People also ask

Does node automatically update?

Note: By default, subsequent node pools do not have auto-upgrades enabled.

Which method is used to update node JS?

Update Files The File System module has methods for updating files: fs.appendFile() fs.writeFile()


2 Answers

A different "pure-node" solution is to use the built-in cluster module:

Your code runs as one cluster-client (of many). A cluster-server process binds to the port and does automatic load-balancing between clients. When you've changed the code, you can send a signal to the cluster-server and it will gracefully restart your clients, without dropping any existing connections.

Here are some projects that can do the cluster-server management for you:

  • https://npmjs.org/package/cluster-master
  • https://github.com/daeq/node-cluster-manager.

Advantages:

  • Fewer components in your stack. (Easier to deploy / manage.)
  • Can automatically start a node process for each core on the CPU, so your program can fully leverage the underlying hardware.
  • No need to learn nginx's config syntax
like image 69
rdrey Avatar answered Nov 15 '22 12:11

rdrey


Basically, what you can (should) do is the following:

  • Run a webserver such as Nginx as a reverse proxy
  • Use at least two (!) Node.js instances behind it
  • Put the sessions in a database such as Redis

When you need to update your system, shut down one of the two Node.js instances, update it, and restart it. Then do the same with the other.

Basically, this should do it.

The downside is that you are potentially running different versions of your application for a small amount of time concurrently, and your database (e.g.) needs to be able to catch up with this.

like image 25
Golo Roden Avatar answered Nov 15 '22 12:11

Golo Roden