Hot code swapping e aggiornamenti live
Impari come Erlang aggiorna il codice in esecuzione senza arrestare il sistema e come i comportamenti OTP supportino aggiornamenti live con stato persistente.
Hot code swapping e aggiornamenti live è una lezione Erlang OTP: Distributed & Fault-Tolerant Systems Programming gratuita su CoddyKit. Questa è la lezione 4 di 4. Puoi leggere la lezione completa qui gratuitamente — poi esercitati direttamente nel browser con un editor di codice integrato e un tutor IA disponibile 24/7. Fa parte del percorso di apprendimento Erlang OTP: Distributed & Fault-Tolerant Systems Programming, e i tuoi progressi si sincronizzano tra il web e l'app CoddyKit. Il corso Erlang OTP: Distributed & Fault-Tolerant Systems Programming include 4 lezioni in totale.
Parti di questa lezione non sono ancora state tradotte e vengono mostrate in inglese.
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
Impara Erlang con un tutor IA — gratis
Scrivi ed esegui vero codice nel tuo browser, ricevi aiuto istantaneo da un tutor IA disponibile 24/7, e riprendi da dove hai lasciato sul web o nell'app.
- Corsi
- 12
- Lezioni
- 48
Domande Frequenti
La lezione «Hot code swapping e aggiornamenti live» è gratuita?
Sì — il testo completo di «Hot code swapping e aggiornamenti live» è gratuito qui sul web. Per esercitarvi in modo interattivo (un editor di codice integrato e un tutor IA 24/7) e sbloccare il resto del corso Erlang OTP: Distributed & Fault-Tolerant Systems Programming, passa a CoddyKit PRO. Il corso Erlang OTP: Distributed & Fault-Tolerant Systems Programming include 4 lezioni in totale.
Cosa imparerò in «Hot code swapping e aggiornamenti live»?
Impari come Erlang aggiorna il codice in esecuzione senza arrestare il sistema e come i comportamenti OTP supportino aggiornamenti live con stato persistente. Eserciti Erlang OTP: Distributed & Fault-Tolerant Systems Programming con codice pratico che esegui direttamente nel browser, e un tutor IA 24/7 risponde alle tue domande mentre lavori sulla lezione.
Ho bisogno di esperienza per iniziare Erlang OTP: Distributed & Fault-Tolerant Systems Programming?
Non è richiesta alcuna esperienza precedente. Erlang OTP: Distributed & Fault-Tolerant Systems Programming su CoddyKit è strutturato per principianti e studenti avanzati, quindi puoi iniziare da qui o dall'inizio e procedere al tuo ritmo. Questa è la lezione 4 di 4.
Quanto tempo richiede la lezione «Hot code swapping e aggiornamenti live»?
La maggior parte delle lezioni CoddyKit richiede circa 5–10 minuti. Ogni lezione è breve e interattiva, quindi fai progressi costanti e riprendi esattamente da dove hai lasciato su web e app.
Posso scrivere ed eseguire codice in questa lezione Erlang OTP: Distributed & Fault-Tolerant Systems Programming?
Sì. Ogni lezione Erlang OTP: Distributed & Fault-Tolerant Systems Programming include un editor di codice integrato, quindi scrivi ed esegui codice reale direttamente nel tuo browser e ricevi feedback istantaneo dall'IA — nessuna configurazione locale necessaria.
Tutte le lezioni di questo corso
- GenStatem per la gestione dello stato
- GenEvent per la gestione degli eventi
- Behavior OTP personalizzati
- Hot code swapping e aggiornamenti live