Most descriptions of resilience patterns take one at a time: here is a circuit breaker, here is a bulkhead. In a real service they interact, and the combination matters more than any single one. This post applies four patterns, timeouts, bulkheads, fallbacks and load shedding, to one service our team runs, and then describes the drill we used to check whether they work together.
The service
product-page assembles everything the product detail page needs. It calls five dependencies:
| Dependency | Needed for | If it fails |
|---|---|---|
| catalog | title, images, description | page cannot render |
| pricing | price, discounts | page cannot sell |
| inventory | stock badge | can show "check availability" |
| reviews | rating and top reviews | can hide the section |
| recommendations | "customers also bought" | can hide the section |
At peak it handles about 3,000 requests per second across 12 instances, each with a thread pool of 200 for outgoing calls.
Before this work, all five calls shared that pool, had a 5 second timeout, and had no fallbacks. A slow reviews service once made the whole page time out, because 200 threads were all waiting on reviews and none were left for catalog.
Pattern 1: timeouts that match what the caller can wait
A timeout is the most basic protection: never wait longer than the result is worth. We set each timeout from the dependency's normal latency and the page's budget, not from a default:
| Dependency | Normal p99 | Timeout |
|---|---|---|
| catalog | 25 ms | 250 ms |
| pricing | 30 ms | 250 ms |
| inventory | 15 ms | 150 ms |
| reviews | 60 ms | 300 ms |
| recommendations | 80 ms | 300 ms |
The page's overall budget is 600 ms at the gateway. Calls run in parallel, so the slowest timeout (300 ms) plus our own rendering fits.
Timeouts alone do not prevent the pool problem. A dependency that is slow but under its timeout still holds threads for 300 ms instead of 60 ms, five times longer.
Pattern 2: bulkheads
A bulkhead isolates resources per dependency, like watertight compartments in a ship, so a problem in one compartment cannot flood the others. We gave each dependency its own limited concurrency:
resilience4j:
bulkhead:
instances:
catalog: { maxConcurrentCalls: 80, maxWaitDuration: 0 }
pricing: { maxConcurrentCalls: 80, maxWaitDuration: 0 }
inventory: { maxConcurrentCalls: 40, maxWaitDuration: 0 }
reviews: { maxConcurrentCalls: 30, maxWaitDuration: 0 }
recommendations: { maxConcurrentCalls: 30, maxWaitDuration: 0 }
How we sized them: by Little's law, concurrency needed equals request rate times latency. Per instance, 250 requests per second times 60 ms for reviews is about 15 concurrent calls. We allowed 30, double the normal need. If reviews slows down to 300 ms, it would need 75, but it only gets 30. The other 45 calls are rejected immediately and go to the fallback, and the remaining capacity stays available for catalog and pricing.
maxWaitDuration: 0 means a call that cannot get a permit fails at once rather than queueing. Queueing would
only add latency to requests that are going to use the fallback anyway.
The critical dependencies, catalog and pricing, have the largest bulkheads, so a problem elsewhere cannot starve them.
Pattern 3: fallbacks that are honest
A fallback is what you return when a dependency is unavailable. Good fallbacks degrade the page in a way the user understands; bad ones hide errors or show wrong data.
| Dependency | Fallback |
|---|---|
| catalog | none: return an error page (503); a product page without the product is not useful |
| pricing | last known price from a 60 second local cache, only for display, with "Add to cart" disabled and the message "Price is being updated" |
| inventory | "Check availability at checkout" instead of a stock badge |
| reviews | hide the reviews section |
| recommendations | show a static list of bestsellers in the category, refreshed hourly |
Two rules we hold to:
- Never invent data that affects money or commitments. A stale price can be shown, clearly marked, but cannot be used to sell. Checkout always reads the live price.
- Make fallbacks cheap. A fallback that calls another service, or queries the database heavily, can fail under the same conditions as the original. Ours read from memory.
Pattern 4: load shedding
Bulkheads protect us from slow dependencies. Load shedding protects us from too much incoming traffic: when the service itself is at capacity, it is better to reject some requests quickly than to accept all of them and serve every one slowly.
We shed at the edge of the service based on concurrent in-flight requests:
public class ConcurrencyLimitFilter implements Filter {
private final Semaphore permits = new Semaphore(400); // per instance
@Override
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain)
throws IOException, ServletException {
if (!permits.tryAcquire()) {
HttpServletResponse r = (HttpServletResponse) res;
r.setStatus(503);
r.setHeader("Retry-After", "1");
return;
}
try {
chain.doFilter(req, res);
} finally {
permits.release();
}
}
}
The limit of 400 came from load testing: past about 450 concurrent requests per instance, latency rose sharply and throughput fell. Rejecting at 400 keeps accepted requests fast.
We also prioritise. Health checks and requests from the checkout flow (marked by the gateway with a header) use a separate, reserved pool of permits, so shedding never blocks a customer who is already paying. Bot traffic, identified at the edge, is shed first.
How the patterns fit together
For one request when reviews is slow:
- The request passes load shedding (the service has capacity).
- Five calls start in parallel, each acquiring a permit from its bulkhead.
- Reviews' bulkhead is full; the call fails immediately and the fallback hides the section.
- Catalog and pricing complete in normal time with their own permits.
- The page renders in about 120 ms without reviews.
Without the bulkhead, step 3 would have waited, and with enough slow reviews calls, steps 4 and 5 would have waited too. Without the timeout, calls that did get permits would hold them indefinitely. Without the fallback, the page would have returned an error. Each pattern covers a gap the others leave.
The drill
Configuration that has never been tested under failure is a hypothesis. We ran a game day in staging with production-like load (2,500 requests per second from k6) and injected one failure at a time with a fault proxy in front of each dependency.
| Scenario | Expected | Observed | Action |
|---|---|---|---|
| reviews +2 s latency | reviews hidden, page p95 normal | as expected, p95 140 ms | none |
| recommendations returns 500s | bestsellers shown | as expected | none |
| inventory down (connection refused) | "check availability" | page p95 rose to 900 ms | fixed, see below |
| pricing +500 ms | display stale price, add to cart disabled | as expected | improved the message |
| catalog down | 503 error page quickly | as expected, 503 in 5 ms after breaker opened | none |
| traffic at 2x capacity | some 503s, accepted requests fast | as expected, p95 of accepted requests 180 ms | none |
The inventory surprise: "connection refused" should fail instantly, but our HTTP client had a connection pool with a 1 second wait for a free connection, and DNS for the inventory service was being re-resolved on every failure with a slow resolver. Every call waited about a second before failing. We set the connection acquire timeout to 50 ms and cached DNS results properly. On the re-run, the page p95 stayed at 130 ms with inventory down.
That finding alone justified the drill. Every individual pattern had been configured "correctly", and the system still had a one-second hole in it.
Running drills regularly
We now repeat the drill every quarter and after any significant change to the service's dependencies. Each drill has a written hypothesis per scenario, a stop condition (abort if staging error rate exceeds 50 percent for reasons unrelated to the injected fault), and a short write-up with action items.
Summary
- Set timeouts from each dependency's real latency and the caller's budget.
- Use bulkheads so one slow dependency cannot use up the resources needed for the others; size them with request rate times latency, plus headroom.
- Fallbacks should degrade honestly and cheaply, never inventing data that affects money.
- Shed load early when the service itself is saturated, and reserve capacity for the most important traffic.
- Test the combination with failure drills; correct configuration of each pattern does not guarantee the system behaves.
References: Resilience4j Bulkhead, Microsoft Azure Architecture Center, Bulkhead pattern, Google SRE book, handling overload, AWS Builders' Library, using load shedding to avoid overload.
Comments
0No comments yet. Be the first to comment!
Sign In to post a comment.