Video in progress
One service calls several external systems, and when one of them slows down everything stops. How do you design a bulkhead at the thread level?
Each external dependency gets its own bounded resource, a separate thread pool or a semaphore. That way a slow dependency uses up only its own share, and the other endpoints keep working.
📖 Answer
Without a bulkhead: Tomcat has 200 threads. The recommendation service moves to 10-second responses. Within seconds all 200 threads are hanging on it, and even /health stops answering. Kubernetes restarts the pod, and the problem moves to another pod.
Two kinds of bulkhead:
| Type | How | When |
|---|---|---|
| Thread pool | a separate executor per dependency | a blocking client, and you need to free the caller with a timeout |
| Semaphore | on the caller's thread, with a limit on concurrent calls | an async client or virtual threads |
With Resilience4j:
resilience4j:
bulkhead:
instances:
recommendations:
maxConcurrentCalls: 20
maxWaitDuration: 0 # reject right away if there's no room
timelimiter:
instances:
recommendations:
timeoutDuration: 800ms
@Bulkhead(name = "recommendations")
@TimeLimiter(name = "recommendations")
@CircuitBreaker(name = "recommendations", fallbackMethod = "emptyRecommendations")
public CompletableFuture<List<Item>> recommend(long userId) { ... }
A bulkhead doesn't work alone. Without a timeout, even those 20 slots stay occupied forever. Without a circuit breaker, we keep hitting a dead service anyway. And a fallback protects the user experience: a page without recommendations is better than an empty page.
How to pick the size: by Little's law, normal RPS × p99 latency, plus headroom. For example, 100 RPS × 0.15 s = 15, so a bulkhead of 25.
The organizational side: you have to classify dependencies by how critical they are. For payments the bulkhead is large and there's no fallback (returning an error is better); for recommendations it's small and there is a fallback. Every bulkhead's rejections should be in metrics, otherwise it silently switches off functionality.
Sign up to mark studied questions and save your progress.