Netflix Hystrix

Overview

Netflix Hystrix is a powerful library designed to enhance the resilience and robustness of distributed systems. It achieves this by controlling the interactions between distributed services, adding latency tolerance and fault tolerance logic. Hystrix employs the circuit breaker pattern to manage remote service calls, ensuring that applications remain responsive even when some services are experiencing failures. This is particularly crucial in complex distributed systems where failures are inevitable.

Hystrix isolates points of access to remote systems, services, and third-party libraries, effectively stopping cascading failures and enabling resilience. By doing so, it helps maintain the overall health and performance of an application, even under adverse conditions.

Circuit Breaker Pattern

The circuit breaker pattern is a key feature of Netflix Hystrix. It allows developers to wrap potentially failing calls to remote services with a circuit breaker, which can handle timeouts and failures gracefully. This is implemented in Hystrix by annotating methods with @HystrixCommand. When a method is annotated with @HystrixCommand, Hystrix manages the method call, providing a fallback mechanism in case of failure.

Example: Wrapping a Remote Resource Call

Below is an example of how to use the @HystrixCommand annotation to wrap a method call with a Hystrix circuit breaker:

//Imports removed for conciseness
@HystrixCommand                                                         #1
public List<License> getLicensesByOrg(String organizationId){
    return licenseRepository.findByOrganizationId(organizationId);
  }
#1 - @HystrixCommand annotation is used to wrapper the getLicenseByOrg() method with a Hystrix circuit breaker.

In this example, the getLicensesByOrg method is wrapped with a Hystrix circuit breaker using the @HystrixCommand annotation. This ensures that if the licenseRepository.findByOrganizationId call fails or times out, the system can handle the failure without affecting the overall application performance.

Figures

Hystrix in Action

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. 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.

This figure illustrates how Hystrix acts as an intermediary between the client and remote resource calls, whether they are database calls or REST-based service calls. By doing so, Hystrix protects the client from failures in these remote resources.

Circuit Breaker Checks

Figure 5.9. Hystrix goes through a series of checks to determine whether or not to trip the circuit breaker. Figure 5.9. Hystrix goes through a series of checks to determine whether or not to trip the circuit breaker.

This figure demonstrates the process Hystrix follows to decide whether to trip the circuit breaker. By performing a series of checks, Hystrix can determine the health of a service and decide whether to allow or block requests to it, thus preventing potential failures from cascading through the system.