Resilience4j Circuit Breaker with Spring Boot

a man wearing a virtual reality headset
Reading Time: 2 minutes

Resilence4j

Resilience4j is a Java library that provides several resilience patterns for building fault-tolerant applications. It is designed to help developers write resilient, fault-tolerant, and scalable applications with minimal effort. Resilience4j is designed to be easy to use and integrate into the applications. It also provides a flexible configuration system that allows developers to customize the behavior of each resilience pattern to suit their specific needs.

What is Circuit Breaker

This pattern allows developers to build fault-tolerant applications by preventing cascading failures in distributed systems. It monitors the status of a remote system and “breaks the circuit” when it detects a failure, preventing further requests from being sent to the system until it has recovered.

Circuit Breaker Flow Diagram

How to implement Resilience4j

We need to add the dependency, and circuit breaker configuration to our service.

 Add the below dependency in the pom.xml.

<dependency>

    <groupId>io.github.resilience4j</groupId>

    <artifactId>resilience4j-spring-boot2</artifactId>

    <version>1.7.1</version>

</dependency>

Configuration: We will configure the circuit breaker through the application.yml using the below configuration.

resilience4j:

  circuitbreaker:

    instances:

      service_A:

        registerHealthIndicator: true

        eventConsumerBufferSize: 10

        failureRateThreshold: 50

        minimumNumberOfCalls: 5

        automaticTransitionFromOpenToHalfOpenEnabled: true

        waitDurationInOpenState: 5s

        permittedNumberOfCallsInHalfOpenState: 3

        slidingWindowSize: 10

        slidingWindowType: COUNT_BASED

Actuator Configuration: We will enable the circuit breaker actuator endpoint through the application.yml using the below configuration.

management:

  health:

    circuitbreakers:

      enabled: true

  endpoints:

    web:

      exposure:

        include: health

  endpoint:

    health:

      show-details: always

After configuring the circuit breaker, you can check the health endpoints from the browser by the URL “http://localhost:7070/actuator/health&#8221;. In upstate, we will get state CLOSED. Please check below API Response.

In Half-open state <Retry>, state would be HALF_OPEN

In Open state <Remote Api Down>, the state would be OPEN

Now we will move to implement the circuit breaker in Our Micro Service API Call.
We will use @CircuitBreaker annotation followed by the name of the circuit breaker and fallback method.
fallback method: From the fallback method we will return the default response when the state is HALF_OPEN or OPEN.

Note: Make sure the method annotated with @CircuitBreaker should not in the same class as the caller.

References

https://resilience4j.readme.io/docs/getting-started

Written by 

Deepak kumar is a Software Intern at Knoldus Inc. He has done Post Graduation from Ajay Kumar Garg Engineering College Ghaziabad. He has the knowledge of various programming languages. He is passionate about Java development and curious to learn Java Technologies. He is a quick learner, problem solver and always enjoy to help others. His hobbies playing Cricket and watching Hollywood movies.