Robin Heggelund Hansen
Robin Heggelund Hansen

Reputation: 5016

Web-app with clojure using hot-swapping of code

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?

Upvotes: 6

Views: 1070

Answers (2)

BillRobertson42
BillRobertson42

Reputation: 12883

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.

Upvotes: 4

laczoka
laczoka

Reputation: 407

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

Upvotes: 6

Related Questions