InterviewSolution
| 1. |
What is Hystrix and how it can be implemented in the Spring Boot application? |
|
Answer» Netflix’s Hystrix is a library that provides an implementation of the Circuit BREAKER pattern for Microservices based applications. A circuit breaker is a pattern that monitors for failures and once the failures reach a certain threshold, the circuit breaker TRIPS, and all further calls will return with an error, without the external call being made at all. On applying Hystrix circuit breaker to a method, it watches for failing calls to that method, and if failures build up to a threshold; Hystrix opens the circuit so that subsequent calls automatically fail. While the circuit is open, Hystrix redirects call to a specified method called a fallback method. This creates a time buffer for the related service to recover from its failing state. Below are the annotations used to enable Hystrix in a Spring Boot application: @EnableCircuitBreaker: It is added to the main Application class for enabling Hystrix as a circuit breaker and to enable hystrix-javanica; which is a wrapper around native Hystrix required for using the annotations. @HystrixCommand: This is method annotation that notifies Spring to wrap a particular method in a proxy connected to a circuit breaker so that Hystrix can monitor it. We also need to define a fallback method having the backup logic that needs to be EXECUTED in the failure scenario. Hystrix passes the control to this fallback method when the circuit is broken. This annotation can also be used for asynchronous requests. Currently, it works only with classes marked with @COMPONENT or @Service. |
|