Bulkhead
Escrito por Darjus Kazakevic · revisado el
Give each dependency its own pool, so one slow call cannot use them all.
A shared pool means a shared failure. If one slow dependency holds every connection in a pool of 40, requests to healthy dependencies wait behind it. The outage spreads through the shared resource, not through your code.
A bulkhead splits the resource, so each dependency or client can use only its own share, like the sealed compartments in a ship's hull. The cost is capacity that stays idle on purpose, and a size to choose: too small throttles normal traffic, too large protects nothing.
En esencia — Give each dependency or client its own limited pool of threads, connections or permits, so one slow path can use up only its own share.
Cuándo recurrir a él
Recurre a él cuando
- A slow path shares a pool with a path everyone waits on
- A batch job or noisy tenant can starve interactive traffic
- One service calls several dependencies with very different latency
Reconsidéralo cuando
- Capacity so tight that idle, reserved headroom throttles normal load
- One dependency and one kind of traffic: there is nothing to split
- Small services where a timeout and a breaker already limit the damage
Conceptos clave
- A pool per dependency
- Each downstream service gets its own connection pool or concurrency limit. In Node, that is a separate undici Pool or HTTP agent per host. In Envoy, it is the circuit_breakers limits per cluster, which despite the name are bulkheads.
- Thread vs semaphore isolation
- Thread isolation runs each dependency's calls on its own thread pool, so the caller can stop waiting after a timeout. Semaphore isolation only counts calls in flight on the caller's thread: cheaper, but a blocked call keeps its thread.
- Sizing with Little's law
- Calls in flight = arrival rate × time per call. At 200 requests/s and 50 ms each, about 10 calls are in flight; add headroom and use 15. If latency grows to 2 s, the bulkhead rejects everything past 15.
- Bulkheads at a larger scale (Cells · Tenant isolation)
- The same idea applies to whole instances: separate deployments for batch and interactive traffic, or a set of servers per large tenant. AWS calls the largest form a cell-based architecture.
Reglas de diseño
- Reject at the limit, never queue without bound
- A full compartment should fail the call at once, or after a short wait. An unbounded queue in front of it is a slow leak: memory and latency grow, and the isolation you paid for is gone.
- Size each pool from its own traffic
- Use Little's law with the path's real rate and p99 latency, then add headroom. Size checkout and reports separately; one number for every dependency protects none of them well.
- Separate the paths users wait on
- Batch jobs, reports and admin tools get their own small pools or their own instances. Then they can never starve checkout or login.
- Alert on rejections
- A rejected call is the bulkhead working, but steady rejections mean the pool is too small or the dependency is slow. Export in-flight and rejected counts per pool.
Errores habituales
- One pool size for every dependency. The fast path is throttled while the slow path still has room to cause harm.
- A bulkhead with an unbounded queue in front. Nothing is ever rejected, and latency climbs to minutes instead.
- Per-dependency pools that add up to more than the database allows. Postgres refuses new connections past max_connections, which is 100 by default.
- Sizing from average traffic and average latency. A normal peak of slow calls fills the pool, and healthy requests are rejected.