Video in progress
A product page gets data from 6 services. What do you do so the page still opens when the recommendations or reviews service goes down?
You split each dependency into required and optional. The optional ones are called with a short timeout, a circuit breaker and a fallback (an empty block, stale data from the cache), and in parallel; if the required part (price, stock) is missing, you return an explicit error.
📖 Answer
The first step is to talk to the product owner: what can't be missing from the page without breaking the purchase? Usually the product data, price and stock are required. Recommendations, reviews, "frequently bought together" and rating are optional.
Then the technical part:
CompletableFuture<Product> product = supplyAsync(() -> catalog.get(id), pool);
CompletableFuture<List<Review>> reviews = supplyAsync(() -> reviewClient.top(id), pool)
.completeOnTimeout(List.of(), 300, MILLISECONDS)
.exceptionally(e -> List.of());
CompletableFuture<List<Item>> recs = supplyAsync(() -> recClient.forProduct(id), pool)
.completeOnTimeout(List.of(), 200, MILLISECONDS)
.exceptionally(e -> List.of());
return new ProductPage(product.join(), reviews.join(), recs.join());
Note: completeOnTimeout completes the future, but it doesn't stop the underlying HTTP call. So the HTTP client itself also needs a read timeout, otherwise the threads stay busy anyway.
Additions:
- a circuit breaker for every optional service — so you don't wait 200 ms on every request for a service that's down;
- make fallbacks meaningful: instead of recommendations, the top products in that category (from the cache); instead of reviews, hide the block;
- a separate thread pool or bulkhead — so a slow reviews service doesn't leave no threads for catalog calls;
- the frontend can also load the optional blocks later with a separate request — the main page opens faster.
It also helps to be able to turn off the heavy parts by hand through a feature flag at peak load (for example, on a big promotion day).
And metrics: record how many times the fallback fired. Otherwise nobody notices that recommendations came back empty for a week.
Sign up to mark studied questions and save your progress.