Microservice Patterns
Fallback
When a dependency fails, answer with something useful instead of an error — a default, a cached value or a reduced feature — so a non-essential failure never breaks an essential page.
What you will learn
- Separate essential from non-essential dependencies of a request.
- Choose between default values, last-known-good data and feature degradation.
- Understand the cost of stale data and of fallbacks that are never tested.
- Combine fallbacks with circuit breakers to fail fast.
Before you start
Overview
If the coffee machine breaks, a café does not close — it sells tea and pastries and puts up a sign. A fallback is that plan B: when a dependency fails, the service still answers with something useful, just a little less.
A fallback replaces a failed call's result with an alternative: a static default, the last value that worked, or a smaller version of the feature. It turns errors into degraded successes, which only makes sense for parts of a response the user can live without.
The problem: optional parts that are not optional in code
A product page shows the product, its price, stock — and a "you may also like" carousel from a recommendations service. If the code treats every call the same, a hung recommendations service fails the whole page. Customers cannot buy a product because a widget below the fold is unavailable.
Run it: four answers to the same outage
Recommendations hang from t = 6 s to t = 26 s. Compare success rate and p95: a default always works; cached answers work only for products seen before the outage; adding a circuit breaker removes the 800 ms wait before every fallback.
Fallback strategies
| Strategy | Example | Watch out for |
|---|---|---|
| Default value | Show bestsellers instead of personalised recommendations. | Users may not notice the degradation — log and measure it. |
| Last known good | Serve the last successful exchange rate or recommendation list. | Staleness: decide how old is too old (and never for prices you charge). |
| Reduced feature | Hide the carousel; disable "track my parcel" but keep checkout. | UX for the reduced state must be designed, not improvised. |
| Alternative source | Read from a replica or a secondary provider. | The alternative needs capacity for full traffic — and is rarely tested. |
| Queue for later | Accept the order and charge the card asynchronously. | Users must be told the outcome later; requires a reliable queue. |
Implementation with Resilience4j
@Service
class RecommendationsClient {
private final RestClient recs;
private final Cache<String, List<Product>> lastGood = Caffeine.newBuilder()
.maximumSize(50_000)
.expireAfterWrite(Duration.ofHours(6)) // older than this is too stale to show
.build();
RecommendationsClient(RestClient.Builder builder) {
this.recs = builder.baseUrl("http://recommendations").build();
}
@CircuitBreaker(name = "recommendations", fallbackMethod = "fallback")
@TimeLimiter(name = "recommendations")
public CompletableFuture<List<Product>> forProduct(String productId) {
return CompletableFuture.supplyAsync(() -> {
List<Product> result = recs.get().uri("/for/{id}", productId).retrieve().body(PRODUCT_LIST);
lastGood.put(productId, result);
return result;
});
}
private CompletableFuture<List<Product>> fallback(String productId, Throwable cause) {
List<Product> cached = lastGood.getIfPresent(productId);
return CompletableFuture.completedFuture(cached != null ? cached : Bestsellers.TOP_10);
}
private static final ParameterizedTypeReference<List<Product>> PRODUCT_LIST = new ParameterizedTypeReference<>() {};
}Trade-offs
| Benefits | Costs | |
|---|---|---|
| User experience | Pages keep working during partial outages. | Users may see stale or generic content without knowing. |
| Reliability | Isolates non-essential failures. | Fallback code runs rarely, so it is often broken when needed. |
| Complexity | Small, local code change. | Every fallback is a second code path to test and monitor. |
Production considerations
- Count fallbacks as a metric and alert on their rate — a silent fallback can hide an outage for days.
- Exercise fallbacks regularly (fault injection, game days); untested fallbacks fail at the worst moment.
- Keep fallbacks cheap and local. A fallback that calls another remote service can fail for the same reason.
- Pair with a Circuit Breaker so the fallback is served immediately instead of after a timeout.