API / Microservices Design Patterns Interview Questions
What is the Bulkhead pattern for resource isolation (thread pools, connection pools)?
The Bulkhead pattern at the resource level isolates the thread pools and connection pools used to call different downstream dependencies, so that a slow or failed dependency cannot monopolise the shared pool and block calls to unrelated services.
Without Bulkhead: all outbound calls from Service A (to Inventory, Payment, and Notification) share one thread pool. If Inventory becomes slow and holds threads for 30 seconds each, it quickly fills the entire pool. Calls to Payment and Notification then queue up even though those services are healthy.
With Bulkhead: each downstream dependency gets its own bounded pool. Inventory gets 10 threads; Payment gets 10 threads; Notification gets 5 threads. A stalled Inventory pool only blocks Inventory calls.
Two isolation strategies:
- Thread pool isolation — each dependency's calls execute on a dedicated thread pool. The calling thread is released immediately; the dedicated pool thread handles the blocking call. Supports async timeouts because the pool thread can be interrupted. Higher overhead (context switching between pools).
- Semaphore isolation — each dependency is limited to N concurrent calls using a semaphore. No separate thread pool; the calling thread itself makes the blocking call, limited by the semaphore count. Lower overhead but no support for independent timeout interruption — a hung call holds the semaphore and the calling thread.
// Resilience4j Bulkhead (semaphore)
BulkheadConfig config = BulkheadConfig.custom()
.maxConcurrentCalls(10)
.maxWaitDuration(Duration.ofMillis(100))
.build();
Bulkhead bh = BulkheadRegistry.of(config).bulkhead("inventoryService");
Supplier<Stock> decorated = Bulkhead.decorateSupplier(bh,
() -> inventoryClient.getStock(sku));
Invest now in Acorns!!! 🚀
Join Acorns and get your $5 bonus!
Acorns is a micro-investing app that automatically invests your "spare change" from daily purchases into diversified, expert-built portfolios of ETFs. It is designed for beginners, allowing you to start investing with as little as $5. The service automates saving and investing. Disclosure: I may receive a referral bonus.
Invest now!!! Get Free equity stock (US, UK only)!
Use Robinhood app to invest in stocks. It is safe and secure. Use the Referral link to claim your free stock when you sign up!.
The Robinhood app makes it easy to trade stocks, crypto and more.
Webull! Receive free stock by signing up using the link: Webull signup.
More Related questions...
