What are Cascading Failures and how do you prevent them?
Topic: microservices, Resilience. Level: Hard.
Answer
A cascading failure is a chain reaction: service A slows, its callers' threads pile up waiting on A, those callers become unresponsive, *their* callers fail, and the outage spreads upstream. Common triggers: no timeouts, unbounded retries, shared thread pools, and no backpressure. Prevention: aggressive timeouts, circuit breakers (fail fast when A is unhealthy), bulkheads (isolate A's blast radius), load shedding/rate limiting, and retry budgets with jitter. The goal is to convert a slow dependency into a fast, contained failure.
```java
// Layered defense converts a slow dependency into a fast, contained failure
Supplier<Reply> guarded =
Decorators.ofSupplier(() -> downstream.call(req))
.withThreadPoolBulkhead(bulkhead) // isolate blast radius
.withTimeLimiter(timeLimiter, scheduler) // fail fast on slowness
.withCircuitBreaker(circuitBreaker) // stop calling when unhealthy
.withFallback(List.of(Exception.class), ex -> Reply.degraded())
.decorate()::get;
```