Erlang OTP: Distributed & Fault-Tolerant Systems Programming · บทเรียน

การสลับโค้ดแบบร้อนและการอัปเกรดขณะทำงาน

เรียนรู้ว่า Erlang อัปเกรดโค้ดที่กำลังทำงานโดยไม่หยุดระบบได้อย่างไร และพฤติกรรมของ OTP รองรับการอัปเกรดขณะทำงานที่มีสถานะได้อย่างไร

บทเรียน 4 จาก 413 ขั้นตอน

การสลับโค้ดแบบร้อนและการอัปเกรดขณะทำงาน เป็นบทเรียน Erlang OTP: Distributed & Fault-Tolerant Systems Programming ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน 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
เริ่มต้นได้ฟรี

เรียนรู้ Erlang ด้วย AI tutor — ฟรี

เขียนและเรียกใช้โค้ดจริงในเบราว์เซอร์ของคุณ รับความช่วยเหลือทันทีจาก AI tutor 24/7 และเรียนรู้ต่อจากที่คุณหยุดบนเว็บหรือในแอป

คอร์ส
12
บทเรียน
48

คำถามที่พบบ่อย

บทเรียน “การสลับโค้ดแบบร้อนและการอัปเกรดขณะทำงาน” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “การสลับโค้ดแบบร้อนและการอัปเกรดขณะทำงาน” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส 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 ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Erlang OTP: Distributed & Fault-Tolerant Systems Programming หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน Erlang OTP: Distributed & Fault-Tolerant Systems Programming บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 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