Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Web-app with clojure using hot-swapping of code

Tags:

clojure

I'm thinking of writing a web-app in clojure that can update itself without restarting or loosing state.

I've seen some articles where Clojure apps can perform so-called hot-swapping of code. Meaning that they can update their own functions at runtime. Would this be safe to perform on a web-server?

like image 916
Robin Heggelund Hansen Avatar asked Jul 26 '12 12:07

Robin Heggelund Hansen


2 Answers

To get hot-swap for code is tricky to get right, if possible at all. It depends on the changeset and the running application too.

Issues:

  • old vars may litter namespaces and cause subtle conflicts, bugs
  • redefinition of multiple vars is not atomic

There may be old vars in a namespace that will not be there if you restart the application, however will interfere if you just redefine some of the functions and keep the app running without restart.

The other issue is atomicity: redefining multiple functions i.e. changing multiple vars is not atomic. If you change functions in one or more namespace that code in some other namespace depends on, reloading the namespaces with the new code is not atomic.

Generally, you are better off either

  1. having a proxy hold the requests until your app restarts
  2. spinning up a new app instance parallel to the "old version" and use a proxy to switch from the new version after the new version is ready to process requests
like image 52
laczoka Avatar answered Sep 30 '22 17:09

laczoka


OTP applications in Erlang support this. Basically, it will spin the new version of your application up and start sending requests to the new version of your application. It will keep the old version alive until it has completed processing requests and then shut it down.

like image 23
Bill Avatar answered Sep 30 '22 18:09

Bill