What do you mean by Hystrix:
Hystrix is a circuit breaker library developed by Netflix. Hystrix is used in distributed systems to achieve resilience because of it's fault tolerance mechanism.
Why is necessary to use Hystrix?
In a distributed system like microservice architecture, we have multiple services that interact with each other to achieve certain business goals. In this type of architecture, it is very easy that if a single service fails, all the subsequent services depending on or getting data from that service will also fail. And eventually, the error will propagate to the customer which will cause bad customer experience. Hence, it is better to have a fall back mechanism so that whenever some underlying application is not behaving as expected we will stop making calls to that service and call the fallback method. This will also give some time to the unhealthy system to allow it to recover which is not possible, if the unhealthy service is getting requests continuously.
Hystrix is circuit breaker library which means that after getting failure from the unhealthy microservice a certain number of times, the circuit breaker will trip and not allow requests to go through.
After the circuit trips the service will depend on the fallback method to get the response. This will prevent a cascading of errors to all the dependent services.
For example, let us consider the figure below which has a critical service payment. It is dependent on a microservice promotion and due to some reason there is a failure in the promotion microservice. The promotion microservice is not very critical because even if a customer can’t apply the promotion code for the purchase, he should be allowed to pay and purchase the product. So when promotion throws an error we can call the fallback method to show an error message saying that the promotion code can’t be applied. And after seeing this message, if the customer wishes he can purchase the product, without applying the promotion code.
Thus a hystrix is helpful in graceful degradation of an error as it stops the propagation of error, using it's fallback mechanism system.
How to add Hystrix in your project ?
Now that we know all about Hystrix, let’s implement it to any of our project.
Add the following hystrix dependency provided by Spring cloud Netflix to pom.xml file.
org.springframework.cloud
spring-cloud-starter-hystrix
1.0.0.RELEASE
Then add the annotation @EnableCircuitBreaker or @EnableHystrix (this annotation is particularly for enabling hystrix only) to the configuration file.
@Configuration
@EnableCircuitBreaker
public class HystrixConfiguration{}
Add @HystrixCommand annotation to the method that can cause system failure and in case of an error, add the name of the method that it should fallback to. In our case validatePromotionCouponFallbackMethod will be the fallback method.
@HystrixCommand(fallbackMethod = "validatePromotionCouponFallbackMethod")
public String validatePromotionCoupon(String couponCode, String url){
RestTemplate restTemplate = new RestTemplate();
return restTemplate.getForEntity(url+ “/” +couponCode, String.class);}
Then write the method validatePromotionCouponFallbackMethod() which will handle graceful degradation of the error.
private String validatePromotionCouponFallbackMethod(String couponCode, String url){ return "Promotion Service currently unavailable";}
So whenever the promotion microservice is not responding the fallback method validatePromotionCouponFallbackMethod() will be called which will return a custom message for customer.
This way Hystrix will be added to your project to achieve resiliency in few simple steps.
Configurations for a Hystrix:
There are multiple properties that a Hystrix provides which we can configure according to our will. This properties can be specified either, in the configuration file or in the properties file. If none of them are configured, the hystrix will take the default properties. The threshold, fallback properties, command properties, thread-pool properties for hystrix can be configured. If you have multiple hystrix configured you can have dynamic instances containing specific properties for each hystrix. All the netflix libraries use Archaius for default properties implementation.
Conclusion:
A hystrix adds the ability to fallback to a certain method and skip an intended call in case of an error. It provides circuit breakers which will trip after the threshold. Hence it prepares developer for failure beforehand instead of thinking what to do after the failure has already occurred.
REFERENCES:
Comments