舱壁模式
了解舱壁模式如何隔离资源,使系统某一部分发生故障时不会耗尽共享资源并拖垮整个应用程序。
舱壁模式 是 CoddyKit 上的免费 Microservices Communication Patterns (Saga, Circuit Breaker) 课时。 这是第 4 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Microservices Communication Patterns (Saga, Circuit Breaker) 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Microservices Communication Patterns (Saga, Circuit Breaker) 课程共包含 4 节课。
本课时的部分内容尚未翻译,以英文显示。
What Is a Bulkhead?
The name comes from ships: a hull is divided into watertight bulkhead compartments. If one floods, the others stay dry and the ship survives.
In software, the bulkhead pattern isolates resources so a failure in one area cannot sink the entire system.
The Problem It Solves
Imagine one slow downstream service. If all requests share a single thread pool, requests to the slow service consume every thread. Soon healthy services cannot get a thread either. One failure cascades into total outage.
Isolating Resource Pools
The fix: give each dependency its own pool of resources (threads or connections). When the slow service exhausts its pool, only calls to that service degrade.
pools = {'payments': 10, 'inventory': 10, 'reports': 5}
def acquire(service):
if pools[service] > 0:
pools[service] -= 1
return 'acquired'
return 'rejected (bulkhead full)'
print(acquire('reports'))Thread-Pool Bulkheads
The classic implementation assigns a dedicated thread pool per dependency. Calls run on that pool, so saturation is contained to one dependency.
Libraries like Resilience4j and the historical Hystrix offer this out of the box.
Semaphore Bulkheads
A lighter approach uses a semaphore that limits concurrent calls without a separate thread pool. It has less overhead but cannot enforce timeouts on the caller's thread.
max_concurrent = 3
in_flight = 0
def try_call():
global in_flight
if in_flight >= max_concurrent:
return 'rejected'
in_flight += 1
return 'running'
print(try_call())Rejecting Excess Load
When a bulkhead is full, the call is rejected immediately instead of queuing forever. Fast rejection lets the caller fall back gracefully and keeps the system responsive.
Sizing the Bulkhead
Pool size is a trade-off. Too small wastes capacity; too large defeats isolation. A common starting point is based on expected concurrency: poolSize = peakRPS * avgLatencySeconds plus a small buffer.
peak_rps = 50
avg_latency = 0.2
pool = round(peak_rps * avg_latency) + 5
print('Suggested pool size:', pool)Bulkheads vs Circuit Breakers
They complement each other:
- Bulkhead limits how many calls run at once (resource isolation).
- Circuit breaker stops calling a failing service entirely.
Use both: the bulkhead contains the blast radius, the breaker stops the bleeding.
Per-Tenant Bulkheads
In multi-tenant systems, isolate resources per tenant so one noisy customer cannot starve others. This is sometimes called the noisy neighbor defense.
Monitoring Bulkheads
Track pool utilization and rejection counts. A consistently saturated bulkhead means either the dependency is unhealthy or the pool is undersized. Alerts on rejection rate catch problems early.
When to Use It
Apply bulkheads whenever a single component calls multiple independent dependencies, especially when some are slow or unreliable. It is one of the simplest, highest-value resilience patterns.
Quick Check
What is the primary benefit of the bulkhead pattern?
Recap
You learned the bulkhead pattern:
- Isolate resources per dependency, like ship compartments.
- Use thread-pool or semaphore bulkheads.
- Reject excess load fast and size pools deliberately.
- Combine with circuit breakers for layered resilience.
Bulkheads keep one failure from sinking the whole ship.
常见问题解答
「舱壁模式」课时是免费的吗?
是的 — 「舱壁模式」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Microservices Communication Patterns (Saga, Circuit Breaker) 课程的其余内容,请升级到 CoddyKit PRO。 Microservices Communication Patterns (Saga, Circuit Breaker) 课程共包含 4 节课。
「舱壁模式」这节课中我会学到什么?
了解舱壁模式如何隔离资源,使系统某一部分发生故障时不会耗尽共享资源并拖垮整个应用程序。 你通过在浏览器中直接运行的动手代码来练习 Microservices Communication Patterns (Saga, Circuit Breaker),全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 Microservices Communication Patterns (Saga, Circuit Breaker) 需要有经验吗?
无需任何先前经验。CoddyKit 上的 Microservices Communication Patterns (Saga, Circuit Breaker) 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 4 节课,共 4 节。
「舱壁模式」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 Microservices Communication Patterns (Saga, Circuit Breaker) 课中编写并运行代码吗?
能。每节 Microservices Communication Patterns (Saga, Circuit Breaker) 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。