# Bulkhead pattern

## What it is

The bulkhead pattern isolates a part of a system so its failures or heavy demand
affect a limited area. A service can give different types of work separate
queues, connection pools, worker pools or capacity on separate instances. Each
boundary has a defined amount of shared resource available. When one part uses
its allowance, other operations can continue if they still have the resources
they need.

Isolation can follow a dependency, a tenant, a class of request or a particular
set of data. An application might reserve workers for short interactive
operations and use a separate pool for slow reports. Another design might
isolate requests to an unreliable external service from ordinary internal reads.
The chosen boundary should follow what needs to remain available when one part
becomes slow or stops.

A pool by itself provides a limited kind of isolation. The work behind two pools
can still share storage, process memory or an upstream provider. Review the path
end to end and identify where one part can consume capacity another part needs.
The pattern is useful when an owner can explain which failures the boundary
contains and what limits still apply across the wider service.

## Why it matters

Imagine a service that serves product pages and generates large export files. A
burst of slow exports can fill every worker in a shared pool. The product pages
may then wait even though each page needs little work. Giving exports their own
bounded queue and capacity can preserve responsiveness for browsing while the
export system catches up. The waiting requests still need honest status,
deadlines and an end to repeated retries.

A boundary also lets teams set different policies for work with different costs.
A request that opens a product page may need a result quickly. An offline report
can wait longer and show its position or the age of the queued request. When the
report workload exceeds the resources set aside for it, the service might defer
new reports or refuse them clearly. Those choices are easier to make once the
affected work and the resources it can consume are defined.

There is a trade-off in reserving resources. An idle pool can leave capacity
unused while another one is busy. Allowing unlimited borrowing can create the
same shared saturation the design was meant to prevent. Use measurements to
decide which minimum must be protected and where capacity can be shared within a
controlled limit. Include the failure of any shared connection or service when
testing what protection users receive.

## What it looks like in practice

Consider an application sending notifications through a slow partner connection.
Give the partner requests a fixed number of workers and a bounded queue. Reserve
a different capacity budget for requests that only read application records.
Send enough slow partner work to fill its queue, then check a normal read.
Measure response times for each group, the number of active tasks and the result
when an affected requester tries to submit more work.

Exercise a second case where a shared database connection limit is reached. The
isolated worker pools may still depend on that database. If the normal reads
slow down too, inspect which capacity the separation actually protects. Reserve
a suitable budget for the work that must continue or redesign the expensive path
to release its connection while it waits. Record the conditions of the test and
compare a real operation on each side of the boundary.

Engineer's
[limited-stock engineering note](/notes/selling-limited-stock-without-overselling/)
describes scoping a pause to the stock items whose shard is unavailable while
keeping other parts of the shop usable. That is a concrete example of defining
the affected work and preserving useful work elsewhere. A bulkhead exercise can
apply the same question before a failure occurs. Which users should still be
able to browse, submit or read a result when one area reaches its limit?

## Where it goes wrong

A bounded worker pool can be paired with an unbounded queue. During a long
outage the backlog can grow until the process runs out of memory or accepts jobs
long after they are useful. Define the queue's length, lifetime and response to
overload. Make rejection or postponement visible so that an upstream caller can
choose how to respond without producing an uncontrolled retry wave.

A different failure arises when a limit is too broad. Placing all partners
behind one isolation boundary protects the rest of the application but lets one
partner delay every other partner. Test where the expected failure originates
and where it should stop. More pools also mean more configuration and
observation, so use a boundary that somebody can operate and measure.

Review retry behaviour when each boundary has its own queue. If a stalled
operation occupies all its capacity through repeated attempts, it can still
prevent newer work from finishing within the same boundary. Consider the cost
and value of each attempt, and record how the service handles an operation that
has passed its useful deadline. A short queue can keep the problem visible and
offer operators a predictable way to restore normal throughput.

Finally, verify the policy across restarts and changes in traffic. Establish how
tasks already accepted are completed or returned, and check whether a new
deployment preserves the intended capacity split. Measure request latency and
completion separately for each workload, including the affected integration and
the routes you want to protect. A design is useful when responders can say which
work continued, which work was delayed and what resource governed that outcome.

## Further reading

- [Microsoft's Bulkhead pattern guidance](https://learn.microsoft.com/en-us/azure/architecture/patterns/bulkhead).
  Explains partitioning by service, consumer and resource.
- [Resilience4j bulkhead controls](https://resilience4j.readme.io/docs/bulkhead).
  Describes semaphore and thread-pool forms of concurrency isolation.
- [AWS workload isolation using shuffle sharding](https://aws.amazon.com/builders-library/workload-isolation-using-shuffle-sharding/).
  Examines another way to limit the set of users affected by shared-worker
  failures.

<https://engineer.company/glossary/bulkhead-pattern/>
