0Pricing
Elixir & Phoenix: Scalable Backend Development · 课时

零停机部署与热升级

通过滚动部署、健康检查以及 BEAM 的热代码升级能力,在不中断连接的情况下发布新的 Elixir 版本。

零停机部署与热升级 是 CoddyKit 上的免费 Elixir & Phoenix: Scalable Backend Development 课时。 这是第 4 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Elixir & Phoenix: Scalable Backend Development 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Elixir & Phoenix: Scalable Backend Development 课程共包含 4 节课。

本课时的部分内容尚未翻译,以英文显示。

Why Zero Downtime Matters

Restarting a server drops in-flight requests and live WebSocket connections. Zero-downtime deploys roll out new code while users stay connected and unaware.

Two Strategies

The BEAM supports two upgrade paths:

  • Rolling deploys: start new nodes, drain old ones — simple and common
  • Hot code upgrades: swap code inside a running node without restart — powerful but complex

Rolling Deploys

A rolling deploy boots a new release alongside the old, shifts traffic over via a load balancer, then shuts the old one down. Most teams use this because it is straightforward and tooling-friendly.

Health Check Endpoints

Load balancers need to know when a new node is ready. Expose a lightweight health endpoint that returns 200 only when the app can serve traffic.

get "/healthz", HealthController, :check
# def check(conn, _), do: send_resp(conn, 200, "ok")

Connection Draining

Before stopping an old node, stop accepting new requests but let active ones finish. This draining avoids cutting users off mid-request.

config :my_app, MyAppWeb.Endpoint,
  drainer: [shutdown: 30_000]

Database Migrations Safely

Old and new code run side by side during a rolling deploy, so migrations must be backward compatible. Add columns before using them; never rename or drop in a single deploy.

Hot Code Upgrades

The BEAM can replace a module's code in a live system. :code.load_file/1 loads the new version; running processes switch over on their next function call.

:code.purge(MyModule)
:code.load_file(MyModule)

Appups and Relups

For full hot upgrades of a release, the BEAM uses appup files (per-application upgrade instructions) and relup files (release-level). Tools like Distillery could generate these.

code_change Callbacks

A long-lived GenServer can migrate its state during a hot upgrade via the code_change/3 callback, reshaping old state into the new format.

def code_change(_old_vsn, state, _extra) do
  {:ok, Map.put(state, :version, 2)}
end

Trade-offs

Hot upgrades avoid any restart but are hard to test and reason about. Most teams prefer rolling deploys for predictability, reserving hot upgrades for embedded or always-on telecom-style systems.

Rollback Plans

Always have a rollback ready:

  • Keep the previous release artifact
  • Make migrations reversible or additive only
  • Automate rollback in your deploy pipeline

Quick Check

Test your zero-downtime knowledge.

Recap

You learned zero-downtime deployment:

  • Rolling deploys drain old nodes while new ones serve
  • Health checks tell the load balancer when a node is ready
  • Migrations must be backward compatible
  • Hot code upgrades swap code live via appup/relup and code_change
  • Always keep a rollback plan

Users stay connected through every release.

常见问题解答

「零停机部署与热升级」课时是免费的吗?

是的 — 「零停机部署与热升级」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Elixir & Phoenix: Scalable Backend Development 课程的其余内容,请升级到 CoddyKit PRO。 Elixir & Phoenix: Scalable Backend Development 课程共包含 4 节课。

「零停机部署与热升级」这节课中我会学到什么?

通过滚动部署、健康检查以及 BEAM 的热代码升级能力,在不中断连接的情况下发布新的 Elixir 版本。 你通过在浏览器中直接运行的动手代码来练习 Elixir & Phoenix: Scalable Backend Development,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 Elixir & Phoenix: Scalable Backend Development 需要有经验吗?

无需任何先前经验。CoddyKit 上的 Elixir & Phoenix: Scalable Backend Development 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 4 节课,共 4 节。

「零停机部署与热升级」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 Elixir & Phoenix: Scalable Backend Development 课中编写并运行代码吗?

能。每节 Elixir & Phoenix: Scalable Backend Development 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. 使用 Mix Release 构建发布版本
  2. 使用 Docker 容器化
  3. 云部署策略
  4. 零停机部署与热升级
← 返回 Elixir & Phoenix: Scalable Backend Development