Clojure web application using hot swap code

I am thinking of writing a web application in clojure that can update itself without restarting or losing state.

I saw some articles in which clojure applications can do so-called hot-swappable code. This means that they can update their own functions at run time. Will it work safely on a web server?

+6
source share
2 answers

To get a hot swap for code, it's hard to get right, if at all possible. It depends on the changeset and the running application.

Questions:

  • old vars can interfere with namespaces and cause subtle conflicts, errors
  • redefinition of several vars is not atomic

There may be old vars in the namespace, which will not exist if you restart the application, however this will interfere if you simply override some functions and save the application without rebooting.

Another problem is atomicity: redefinition of several functions, i.e. changing several vars is not atomic. If you change functions in one or more namespaces that depend on code in another namespace, reloading the namespaces with the new code is not atomic.

You are generally better off

  • with a proxy server hold requests until your application reloads
  • deploys a new instance of the application parallel to the "old version" and uses a proxy to switch from the new version after the new version is ready to process requests
+6
source

Erlang's OTP applications support this. Basically, it will spin up the new version of your application and start sending requests to the new version of your application. It will keep the previous version until it completes the processing of requests and closes it.

+4
source

Source: https://habr.com/ru/post/921403/


All Articles