Hot Code Swapping und Live-Upgrades
Lernen Sie, wie Erlang laufenden Code aktualisiert, ohne das System anzuhalten, und wie OTP-Verhaltensmuster zustandsbehaftete Live-Upgrades unterstützen.
Hot Code Swapping und Live-Upgrades ist eine kostenlose Erlang OTP: Distributed & Fault-Tolerant Systems Programming-Lektion auf CoddyKit. Dies ist Lektion 4 von 4. Du kannst die komplette Lektion unten kostenlos lesen – dann übst du sie direkt im Browser mit einem integrierten Code-Editor und einem KI-Tutor rund um die Uhr. Sie ist Teil des Erlang OTP: Distributed & Fault-Tolerant Systems Programming-Lernpfads, und dein Fortschritt wird über Web und CoddyKit-App synchronisiert. Der Erlang OTP: Distributed & Fault-Tolerant Systems Programming-Kurs umfasst insgesamt 4 Lektionen.
Teile dieser Lektion wurden noch nicht übersetzt und werden auf Englisch angezeigt.
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
Lerne Erlang mit einem KI-Tutor — kostenlos
Schreibe und führe echten Code in deinem Browser aus, bekomme sofortige Hilfe von einem 24/7 KI-Tutor und setze dein Lernen im Web oder in der App fort.
- Kurse
- 12
- Lektionen
- 48
Häufig gestellte Fragen
Ist die Lektion „Hot Code Swapping und Live-Upgrades“ kostenlos?
Ja — der vollständige Text von „Hot Code Swapping und Live-Upgrades“ ist hier im Web kostenlos zu lesen. Um sie interaktiv zu üben (integrierter Code-Editor und 24/7 KI-Tutor) und den Rest des Erlang OTP: Distributed & Fault-Tolerant Systems Programming-Kurses freizuschalten, upgrade auf CoddyKit PRO. Der Erlang OTP: Distributed & Fault-Tolerant Systems Programming-Kurs umfasst insgesamt 4 Lektionen.
Was lerne ich in „Hot Code Swapping und Live-Upgrades“?
Lernen Sie, wie Erlang laufenden Code aktualisiert, ohne das System anzuhalten, und wie OTP-Verhaltensmuster zustandsbehaftete Live-Upgrades unterstützen. Du übst Erlang OTP: Distributed & Fault-Tolerant Systems Programming mit praktischem Code, den du direkt im Browser ausführst, und ein 24/7 KI-Tutor beantwortet deine Fragen während du die Lektion bearbeitest.
Brauche ich Erfahrung, um Erlang OTP: Distributed & Fault-Tolerant Systems Programming zu starten?
Keine Vorkenntnisse erforderlich. Erlang OTP: Distributed & Fault-Tolerant Systems Programming auf CoddyKit ist für Anfänger bis fortgeschrittene Lernende strukturiert, sodass du hier starten oder von Anfang an beginnen und in deinem eigenen Tempo voranschreiten kannst. Dies ist Lektion 4 von 4.
Wie lange dauert die Lektion „Hot Code Swapping und Live-Upgrades“?
Die meisten CoddyKit-Lektionen dauern etwa 5–10 Minuten. Jede ist kompakt und interaktiv, sodass du stetig Fortschritte machst und genau dort weitermachst, wo du aufgehört hast – im Web und in der App.
Kann ich in dieser Erlang OTP: Distributed & Fault-Tolerant Systems Programming-Lektion Code schreiben und ausführen?
Ja. Jede Erlang OTP: Distributed & Fault-Tolerant Systems Programming-Lektion enthält einen integrierten Code-Editor, sodass du echten Code direkt in deinem Browser schreibst und ausführst und sofort KI-Feedback erhältst — ohne lokale Einrichtung erforderlich.
Alle Lektionen in diesem Kurs
- GenStatem für Zustandsverwaltung
- GenEvent für Ereignisverarbeitung
- Benutzerdefinierte OTP-Behaviors
- Hot Code Swapping und Live-Upgrades