Erlang OTP: Distributed & Fault-Tolerant Systems Programming · 课时

背压与负载调节模式

使用基于 OTP 原语构建的背压、速率限制和负载丢弃模式,让系统在过载时保持稳定。

第 4 / 4 课13 个步骤

背压与负载调节模式 是 CoddyKit 上的免费 Erlang OTP: Distributed & Fault-Tolerant Systems Programming 课时。 这是第 4 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Erlang OTP: Distributed & Fault-Tolerant Systems Programming 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Erlang OTP: Distributed & Fault-Tolerant Systems Programming 课程共包含 4 节课。

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

What Is Backpressure?

When work arrives faster than a system can process it, queues grow and latency explodes. Backpressure is the practice of signalling producers to slow down so the system stays stable instead of melting down.

The Unbounded Queue Problem

An async gen_server:cast never blocks the caller, so its mailbox can grow without limit under load — the system runs out of memory before it ever rejects work.

Synchronous Calls as Backpressure

Using gen_server:call instead of cast makes the caller wait until the server is ready, naturally throttling producers to the server speed.

Reply = gen_server:call(worker, {process, Job}, 5000).

Bounded Queues

Maintain an explicit counter of in-flight work and reject or block new requests once a limit is reached, rather than letting the mailbox grow.

handle_call({job, J}, _From, #{n := N} = S) when N < 100 ->
    {reply, ok, S#{n := N + 1}};
handle_call({job, _}, _From, S) ->
    {reply, {error, overloaded}, S}.

Load Shedding

Under extreme load, the best move can be to shed low-priority work fast — returning an error immediately — to protect the system for high-priority requests.

Rate Limiting with Tokens

A token-bucket limiter grants a fixed number of permits per interval; requests beyond that wait or fail. This caps throughput predictably.

case take_token(Bucket) of
    ok -> do_work();
    empty -> {error, rate_limited}
end.

Measuring Pressure

The mailbox length of a bottleneck process is a live pressure gauge. Monitor it and trigger shedding before it grows dangerous.

process_info(Worker, message_queue_len).

Pooling for Throughput

A pool of worker processes (e.g. via poolboy) bounds concurrency: requests queue for a free worker, giving natural backpressure with controlled parallelism.

Circuit Breakers

When a downstream dependency is failing, a circuit breaker opens to stop sending requests for a cooldown period, preventing pileups and giving the dependency time to recover.

Timeouts as Protection

Always give gen_server:call a finite timeout. An unbounded wait lets a stuck server block callers indefinitely; a timeout converts a hang into a fast, recoverable error.

case catch gen_server:call(srv, req, 2000) of
    {ok, R} -> R;
    _ -> {error, busy}
end.

Choosing a Strategy

Combine patterns: synchronous calls for natural throttling, bounded queues to cap memory, rate limiting for fairness, and load shedding plus circuit breakers as last-resort protection.

Quick Check

Test your load regulation knowledge.

Recap

You learned to keep systems stable under overload:

  • Backpressure signals producers to slow down
  • Synchronous call throttles naturally; bounded queues cap memory
  • Rate limiting enforces fairness; load shedding protects priority work
  • Worker pools bound concurrency; circuit breakers stop downstream pileups
  • Monitor mailbox length as a live pressure gauge
免费开始

用 AI 导师学习 Erlang — 免费

在浏览器中编写并运行真实代码,获得全天候 AI 导师的即时帮助,并在网页或应用中继续学习。

课程
12
课程
48

常见问题解答

「背压与负载调节模式」课时是免费的吗?

是的 — 「背压与负载调节模式」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Erlang OTP: Distributed & Fault-Tolerant Systems Programming 课程的其余内容,请升级到 CoddyKit PRO。 Erlang OTP: Distributed & Fault-Tolerant Systems Programming 课程共包含 4 节课。

「背压与负载调节模式」这节课中我会学到什么?

使用基于 OTP 原语构建的背压、速率限制和负载丢弃模式,让系统在过载时保持稳定。 你通过在浏览器中直接运行的动手代码来练习 Erlang OTP: Distributed & Fault-Tolerant Systems Programming,全天候 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. 面向高可用性进行设计
  2. 分布式共识模式
  3. Erlang OTP 案例研究
  4. 背压与负载调节模式
← 返回 Erlang OTP: Distributed & Fault-Tolerant Systems Programming