Skip to content

Bulkheads

The checkout request from the first module reaches several dependencies. The checkout service calls a payment provider, which takes 120 ms, and a recommendations service, which takes 30 ms.

CustomerHealthy<1%CheckoutserviceHealthy2%Login serviceHealthy1%InventorydatabaseHealthy1%PaymentproviderHealthy7%RecommendationsserviceHealthy5%Order queueHealthy<1%Email workerHealthy24%
CustomerHealthy<1%CheckoutserviceHealthy2%Login serviceHealthy1%InventorydatabaseHealthy1%PaymentproviderHealthy7%RecommendationsserviceHealthy5%Order queueHealthy<1%Email workerHealthy24%
Text version of the diagram
  • Customer sends requests to Checkout service.
  • Checkout service sends requests to Login service, Inventory database, Payment provider, Recommendations service, Order queue.
  • Login service does not call another component.
  • Inventory database does not call another component.
  • Payment provider does not call another component.
  • Recommendations service does not call another component.
  • Order queue sends requests to Email worker.
  • Email worker does not call another component.

Suppose the checkout service makes these calls through one pool of 50 connections that every dependency shares. At 25 payment calls a second, each taking 0.12 s, Little’s Law says the payments use 25 × 0.12, which is 3 connections at any moment. The recommendations at 100 calls a second use 100 × 0.03, which is another 3. Six connections of 50 are busy. The pool is comfortable.

Now the recommendations service goes silent, and each call waits for a 2 s timeout. The calls held are 100 × 2, which is 200, and the pool has 50. It fills in 50 / 100, which is 0.5 s. Then the payment calls, which are healthy, cannot get a connection, and they wait or fail. A failure in an optional dependency has become a failure in the one that takes the money. The Azure Architecture Center describes the same mechanism: a consumer that sends requests to an unresponsive service may use up its resources, such as its connection pool, until it cannot send requests to any other service.

The pattern takes its name from the sectioned partitions of a ship’s hull: if the hull is breached, only the damaged section fills with water. A bulkhead partitions the resources, so that what is used to call one service cannot be used up by calls to another. Azure’s example is a consumer that has a connection pool for each service it calls: if one service begins to fail, only its pool is affected.

Split the 50 into 40 for payments and 10 for recommendations. When the recommendations service goes silent, its 10 connections are held for 2 s each, so at most 10 / 2, which is 5, new recommendation calls start every second. The other 95 of every 100 are refused at once, without waiting. The payments keep their 40 connections and need 3. Checkout can show its page without suggestions, and it still takes the money.

Mental model. A shared pool lets the worst dependency set the pace for all of them. A bulkhead decides in advance how much damage any one of them can do.

A pool that is too small turns away calls that would have worked. Little’s Law again: the recommendations service needs 3 connections for its 100 calls a second at 30 ms. A pool of 10 can do 10 / 0.03, which is about 333 calls a second, over three times the load, before it refuses a call. Size it from the rate and the time each call takes, with room for the busy hour and for the slow ones, which is why the percentile of the call’s latency is the number to use, not the average.

  • A pool or a semaphore in the caller. A fixed number of connections, threads or concurrent calls for each dependency. resilience4j has a semaphore bulkhead, with a maximum of concurrent calls (25 by default) and a maximum time a caller waits to enter a full one (0 by default, so a caller is turned away rather than kept waiting), and a bulkhead with its own fixed thread pool and a bounded queue. Azure names Polly and resilience4j as frameworks for consumer bulkheads.
  • Separate queues. Services that talk by messages can be isolated by different sets of queues, each with its own workers, as Azure notes.
  • Separate instances. Partition service instances into groups by consumer, so that a client that makes too many requests overwhelms only its own instance. Azure also calls this a cell-based architecture.
  • Separate processes or containers, with their own CPU and memory limits.

A bulkhead is not the only thing that protects. Azure advises combining bulkheads with retry, circuit breaker and throttling patterns. A bulkhead limits how much a dependency can take. A timeout makes sure that what it takes comes back, and a breaker stops the calls altogether once the failure is clear. Without a timeout, the 10 connections of the recommendations pool would be held for ever.

What happens if the small pool is too small?

Section titled “What happens if the small pool is too small?”

Say recommendations traffic triples to 300 calls a second while the dependency is healthy and takes 30 ms. It needs 300 × 0.03, which is 9 connections, and the pool has 10. At 4 times the usual traffic, 400 calls a second, it would need 12, and the extra calls are refused although nothing is wrong. That is the cost of a bulkhead: the resources are no longer shared, so a pool cannot borrow what another has spare. Azure lists it among the cases where the pattern may not suit: when a less efficient use of resources is not acceptable. The alternative is a shared pool where the first dependency to go slow takes everything. Which of the two to accept is a decision about which failure is worse.

  • With one shared pool, a silent dependency fills it: 100 calls a second held for 2 s need 200 connections, and a pool of 50 is full in 0.5 s.
  • A bulkhead gives each dependency its own share, 40 and 10 in the example. The slow one admits 10 / 2, which is 5 calls a second, and refuses the rest at once.
  • Size each pool with Little’s Law and room to spare: 3 connections are in use at 100 calls a second and 30 ms, and a pool of 10 can serve about 333.
  • A bulkhead can be a pool, a semaphore, a queue, an instance or a container, and it needs a timeout and a breaker next to it.
  • Its cost is that spare resources are not shared: at 4 times the traffic the small pool refuses calls that a shared pool would have taken.

Next: failover, moving the work when a component is lost.

Your progress

Built by GhaziAlibi.

© ShardCraft. All rights reserved.