You are here

How to Implement Hystrix Circuit Breaker in Microservices Application?

Hystrix Circuit BreakerMicroservices based applications feature a huge amount of distributed components. These components interact with each other during the request processing. As the number of interactions over the network increases, the possibility of an abnormal behavior of a service may also increase. In that case, we need to have some handy techniques that can prevent or minimize this abnormal behavior. Here, we are talking about none other than a fault-tolerance mechanism.

The circuit breaker is fault-tolerance technique that monitors and detects when a service is behaving abnormally. It temporarily rejects those calls until the service becomes healthy again. Netflix Hystrix is an open source library which provides this solution. Hence, we are going to learn ‘How to implement Hystrix Circuit Breaker in Microservices Application?’ and its related concepts.

Netflix has provided a bunch of libraries to form an ecosystem in microservices. Like Eureka, Hystrix is also an open source library provided by Netflix in the Microservices space. Hystrix implements the Circuit Breaker pattern. You don’t have to write the network or thread programming to handle fault tolerance in the Microservices. You need to use Hystrix library just by giving parameters and that’s it.

Hystrix is going to do all the work for you internally. The best part of it is that it works amazingly with Spring Boot. If you pick any Microservice based project, there are pretty good chances that they are using Hystrix. Let’s start discussing our topic ‘How to implement Hystrix Circuit Breaker in Microservices Application?’ and other concepts related to it.

What is Circuit Breaker in Microservices?

In a microservice based application, Circuit Breaker is a technique, where we stop executing an erroneous method and redirect every request to a custom method (Fallback method). Generally, we stop execution of a particular method if it is continuously throwing an exception. When we break the circuit, we also avoid any cascading failures to the downstream services. Its basic function is to interrupt current flow after a fault is detected. A circuit breaker can be reset to resume normal operation either manually or automatically.

What is a Fallback Method in Microservices?

If an actual method of a microservice is throwing exception continuously, then we avoid execution of the actual logic for some time. Instead, we redirect the request to a Dummy method that provides the response back to client’s request. Such dummy method is called as Fallback method. This method can provide dummy responses such as ‘Service Not Working’, ‘Unable to Process’, ‘try after some time’ etc.

Why do we need a Circuit Breaker?

It’s very common for software applications to make remote calls to a software program, possibly running on different machines across a network. One of the big differences between in-memory calls and remote calls is that remote calls may fail, or hang without a response until some timeout limit is reached. What’s worse, if you have many requests on an unresponsive service, then you can run out of critical resources leading to cascading failures across multiple systems.

Michael Nygard in his wonderful book Release It has publicized the Circuit Breaker pattern to prevent this kind of fatal cascade. The basic idea behind the circuit breaker is very simple. You wrap a protected function call in a circuit breaker object, which monitors for failures. Once the failures reach a certain threshold, the circuit breaker falls.

What are the states of Circuit Breaker?

Depending on the state, Circuit Breaker changes its behavior. There are three states of a Circuit Breaker.

Closed 

If Client request is sent to the actual service method only, then it is called as CLOSED CIRCUIT. Hence, this state represents that the service is running properly and providing the expected functionality.

Open 

If Client request is redirected to Fallback method, then such case is an OPEN CIRCUIT. Hence, this state represents that service is unavailable or faulty and error rate is beyond the threshold.

Half-Open 

Once the state becomes OPEN, we wait for some time in the OPEN state. After a certain period of time, the state becomes HALF_OPEN.
During this period, we do send some requests to Service to check if we still get the proper response. If the failure rate is below the threshold, the state would become CLOSED. If the failure rate is above the threshold, then the state becomes OPEN once again. This cycle continues till the service becomes stable.

How to Implement Hystrix Circuit Breaker in Microservices Application?

Let’s create a Spring Boot Application to understand the implementation of Hystrix Circuit Breaker by using Spring Cloud Hystrix. We will create the example step by step.

Step #1 : Create a Spring Boot Project in STS(Spring Tool Suite)

Here, we will use STS(Spring Tool Suite) to create our Spring Boot Project. If you are new to Spring Boot, visit Internal Link to create a sample project in spring boot using STS. While creating a project in STS add starters ‘Hystrix’, ‘Hystrix Dashboard’, ‘Spring Web’, ‘Spring Boot Actuator’ in order to get features of it.

Step #2: Apply Annotation @EnableHystrix and @EnableHystrixDashboard at the main class  

In order to implement Circuit Breaker in our application/microservice, we need to apply @EnableHystrix at the main class of your application. To see the analytics provided by Hystrix as a dashboard, apply @EnableHystrixDashboard at the main class.

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;

@SpringBootApplication
@EnableHystrix
@EnableHystrixDashboard
public class SpringCloudPaymentCircuitBreakerApplication {

          public static void main(String[] args) {
                 SpringApplication.run(SpringCloudPaymentCircuitBreakerApplication.class, args);
          }
}

Step #3: Modify application.properties file

Add below properties in your application.properties file.

server.port=9898
spring.application.name=PAYMENT-APP
#can include eureka details also—-
management.endpoints.web.exposure.include=*

