Hystrix
Hystrix
Hystrix is a fault-tolerant library that helps prevent cascading failures by implementing the circuit breaker pattern.
- Latency and fault tolerance management
- Implements the circuit breaker pattern
- Isolates service failures to avoid system-wide issues
- Ensures graceful degradation in case of failures

Example
Service B
@GetMapping("/serviceB")
@HystrixCommand(fallbackMethod = "fallbackServiceA")
public String serviceB() {
String response = restTemplate.getForObject("http://service-a/serviceA", String.class);
return "Service B calls " + response;
}
public String fallbackServiceA() {
return "Fallback response from Service B";
}
Service C
Fallback Method: In Service C, the @HystrixCommand annotation is used in a similar way. It wraps the call to Service B. If this call fails, Hystrix will call the fallbackServiceB method.
@GetMapping("/serviceC")
@HystrixCommand(fallbackMethod = "fallbackServiceB")
public String serviceC() {
String response = restTemplate.getForObject("http://service-b/serviceB", String.class);
return "Service C calls " + response;
}
public String fallbackServiceB() {
return "Fallback response from Service C";
}
Summary of Hystrix Usage
Service B:
Primary Method: serviceB()
Fallback Method: fallbackServiceA()
If the call to Service A fails, the system falls back to returning "Fallback response from Service B".
Service C:
Primary Method: serviceC()
Fallback Method: fallbackServiceB()
If the call to Service B fails, the system falls back to returning "Fallback response from Service C".
Benefits of Using Hystrix
- Fault Isolation: If one service fails (like Service A), it does not affect the others (Service B and Service C).
- Fallback Mechanism: Provides an alternative response, which can improve user experience by avoiding failures.
- Prevent Cascading Failures: Helps prevent failures from propagating through the system, allowing the application to remain functional even when some services are down.