6

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?

4
  • 2
    Define "safe" in your last sentence. Commented Jul 26, 2012 at 23:01
  • Same way as Meteor does. That would be nice. Commented Jul 27, 2012 at 7:00
  • @dimagog without the server crashing, and without users having to reconnect, and without losing or corrupting state... I realize this is impossible if updating third-party libraries or the actual TCP (or http) handling is changed. Most often however, you make changes to the actual application, and it would be sweet to fix bugs without an actual server re-start. Commented Jul 27, 2012 at 8:29
  • 1
    To get hot-swap for code is tricky to get right, if possible at all. Commented Nov 12, 2012 at 13:38

2 Answers 2

6

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
Sign up to request clarification or add additional context in comments.

Comments

4

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.