Step #4: Write a RestController to implement the Hystrix 

In order to implement our functionality, let’s write a RestController as PaymentRestController.

import java.util.Random;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;

@RestController
@RequestMapping("/payment")
public class PaymentRestController {

       @GetMapping("/pay")
       @HystrixCommand(fallbackMethod = "doDummyPay",
                                         commandProperties = {
                                                  @HystrixProperty(
                                                                                 name= "circuitBreaker.requestVolumeThreshold",
                                                                                 value="6"),
                                                  @HystrixProperty(
                                                                                 name= "circuitBreaker.sleepWindowInMilliseconds", 
                                                                                 value="10000"),
                                                  @HystrixProperty(
                                                                                 name= "circuitBreaker.enabled", 
                                                                                 value = "false")
                                        } )
        public String doPayment() {
               System.out.println("---Start of  PAYMENT-METHOD---");
               if(new Random().nextInt(10) <=10) {
                     throw new RuntimeException("DUMMY Example");
               }
               System.out.println("---End of PAYMENT-METHOD---");
               return "SUCCESS";
        }

       public String doDummyPay() {
              System.out.println("---FROM FALLBACK METHOD---");
              return "SERVICE IS DOWN, PLEASE TRY AFTER SOMETIME !!!";
       }
}

♦ Note: On application startup, default State of Circuit is CLOSED. Moreover, Default Exception count is 20 to open a Circuit and default re-try time is 5 sec to close a Circuit for the next request to the actual method (if it succeeds). Even after RE-CLOSED State, if an exception occurred (for one request) then it will be moved to the OPEN state for 5 more sec.

Circuit Breaker Command Properties

circuitBreaker.requestVolumeThreshold : default 20 (20 exceptions count). This is the count of requests with exceptions.
circuitBreaker.sleepWindowInMilliseconds : default re-try time 5 sec (represents the waiting time for a re-try the next request in CLOSED CIRCUIT).
circuitBreaker.enabled : default value true, start app state as CLOSED CIRCUIT.

Step #5: Set up Spring Cloud Hystrix Dashboard

We utilize Hystrix Dashboard to find out the current/live status of Microservice Application. Moreover, we can also check if the circuit state is open or closed. Additionally, how many requests it is processing, failed or success etc. (current details only).

♦ Note : It is just a recommendation to please modify spring cloud version in pom.xml as below
<spring-cloud.version>Hoxton.SR4</spring-cloud.version>
Since some modules are still in Maintenance phase. However, this is a recommendation at the time of writing this article. Other versions may also work for you based on your testing.

How to test the Circuit Breaker implemented Application?

Please follow below steps to run & test your application:

Step#1: Run the application

Run your application by right clicking on the project and Run As -> Spring Boot App
Open your browser and hit the URL : http://localhost:9494/payment/pay

Step#2: Start Hystrix Dashboard

Open other tab of Browser & hit URL : http://localhost:9494/hystrix
Now you will see the Hystrix dashboard.

Step#3: Enter Application URL in the Hystrix Dashboard 

Enter Application URL in the Hystrix Dashboard as below:

http://localhost:9494/actuator/hystrix.stream
Now click on the ‘Monitor Stream’ button.

How to Implement Hystrix Circuit Breaker in Microservices Application?

Step#4: Refresh the Application URL

Refresh your application’s URL (http://localhost:9494/payment/pay) multiple times in order to check the behavior.
Now, check the Hystrix Dashboard screen for details something like below.

How to Implement Hystrix Circuit Breaker in Microservices Application?

FAQ

Is Hystrix still actively maintained and supported?

As of an update in January 2022, Netflix (the original creator of Hystrix), had announced that they would no longer maintain the project. However, the Hystrix project had been forked by the community and was actively maintained as the “Netflix Hystrix Fork.” You should check the latest updates and community support for the most current information. Be sure to refer to the official Spring Boot and Hystrix documentation for the most up-to-date information.

For the requirements where something like Hystrix makes sense, we can continue using Hystrix for existing applications, and to leverage open and active projects or new projects utilize resilience4j as a latest technique. It is highly advisable to do the same.

Conclusion

After going through all the theoretical & example part of ‘How to Implement Hystrix Circuit Breaker in Microservices Application?’, finally, we should be able to implement the circuit breaker concept using Netflix Hystrix as a whole. Similarly, we expect from you to further apply this knowledge in your project accordingly. Moreover, having the familiarity of terminologies explained in this article is very crucial to implement microservices for a developer especially in Java. In addition, we will also be updating the article time to time accordingly if any need arises. Moreover, feel free to provide your comments in the comments section below.

 

 

 

 

 

 

 

 

 

 

 

3 thoughts on “How to Implement Hystrix Circuit Breaker in Microservices Application?

  1. Very clear and basic explanation about Hystrix and Circuit Breaker Pattern applying in Microservices.

    Thanks.

  2. Very informative article about circuit breaker that everyone should aware. Thank you so much for your valuable post.

Leave a Reply


Top