Building Fault-Tolerant MicroServices using Netflix’s Hystrix
Microservices are a collection of independent, autonomous services compared to the monolithic application where all the modules are tightly fitted into one application.
In Microservices architecture it is very essential to develop fault-tolerant microservices. We must develop the microservices in such a way that the fault impact must be minimized and should be based on the fast-fail mechanism to have minimum impact.
Here is an example of e-commerce application built on the microservices architecture.
The above architecture was working well until one day the e-commerce application stopped working. It was found that the service which fetches the list of available items was not responding. On further analysis, it was found that it used Third party service which was faulty. The error has cascaded from third-party service to Main Application.
Consider another example while using an e-commerce application the user items recommendation service stops working. In this case, we should display the items not specific to the user. In this way, the user will not be able to know that our recommendation engine failed. That’s why it is very important to build fault-tolerant microservices.
Netflix has one of the best infrastructures in the world. For building fault-tolerant microservices, Netflix came up with Hystrix they made it open source.
According to Netflix “ Hystrix is a latency and fault tolerance library designed to isolate points of access to remote systems, services, and 3rd party libraries, stop cascading failure and enable resilience in complex distributed systems where failure is inevitable.”
We will build an application using Hystrix and make it fault tolerant. We will create an application which will fetch the list of user recommendation movies. We will use Spring boot to create microservices.
Go to Spring Initializer and enter the Group name, Artifact name and select the dependencies. Click on generate the project, unzip it and generate it on your favorite IDE (I am using IntelliJ for the example). I have created two applications — “UserApplication” and “UserRecommendation” using spring initializer.
In UserRecommendation application create a rest controller with name “UserRecommendationController” and a POJO Movie which will contain Movie name, release date, and genre. We will populate the list of movies in a static list.
I have created a rest call “getUserMovieDetails” which will fetch the list of all the user-specific movies. Also, we are using port number 8081 by defining the port number in the application.properties file under the resources folder. Starting the tomcat server and when we hit the rest URL we get the result as :
Now we will build User Application which will use the Netflix Hystrix and will call the user recommendation microservice.
Create another Application using the same procedure and import it in IDE. We will use port number 8082 to run this service. Configure the port number as we did in the previous service.
In User Application, we need to enable Hystrix and Hystrix Dashboard. We can do that by adding following annotations.
For implementing fall back mechanism we use the following code :
Here if the method call to getMovieDetails fails then its fallback method call movieDetailsFallback will be called.
Now let’s test the fallback mechanism
We have two microservices running on port 8081 and port 8082. Basically, service running on 8081 gives us movie list based on user recommendation and service running on 8082 gives call service running on 8081 first and if it fails then it displays the general movie list not specific to the user. Both tomcat servers are running now.
Lets first test the user recommendation service running on 8081:
Now test the user service which in turns calls the user recommendation service. As the recommendation service is up here fall back won’t occur and we get the exact same result.
Now we will stop the recommendation service running on port 8081.
Now when we try to hit user service running on 8082 it will call its fall back method and will still give us the result.
As our recommendation service is down, we are showing the movies not specific to the user recommendation by calling the fallback method. In gist above pasted containing UserService.java, we can see the log on the console.
Hystrix also provides us dashboard to monitor the statistics.
Go to this URL — “http://localhost:8082/hystrix” and enter “http://localhost:8082/actuator/hystrix.stream” in the URL input field and click on monitor stream.
Here we can see all the details related to the method we have implemented a fallback mechanism. We can see success, timeout, and failed percentages. Also related information regarding thread pools.
So in this way, we can build fault-tolerant microservices using Hystrix.
You can find the source code in my GitHub repository: