Hot Code Swapping & Live Upgrades
Learn how Erlang upgrades running code without stopping the system, and how OTP behaviors support stateful live upgrades.
Hot Code Swapping & Live Upgrades is a free Erlang OTP: Distributed & Fault-Tolerant Systems Programming lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Erlang OTP: Distributed & Fault-Tolerant Systems Programming learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Zero-Downtime Upgrades
One of Erlang's signature features is hot code swapping: replacing a module's code while the system keeps running, with no restart and no dropped connections.
Two Code Versions
The VM keeps up to two versions of each module: current and old. Loading a third version purges the oldest.
Local vs External Calls
A local call (foo()) stays in the running version. An external/qualified call (?MODULE:foo()) jumps to the newest loaded version. This distinction drives upgrades.
loop(State) ->
receive
Msg ->
NewState = handle(Msg, State),
?MODULE:loop(NewState) % picks up new code
end.Loading New Code
You compile and load the new version with the code module or via the shell.
c(my_module).
code:load_file(my_module).The code_change Callback
Stateful OTP behaviors like GenServer support a code_change/3 callback. OTP calls it during an upgrade so you can transform old state into the new format.
code_change(_OldVsn, State, _Extra) ->
{ok, migrate_state(State)}.Why State Migration Matters
If your new version stores state differently (say a tuple becomes a map), code_change bridges the gap so live processes do not crash on the new code.
migrate_state({old, A, B}) ->
#{a => A, b => B}.Release Upgrades
For whole-system upgrades, OTP uses appup and relup files that describe the steps to move from one release version to the next.
{"1.1.0",
[{"1.0.0", [{update, my_server, {advanced, []}}]}],
[{"1.0.0", [{update, my_server, {advanced, []}}]}]}.Suspending Processes
During an advanced upgrade, OTP suspends the affected processes, applies code_change, then resumes them, ensuring no message is handled with mismatched state.
Purging Old Code
After an upgrade, processes still running old code can be checked and the old version purged once safe.
code:soft_purge(my_module).
% returns false if any process still runs old codeWhen to Use It
Hot upgrades shine for telecom-grade always-on systems. For many apps, a rolling restart of nodes is simpler. Choose based on your uptime requirements.
Best Practices
Key rules:
- Always make the recursive loop call qualified (
?MODULE:loop) - Implement
code_changefor any stateful behavior - Test upgrades in staging before production
Quick Check
Test your hot-swap knowledge.
Recap
You learned Erlang's live upgrade model.
- The VM keeps two code versions
- Qualified calls adopt new code
code_change/3migrates state during OTP upgrades- appup/relup drive full release upgrades
Frequently asked questions
Is the “Hot Code Swapping & Live Upgrades” lesson free?
Yes — the full text of “Hot Code Swapping & Live Upgrades” is free to read here on the web, and the Erlang OTP: Distributed & Fault-Tolerant Systems Programming course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Erlang OTP: Distributed & Fault-Tolerant Systems Programming course, upgrade to CoddyKit PRO.
What will I learn in “Hot Code Swapping & Live Upgrades”?
Learn how Erlang upgrades running code without stopping the system, and how OTP behaviors support stateful live upgrades. You practise Erlang OTP: Distributed & Fault-Tolerant Systems Programming with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Erlang OTP: Distributed & Fault-Tolerant Systems Programming?
No prior experience is required. Erlang OTP: Distributed & Fault-Tolerant Systems Programming on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Hot Code Swapping & Live Upgrades” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Erlang OTP: Distributed & Fault-Tolerant Systems Programming lesson?
Yes. Every Erlang OTP: Distributed & Fault-Tolerant Systems Programming lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- GenStatem for State Management
- GenEvent for Event Handling
- Custom OTP Behaviors
- Hot Code Swapping & Live Upgrades