Hotswapping Erlang Code On AWS EC2 Machine

Hi everyone!

I have a working Erlang application, yay!

Now, I want to release it, turn it into a service, and make it run perpetually on my Ubuntu machine, running on an AWS EC2 instance.

I can handle that, but my main question is how do I take advantage of Erlang’s hotswap functionality here? Is there some suggested/best practice for no/low downtime? This would be an operationally critical component as it continues to develop.

As the code is hosted in GitHub, and I have generated an SSH key for this purpose, I know I can pull the code on demand but, from there, the process becomes unclear.

Do I compile and release the code in the temp download directory and then somehow replace?

How do I know which modules to upgrade/drop?

Any insight would be greatly appreciated, thank you all!

2 Likes

Welcome Ethan (@PCOM),

Hot code loading a module can be as simple as:

1> code:load_file(Module).

During development and integration testing this is fantastic as you can avoid all the startup cost and just start executing the new code. After the new module is loaded all fully qualified function calls (Module:Function/Arity) will use the new code. A running process will continue to use the old module for unqualified function calls (Function/Arity).

The standard behaviours (i.e. gen_server) support an (optional) code_change/3 callback which can be used to port a running process to use the newly loaded code, for example by updating State.

OTP also has great support for upgrading entire releases. You provide a release upgrade file (.relup) which defines how to port running applications between versions.

Hot code loading a release can be as simple as:

2> release_handler:unpack_release(Name).
3> release_handler:install_release(Vsn).
4> release_handler:make_permanent(Vsn).

At SigScale we integrated this into our package management (Apt, Yum) so that upgrading our applications will be performed in-service if the OTP node is running at the time. There’s a video demonstration here.

5 Likes

Cool, thank you!