advancedAdvanced & Design

What is the bulkhead pattern, and how would you implement it in Java?

The bulkhead pattern isolates the resources used to call each downstream dependency, so that one slow or failing service cannot exhaust a shared thread pool and drag down calls to every other service as well. A common implementation uses a separate ThreadPoolExecutor for each downstream dependency, for example a small pool of five threads dedicated to a payment service, ten threads for a user service, and eight threads for an inventory service, each with its own bounded queue and an abort policy so it fails fast rather than queuing indefinitely when overwhelmed. With virtual threads, an equivalent effect can be achieved with a plain Semaphore per service, for instance new Semaphore(20) to cap concurrent calls to the payment service at 20 regardless of how many virtual threads exist overall. The name comes from the watertight compartments, or bulkheads, built into ships: a breach in one compartment is contained there and doesn't sink the whole vessel.

Ready to master this question?

Generate a complete walkthrough — background, the full answer in plain language, a working code example explained line by line, a real-world scenario, common mistakes, and how this same question gets asked in different ways.

Sign in to generate a response

Next Step

Continue to What is the difference between CompletableFuture's thenCompose and thenApply, and when do you use each?← Back to all Java Concurrency & Multithreading questions