BULKHEADS

One slow dependency should not sink the whole ship

L214 MINRELIABILITY
PREDICT

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.

▶ SIMULATION
DEPENDENCY A (healthy, 20 ms)
served: 0 / 0
failed: 0 (no thread free; a real shared pool queues them until the caller times out)
success rate: 100%
last tick: — served · needs 4 threads at once
THREAD POOL
t = 0 ms
held by hung B calls: 0 / 8
free for A: 8
one shared pool of 8 threads
DEPENDENCY B (healthy, 50 ms)
served: 0 / 0
hung in flight: 0 · timed out: 0
no thread (would queue, then time out): 0
responding normally
A success rate100% (0 of 0)
✓ requests to the healthy dependency (A) keep succeeding
SCHEDULE
01[CLOCK]+100 ms: 4 requests → A, 2 requests → B
02[CLOCK]+100 ms: 4 requests → A, 2 requests → B
03[CLOCK]+100 ms: 4 requests → A, 2 requests → B
04[CLOCK]+500 ms (20 → A, 10 → B)
05[CLOCK]end of window: report
Start of both healthy scenario. 5 steps.
// 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.

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.

TUNE IT NOT YET

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)

FIX IT NOT YET

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.

Mental model:

A thread pool is a shared resource, and whoever holds a thread longest owns the pool. A timeout bounds how long one call is stuck; a bulkhead bounds how many calls can be stuck at once. You need both. Size each compartment from that dependency’s concurrency (rate times latency), and make sure what remains still covers everyone else. A call that cannot get a slot should fail in microseconds, not wait.

IN PRODUCTION

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.

IN INTERVIEWS

“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.

RECALL CARDS Tap to flip. They join your review deck when you mark the lesson complete.

USED IN

Chapters that build on this lesson.

NEXT: BACKPRESSURE →