Chapter 5. When bad things happen: client resiliency patterns with Spring Cloud and Netflix Hystrix
Distributed systems are expected to fail, but the chapter emphasizes that failure is often subtle rather than total: a service may slow down, become intermittent, and gradually consume threads, connections, and other scarce resources until an application collapses. Because remote calls are usually synchronous and many systems are designed only for outright outages, a degraded dependency can create cascading failures across multiple services. To address this, the chapter introduces client-side resiliency patterns that protect consumers from unhealthy downstream resources and help prevent localized problems from spreading upstream.
The core resiliency techniques described are client-side load balancing, circuit breakers, fallbacks, and bulkheads. Load balancing helps route around bad instances, circuit breakers stop waiting on slow calls and fail fast once failures exceed a threshold, fallbacks provide an alternate response path when a call fails, and bulkheads isolate different categories of remote calls into separate thread pools so one slow dependency cannot exhaust all available capacity. Together, these patterns let applications remain partially available, recover more gracefully, and avoid the “cascading death” that can occur when one misbehaving service overwhelms the rest of the system.
The chapter then shows how Spring Cloud and Netflix Hystrix make these patterns practical to implement with relatively little code. By adding the Hystrix dependencies, enabling circuit breaking, and annotating methods with @HystrixCommand, developers can wrap database and service calls with timeouts, fallbacks, and custom thread-pool isolation. The text also explains how to tune timeout values, request thresholds, sleep windows, and thread-pool sizes, and it warns that default settings may not be sufficient for real workloads. Finally, it addresses a more advanced concern: preserving request context such as correlation IDs across Hystrix-managed threads by using a custom concurrency strategy and callable wrapper, ensuring operational data is not lost when calls are isolated for resilience.
Figure 5.1. The four client resiliency patterns act as a protective buffer between a service consumer and the service.
Figure 5.2. An application is a graph of interconnected dependencies. If you don’t manage the remote calls between these, one poorly behaving remote resource can bring down all the services in the graph.
Figure 5.3. The circuit breaker trips and allows a misbehaving service call to fail quickly and gracefully.
Figure 5.4. Hystrix sits between each remote resource call and protects the client. It doesn’t matter if the remote resource call is a database call or a REST-based service call.
Summary
- When designing highly distributed applications such as a microservice-based application, client resiliency must be taken into account.
- Outright failures of a service (for example, the server crashes) are easy to detect and deal with.
- A single poorly performing service can trigger a cascading effect of resource exhaustion as threads in the calling client are blocked waiting for a service to complete.
- Three core client resiliency patterns are the circuit-breaker pattern, the fallback pattern, and the bulkhead pattern.
- The circuit breaker pattern seeks to kill slow-running and degraded system calls so that the calls fail fast and prevent resource exhaustion.
- The fallback pattern allows you as the developer to define alternative code paths in the event that a remote service call fails or the circuit breaker for the call fails.
- The bulkhead pattern segregates remote resource calls away from each other, isolating calls to a remote service into their own thread pool. If one set of service calls is failing, its failures shouldn’t be allowed to eat up all the resources in the application container.
- Spring Cloud and the Netflix Hystrix libraries provide implementations for the circuit breaker, fallback, and bulkhead patterns.
- The Hystrix libraries are highly configurable and can be set at global, class, and thread pool levels.
- Hystrix supports two isolation models: THREAD and SEMAPHORE.
- Hystrix’s default isolation model, THREAD, completely isolates a Hystrix protected call, but doesn’t propagate the parent thread’s context to the Hystrix managed thread.
- Hystrix’s other isolation model, SEMAPHORE, doesn’t use a separate thread to make a Hystrix call. While this is more efficient, it also exposes the service to unpredictable behavior if Hystrix interrupts the call.
- Hystrix does allow you to inject the parent thread context into a Hystrix managed Thread through a custom HystrixConcurrencyStrategy implementation.