Code swap: Achieving code swapping in Erlang's gen_server
Module redefine:
iex(node2@127.0.0.1)6> Code.load_file("mesngr.ex", "./lib")
[{Mesngr,
<<70, 79, 82, 49, 0, 0, 12, 72, 66, 69, 65, 77, 69, 120, 68, 99, 0, 0, 0, 255, 131, 104, 2, 100, 0, 14, 101, 108, 105, 120, 105, 114, 95, 100, 111, 99, 115, 95, 118, 49, 108, 0, 0, 0, 4, 104, 2, 100, 0, ...>>}]
iex(node2@127.0.0.1)8> Code.load_file("mesngr.ex", "./lib")
lib/mesngr.ex:1: warning: redefining module Mesngr
[{Mesngr,
<<70, 79, 82, 49, 0, 0, 12, 72, 66, 69, 65, 77, 69, 120, 68, 99, 0, 0, 0, 255, 131, 104, 2, 100, 0, 14, 101, 108, 105, 120, 105, 114, 95, 100, 111, 99, 115, 95, 118, 49, 108, 0, 0, 0, 4, 104, 2, 100, 0, ...>>}]
iex(node2@127.0.0.1)9>
I already noticed certain differences, such as that GenServer's code_change callback wouldn't be called in the case of a module redefine (since what I assume is just an overwrite, not a transition from new -> current and current -> old). But I also notice that re-defining a module like this does change the underlying code (which make sense in an FP language).
I guess my question boils down to the following:
- How good/bad/ugly would it be to simply redefine modules on the fly during development, on production?
- What are the benefits of doing proper code swapping, outside of version management and rollbacks? Are there good tutorials/manual/articles on the subject?
- How does hot code swapping work under the hood, versus module redefinition?