Erlang OTP: Distributed & Fault-Tolerant Systems Programming · レッスン

ホットコードスワップとライブアップグレード

Erlangがシステムを停止せずに実行中のコードを更新する仕組みと、OTPビヘイビアが状態を持つライブアップグレードを支える方法を学びます。

レッスン 4/413 ステップ

「ホットコードスワップとライブアップグレード」はCoddyKit上の無料Erlang OTP: Distributed & Fault-Tolerant Systems Programmingレッスンです。 これはレッスン4/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはErlang OTP: Distributed & Fault-Tolerant Systems Programming学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Erlang OTP: Distributed & Fault-Tolerant Systems Programmingコースには全4レッスンが含まれています。

このレッスンの一部はまだ翻訳されておらず、英語で表示されています。

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 code

When 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_change for 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/3 migrates state during OTP upgrades
  • appup/relup drive full release upgrades
無料で開始

AI チューターと学ぶ Erlang — 無料

ブラウザでリアルコードを書いて実行し、24/7 の AI チューターから瞬時にサポートを受け、ウェブまたはアプリで続きから学習できます。

コース
12
レッスン
48

よくある質問

「ホットコードスワップとライブアップグレード」レッスンは無料ですか?

はい。「ホットコードスワップとライブアップグレード」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Erlang OTP: Distributed & Fault-Tolerant Systems Programmingコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Erlang OTP: Distributed & Fault-Tolerant Systems Programmingコースには全4レッスンが含まれています。

「ホットコードスワップとライブアップグレード」で何を学びますか?

Erlangがシステムを停止せずに実行中のコードを更新する仕組みと、OTPビヘイビアが状態を持つライブアップグレードを支える方法を学びます。 ブラウザで直接実行するハンズオンコードでErlang OTP: Distributed & Fault-Tolerant Systems Programmingを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。

Erlang OTP: Distributed & Fault-Tolerant Systems Programmingを始めるのに経験は必要ですか?

事前経験は必要ありません。CoddyKitのErlang OTP: Distributed & Fault-Tolerant Systems Programmingは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン4/4です。

「ホットコードスワップとライブアップグレード」レッスンにはどのくらい時間がかかりますか?

ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。

このErlang OTP: Distributed & Fault-Tolerant Systems Programmingレッスンでコードを書いて実行できますか?

はい。すべてのErlang OTP: Distributed & Fault-Tolerant Systems Programmingレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。

このコースのすべてのレッスン

  1. 状態管理のためのGenStatem
  2. イベント処理のためのGenEvent
  3. カスタムOTPビヘイビア
  4. ホットコードスワップとライブアップグレード
← Erlang OTP: Distributed & Fault-Tolerant Systems Programmingに戻る