Video in progress

Strong Middle 🏗️ System design

What is the bulkhead pattern, and how does it protect against one slow dependency stopping the whole application?

A bulkhead isolates resources (thread pools, connections, semaphores) separately for each dependency or kind of work. If one fills up, only that part rejects requests, and the rest keep working.

📖 Answer

The name comes from ships: the hull is divided into watertight compartments, and if water gets into one, the ship doesn't sink.

Without a bulkhead: the application has 200 request threads. The report service slows down, and each request going to it hangs for 20 seconds. Within a minute all 200 threads are waiting on reports. Login, the catalog, the cart — nothing responds, even though none of them depends on the report service.

With a bulkhead, say up to 20 concurrent calls are allocated to report calls. The 21st is rejected immediately (or waits briefly), and the other 180 threads stay free for other work.

BulkheadConfig cfg = BulkheadConfig.custom()
    .maxConcurrentCalls(20)
    .maxWaitDuration(Duration.ofMillis(50))
    .build();
Bulkhead reports = Bulkhead.of("reports", cfg);

Supplier<Report> call = Bulkhead.decorateSupplier(reports, () -> reportClient.fetch(id));

Other forms:

  • a separate connection pool for each external HTTP client (not one shared RestTemplate pool);
  • moving heavy export requests to separate pods or a separate queue;
  • in the DB, a separate user and statement_timeout for analytical queries;
  • in Kafka, a slow consumer in its own consumer group.

With Java 21 virtual threads, running out of threads is less of a problem, but a bulkhead is still needed: now it's the downstream service or the DB connections that run out, not threads. A semaphore-based bulkhead keeps working with virtual threads.

It's used together with a circuit breaker and a timeout: the timeout limits a single call, the bulkhead limits the number of parallel calls, and the breaker stops calling based on the overall state.