Hystrix (https://github.com/Netflix/Hystrix) is a fault tolerance library developed by Netflix, which uses the circuit breaker pattern to increase the fault tolerance of your Java code. It works by wrapping your code in so called Commands and providing Fallback Methods.
It is integrated in spring cloud, with the spring-cloud-starter-hystrix maven artifact.
The spring integration allows you to declare your service methods as Hystrix commands with annotations, for example:
@HystrixCommand(fallbackMethod = "fallback", commandProperties = {
@HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = TIMEOUT)
})
public Object callService() {
//insert code to send a request to one of your microservices, which could fail, here
}
private Object fallback(){
//insert code to execute if service fails or takes too long to answer here
}
With above code snippet you can declare a time out for your method, after which the fallback method will be executed. Furthermore the fallback method is executed should an exception occur or if the method failed too often consecutively (circuit breaker pattern). This allows for fail fast semantics and the failing/overwhelmed microservice you are trying to call is given time to recover.