Linux Networking & TCP/IP for Developers · 课时

韧性模式:熔断器、重试与超时

通过超时、带退避的有界重试以及熔断器模式,防止故障级联,保持分布式系统在故障期间健康运行。

第 4 / 4 课13 个步骤

韧性模式:熔断器、重试与超时 是 CoddyKit 上的免费 Linux Networking & TCP/IP for Developers 课时。 这是第 4 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Linux Networking & TCP/IP for Developers 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Linux Networking & TCP/IP for Developers 课程共包含 4 节课。

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

Failure Is Normal

In a microservices network, calls cross machines and links that will fail. Resilience patterns keep one slow or broken service from dragging down the whole system.

Always Set Timeouts

A call with no timeout can hang forever, exhausting threads and connections. Every remote call must have a deadline.

import requests
r = requests.get('http://orders/api', timeout=2.0)

The Danger of Naive Retries

Retrying immediately after a failure can amplify an outage — a struggling service gets hit even harder. Retries must be bounded and spaced.

Exponential Backoff

Increase the wait between attempts exponentially so a recovering service gets breathing room.

for attempt in range(5):
    try:
        return call()
    except Exception:
        wait = 2 ** attempt
        time.sleep(wait)

Adding Jitter

If many clients back off on the same schedule they retry in sync, creating a thundering herd. Add random jitter to spread the load.

import random
wait = (2 ** attempt) + random.uniform(0, 1)

Retry Only Idempotent Operations

Retrying a non-idempotent write (like 'charge card') can double-execute it. Only retry safe operations, or use idempotency keys to make writes safe.

The Circuit Breaker

A circuit breaker tracks failures to a dependency. After too many, it opens and fails fast instead of waiting on a dead service.

This stops resources from piling up on a doomed call.

Breaker States

A circuit breaker has three states:

  • Closed — calls flow normally
  • Open — calls fail immediately
  • Half-Open — a few test calls probe recovery

Breaker in Code

A minimal breaker counts failures and trips after a threshold, refusing calls until a cooldown elapses.

if breaker.is_open():
    raise CircuitOpenError()
try:
    result = call()
    breaker.record_success()
except Exception:
    breaker.record_failure()
    raise

Fallbacks and Graceful Degradation

When a breaker is open, return a sensible fallback: cached data, a default value, or a reduced feature set. A degraded response beats a total failure.

Bulkheads

The bulkhead pattern isolates resources (thread pools, connection pools) per dependency, so one saturated dependency cannot starve the others.

Quick Check

Test your resilience knowledge.

Recap

You can now build resilient service calls:

  • Always set timeouts
  • Bounded retries with exponential backoff + jitter
  • Retry only idempotent operations
  • Circuit breakers (closed/open/half-open) to fail fast
  • Fallbacks and bulkheads for graceful degradation

This complements your load balancing, service mesh, and API gateway lessons.

免费开始

用 AI 导师学习 Linux Networking & TCP/IP for Developers — 免费

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

课程
12
课程
48

常见问题解答

「韧性模式:熔断器、重试与超时」课时是免费的吗?

是的 — 「韧性模式:熔断器、重试与超时」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Linux Networking & TCP/IP for Developers 课程的其余内容,请升级到 CoddyKit PRO。 Linux Networking & TCP/IP for Developers 课程共包含 4 节课。

「韧性模式:熔断器、重试与超时」这节课中我会学到什么?

通过超时、带退避的有界重试以及熔断器模式,防止故障级联,保持分布式系统在故障期间健康运行。 你通过在浏览器中直接运行的动手代码来练习 Linux Networking & TCP/IP for Developers,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 Linux Networking & TCP/IP for Developers 需要有经验吗?

无需任何先前经验。CoddyKit 上的 Linux Networking & TCP/IP for Developers 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 4 节课,共 4 节。

「韧性模式:熔断器、重试与超时」课时需要多长时间?

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

我能在这节 Linux Networking & TCP/IP for Developers 课中编写并运行代码吗?

能。每节 Linux Networking & TCP/IP for Developers 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. 负载均衡策略
  2. 服务网格架构(Istio/Linkerd)
  3. API 网关与边缘路由
  4. 韧性模式:熔断器、重试与超时
← 返回 Linux Networking & TCP/IP for Developers