BULKHEADS
One slow dependency should not sink the whole ship
Your service calls two dependencies, A and B, through one pool of 8 threads. B hangs, so each call to B holds its thread until a 1000 ms timeout, and 2 new calls to B arrive every 100 ms. A gets 4 calls at the start of every 100 ms, arriving together, each needing a thread for 20 ms. What happens to A?
Your service calls two dependencies through one thread pool. A answers in 20 ms. B stops answering, so every call to B holds its thread until the 1000 ms timeout. New calls to B keep arriving and keep taking threads. Soon B’s hung calls hold the whole pool and a call to A, which would finish in 20 ms, cannot get a thread. A is healthy and your service fails it anyway.
A bulkhead caps how many threads one dependency can hold: a separate pool per dependency, or a semaphore in front of a shared one. B can fill its own compartment and no more. The ship takes water in one compartment and keeps floating.
// One pool for every outbound call.
const pool = new ThreadPool(8);
function callA() { return pool.run(() => http.get(A_URL, { timeout: 1000 })); }
function callB() { return pool.run(() => http.get(B_URL, { timeout: 1000 })); }
// B hangs → 8 calls to B hold 8 threads for 1 s each → callA() finds no thread.// A bulkhead per dependency: B can hold at most 3 threads, A gets the other 5.
const poolA = new ThreadPool(5);
const poolB = new ThreadPool(3); // or: new Semaphore(3) in front of a shared pool
function callA() { return poolA.run(() => http.get(A_URL, { timeout: 1000 })); }
function callB() { return poolB.run(() => http.get(B_URL, { timeout: 1000 })); }
// B hangs → 3 threads stuck, the 4th call to B is rejected at once → A unaffected. Watch the invariant strip in SHARED POOL, B HANGS: it turns red at t = 300 ms, when the hung calls leave fewer than 4 threads for A’s four calls that arrive together each tick. In FIX: BULKHEAD the same hang happens and the strip stays green, because B is capped at B SLOTS threads.
B's team wants as many threads as you can spare. A's requests arrive 4 at a time and each needs its own thread. Find the largest B SLOTS that keeps A at 100% while B is hung — one more slot and A starts failing.
GOAL: A at 100% with B hung, and B SLOTS at its maximum (B SLOTS + 4 == POOL SIZE)
acquire(dep, pool) decides whether a call to dep gets a thread (true) or is rejected at once (false). The pool has 8 threads. A needs 4 at once, B needs 2. Rewrite it so A stays at 100% when B hangs, and nothing is rejected while both are healthy.
Hystrix and Resilience4j thread-pool and semaphore bulkheads, per-upstream connection limits in Envoy and nginx, per-host connection pools in HTTP clients, separate database pools for the request path and background jobs. The symptom without one: an unrelated endpoint fails when one dependency goes slow.
“Service B is slow and now service A's endpoints fail. Why, and what do you do?” Name thread-pool exhaustion, explain why a timeout alone does not stop it, then describe a per-dependency pool or semaphore and how you size it from rate times latency. Mention that the overflow should fail fast and fall back.
USED IN
Chapters that build on this lesson.
- Inside a switch: routing, timeouts, PIN translation and stand-inPayments
- Chaos and resilience testingSRE, Platform and DevOps