DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Enterprise AI Trend Report: Gain insights on ethical AI, MLOps, generative AI, large language models, and much more.

2024 Cloud survey: Share your insights on microservices, containers, K8s, CI/CD, and DevOps (+ enter a $750 raffle!) for our Trend Reports.

PostgreSQL: Learn about the open-source RDBMS' advanced capabilities, core components, common commands and functions, and general DBA tasks.

AI Automation Essentials. Check out the latest Refcard on all things AI automation, including model training, data security, and more.

Related

  • Java, Spring Boot, and MongoDB: Performance Analysis and Improvements
  • Introduction to Spring Data JPA — Part 4 Bidirectional One-to-Many Relations
  • Introduction to Spring Data JPA, Part 3: Unidirectional One to Many Relations
  • MicroProfile: What You Need to Know

Trending

  • Navigating the AI Renaissance: Practical Insights and Pioneering Use Cases
  • Implementation Best Practices: Microservice API With Spring Boot
  • Scaling Java Microservices to Extreme Performance Using NCache
  • Long Tests: Saving All App’s Debug Logs and Writing Your Own Logs
  1. DZone
  2. Software Design and Architecture
  3. Microservices
  4. Circuit Breaker Pattern With Netflix-Hystrix: Java

Circuit Breaker Pattern With Netflix-Hystrix: Java

This article covers what the circuit breaker pattern is, what it does, and how to use the library as an implementation of the circuit breaker pattern.

By 
Upanshu Chaudhary user avatar
Upanshu Chaudhary
·
Feb. 09, 22 · Code Snippet
Like (5)
Save
Tweet
Share
5.0K Views

Join the DZone community and get the full member experience.

Join For Free

If you are familiar with the circuit breaker pattern then you may have heard about Netflix-hystrix. But, before diving into our topic, let's make sure we have an understanding of what circuit breaker is and how Netflix-hystrix is implementing this pattern.

What Problem Are We Trying to Solve?

When in a distributed environment, our service may interact with other services or applications via remote call and these external services may be unavailable anytime for several reasons such as downtime, unavailable resources, etc. This unavailability due to the fault in an external application will also affect our perfectly working application. 

For example, we know that in a microservice architecture number of services interact with each other, so if we have a service A whose sole purpose is to process and pass on data to service B via a rest call then, ideally, it will be assumed that service B will always be available for service A to send data and return a proper response. But, we don’t live in an ideal world, do we? It may very possibly happen that service B goes down due to some reason like pod failure, resource issues, etc, and when its service A’s turn to send data, it finds that the endpoint it’s trying to send data to is not available anymore. So, what does it do with that piece of data and the data coming after that? 

Well, it isn't service A’s fault that service B is not available, correct? It’s doing its best, but it also does not know what to do now. The solution is to make service A fault-tolerant. We want the service to handle the failures gracefully and take action on the failure, which won’t affect the working of the service itself. Irrespective of what happens to service B, service A should not suffer, and it should keep on processing the data to pass on to service B. We can achieve this by using the circuit breaker pattern.

Circuit Breaker Pattern

The circuit breaker is a pattern that prevents our service from repeatedly retrying the operation that is failing and allows it to work continuously irrespective of what the fault is and how much time it can take to fix, therefore saving us CPU resources. This pattern also helps us monitor if the fault has been resolved and, if it is, it would again continue making calls through the original operation. You can find a more detailed and better explanation here in the Microsoft docs.

What Is Netflix-hystrix?

Hystrix logo.

In very simple terms, a circuit breaker is a pattern, and Netflix-hystrix is an implementation of this pattern. This library was designed to control the interactions between distributed services by adding latency tolerance and fault tolerance logic. Hystrix does this by isolating points of access between the services, stopping cascading failures across them, and providing fallback options -- all of which improve your system’s overall resiliency.

Features of Netflix-hystrix:

  • Give protection from and control over latency and failure from dependencies
  • Stop cascading failures in a complex distributed system
  • Fallback Mechanism
  • Enable near real-time monitoring, alerting, and operational control

You can get more detail about its works in their documentation here.

Let’s implement this ourselves for better understanding and use of netflix-hystrix with a Java Springboot Application.

Here, we have created a sample Hystrix Java service that does the following:

  • Exposes a GET endpoint that provides information about the service.
  • The service internally calls another endpoint to retrieve this information. This is again a rest call.
  • When we hit the service endpoint, the controller will execute this method.
 
   public List<ServiceInformation> showServiceInformation() {
        List<ServiceInformation> information = restTemplate.exchange(
                "http://localhost:8081", HttpMethod.GET, null, new
                        ParameterizedTypeReference<List<ServiceInformation>>(){}).getBody();
        for(ServiceInformation serviceInformation : information) {
            logger.trace(serviceInformation.getServiceName());
            logger.trace(serviceInformation.getMessage());
        }
        return information;
    }


Now, we can see that the method makes a rest call to another service, so we will only be able to receive information if the external service returns List<ServiceInformation>

But, if this rest endpoint is unavailable, we won't receive any data. This is where we can implement the circuit breaker pattern using the Netflix-hystrix.

The library provides us with a fallback method mechanism, which is a method that will be invoked in case the call to external service fails. The logic to the fallback method will depend on your use case; here, we are just going to log for demo purposes that the service failed and the application moved to a fallback method.

 
    @HystrixCommand(fallbackMethod = "defaultStatus")
    public List<ServiceInformation> showServiceInformation() {
        List<ServiceInformation> information = restTemplate.exchange(
                "http://localhost:8081", HttpMethod.GET, null, new
                        ParameterizedTypeReference<List<ServiceInformation>>(){}).getBody();
        for(ServiceInformation serviceInformation : information) {
            logger.trace(serviceInformation.getServiceName());
            logger.trace(serviceInformation.getMessage());
        }
        return information;
    }
    
      public List<ServiceInformation> defaultStatus() {
        logger.error("circuit-breaker-proxy is down, running fallback method");
        return Collections.emptyList();
    }


@HystrixCommand() enables us to provide the fallback method. The name of the fallback method is passed in the parameter of this annotation.

Here, the default status is the fallback method, and the name of this method is passed as a parameter in the @HystrixCommand annotation which will execute the fallback method if the call to external service fails. We can also have fallbacks for the fallback method.

The Annotation can have several more parameters depending on the property you want to provide. For now, we will just look at the fallback method. 

Hystrix is no longer in active development and is currently in maintenance mode. There are more alternatives to Hystrix such as Resilience4j, which we will cover in future blogs.

For more detailed implementation and understanding of the annotation check out the documentation here.

The link to the project is here.

Refer to the readme file to run the project and see how the library actually works. Fork/clone, and play around to have a better understanding.

Circuit Breaker Pattern microservice Java (programming language) Data (computing) application Fault (technology) Annotation workplace Fault tolerance

Opinions expressed by DZone contributors are their own.

Related

  • Java, Spring Boot, and MongoDB: Performance Analysis and Improvements
  • Introduction to Spring Data JPA — Part 4 Bidirectional One-to-Many Relations
  • Introduction to Spring Data JPA, Part 3: Unidirectional One to Many Relations
  • MicroProfile: What You Need to Know

Partner Resources


Comments

ABOUT US

  • About DZone
  • Send feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: