This website uses cookies to ensure you get the best experience on our website.

Skip to content
LOGO
  • Company

    About IGNEK

    Learn about our story and our mission.

    Careers

    We're hiring!

    We always looking for talented peoples. Join our team!

    Blogs

    The latest industry news, updates and info.

    Case Studies

    Take a closer look at the projects we've delivered

    Webinar

    Watch our latest organized webinar

    Get in touch with our HR team

    • hr@ignek.com
    • +91 93284 95160
    • Ahmedabad, Gujarat, India – 382470
  • Services

    Enterprise Portal Development

    Custom Enterprise Portal Development for flawless Solutions

    Dedicated Development Team

    Expand Teams, Optimize Development with Our Expertise

    Back-end Development

    Empowering your digital presence with robust backend development expertise

    Front-end Development

    Crafting engaging user experiences through expert frontend development

    Digital Transformation

    Embrace digital transformation by modernizing process

    SaaS Development

    Transform your business with SaaS Innovative Solutions

    Technologies

    Liferay Services

    Development & Customization

    Theme

    Upgradation

    Migration

    Proof of Concept

    Performance Tunning

    Architecture

    Support & Maintenance

    E-commerce

    Expert Advice

    React Services

    Enterprise Development

    Custom Application

    Single Page Application

    Architecture

    API Integration

    Migration

    Consulting

    Maintenance & Support

    Spring Boot Services

    Web Service

    MVC Web Development

    API Integration

    Security

    Migration

    Microservices

    Support & Maintenance

    AEM Services

    Consulting Service

    Site Development

    Migration & Upgradation

    Integration

    Maintenance & Support

    Custom Solutions

    Content Management

    Team Augementation

  • Solution

    Ready for digital excellence

    In our rapidly evolving world, businesses are always on the lookout for fresh ways to improve their operations and connect with their audiences.

    Contact US

    Solutions

    SaaS : Software As A Service

    Transforming industries through cutting edge SaaS solutions.

    Customer Portal : Boost Business Productivity

    Streamline operations and enhance productivity with our Customer Portal solution.

    Liferay Based Intranet Portal

    Internal portal for company communication, collaboration and resources

    Integration

    Matomo Integration with Liferay

    Integrate Matomo to enable user interactions seamlessly.

    Microsoft Teams integration with Liferay

    Integrate to enable seamless collaboration, messaging within your portal.

    Jira Integration With Liferay

    Optimizes business operations by integrating with Jira.

  • Hire Us

    Liferay

    Get expert Liferay developers for seamless portal solutions.

    React JS

    Get expert ReactJS developers for dynamic web solutions.

    Web Developer

    Get custom web solutions from IGNEK's expert developers.

    Spring Boot

    Get top-notch Spring Boot developers for your project success!

    Healthcare

    Get top Healthcare Developers for cutting-edge medical software.

    MERN Stack

    Get expert MERN developers from IGNEK for custom, innovative project solutions.

    Why Hire Developers From IGNEK ?

    • Certified Developer
    • Transparent Communication
    • Flexible Hiring Modals
    • Fully Signed NDA
    • Cost Effective
    • Easy Exit Policy

    Get in touch with our Sales team

    • sales@ignek.com
    • +91 6351576580
    • info@ignek.com
    • Ahmedabad, Gujarat, India – 382470
SCHEDULE CALL
  • COMPANY
    • About
    • Case Studies
    • Blogs
    • Career
    • Webinar
  • SERVICES
    • Enterprise Portal Development
    • Dedicated Development Team
    • Back-end Development
    • Front-end Development
    • Digital Transformation
    • SaaS Development
  • SOLUTION
    • Customer Portal:  Boost Business Productivity
    • SaaS : Software as a Service
    • Liferay Based Employee Intranet Portal
  • TECHNOLOGY
    • Liferay Services
      • Liferay Development and Customization
      • Liferay Theme Development Service
      • Liferay DXP 7.4 Upgrade
      • Liferay Migration
      • Liferay Proof Of Concept
      • Liferay Performance Tuning
      • Liferay Architecture Design Service
      • Liferay Support And Maintenance Service
      • Liferay Ecommerce Development
      • Liferay Expert Advice
    • ReactJS Services
      • ReactJS Enterprise Application Development
      • ReactJS Custom Application Development
      • ReactJS Single Page Application Development (SPA)
      • ReactJS Applications Architecture
      • ReactJS API Integration
      • ReactJS Migration
      • ReactJS Consulting
      • ReactJS Maintenance and Support
    • Spring Boot Services
      • Spring Boot Web Development Service
      • Spring MVC Web Development
      • Spring Boot API Integration Service
      • Spring Boot Security
      • Spring Boot Migration Service
      • Spring Boot Microservices
      • Spring Boot Support & Maintenance Service
    • AEM Development Services
      • AEM Consulting Services
      • AEM Site Development
      • AEM Migration & Upgradation
      • AEM Integration Services
      • AEM Maintenance & Support
      • AEM Content Management
      • Custom AEM Solutions
      • AEM Team Augmentation
  • INTEGRATION
  • HIRE US
    • Hire Liferay Developer
    • Hire ReactJS Developer
    • Hire Spring Boot Developer
    • Hire Healthcare Developer
    • Hire Web Developer
    • Hire MERN Stack Developer
  • CONTACT US
  • Company

    About IGNEK

    Careers

    We're hiring!

    Blogs

    Case Studies

    Webinar

    Get in touch with our HR team

    • hr@ignek.com
    • +91 93284 95160
    • Ahmedabad, Gujarat, India – 382470
  • Services

    Enterprise Portal Development

    Dedicated Development Team

    Back-end Development

    Front-end Development

    Digital Transformation

    SaaS Development

    Technologies

    Liferay Liferay Services
    • Development & Customization
    • Theme
    • Upgradation
    • Migration
    • Proof of Concept
    • Performance Tuning
    • Architecture
    • Support & Maintenance
    • E-commerce
    • Expert Advice
    React React Services
    • Enterprise Development
    • Custom Application
    • Single Page Application
    • Architecture
    • API Integration
    • Migration
    • Consulting
    • Maintenance & Support
    Spring Boot Spring Boot Services
    • Web Service
    • MVC Web Development
    • API Integration
    • Security
    • Migration
    • Microservices
    • Support & Maintenance
    AEM AEM Services
    • Consulting Service
    • Site Development
    • Migration & Upgradation
    • Integration
    • Maintenance & Support
    • Custom Solutions
    • Content Management
    • Team Augmentation
  • Solution

    Ready for digital excellence

    In our rapidly evolving world, businesses are always on the lookout for fresh ways to improve their operations and connect with their audiences.

    Contact US

    Solutions

    SaaS : Software As A Service

    Customer Portal : Boost Business Productivity

    Liferay Based Intranet Portal

    Integration

    Matomo Integration with Liferay

    Microsoft Teams integration with Liferay

    Jira Integration With Liferay

  • Hire Us

    Liferay

    React JS

    Web Developer

    Spring Boot

    Healthcare

    MERN Stack

    Why Hire Developers From IGNEK ?

    • Certified Developer
    • Transparent Communication
    • Flexible Hiring Modals
    • Fully Signed NDA
    • Cost Effective
    • Easy Exit Policy

    Get in touch with our Sales team

    • sales@ignek.com
    • +91 6351576580
    • IGNEK
    • Ahmedabad, Gujarat, India – 382470
  • SCHEDULE CALL

Building Resilient Spring Boot Microservices with Circuit Breaker Patterns using Resilience4j

  • Spring Boot
  • August 30, 2024

Share On :

Introduction :

In the world of microservices, reliability and resilience are paramount. Microservices often depend on other services to function, and these dependencies can lead to cascading failures if one service goes down or becomes slow. To mitigate these risks, the Circuit Breaker pattern is widely used. In this blog, we’ll explore how to implement this pattern in Spring Boot microservices using Resilience4j.

Prerequisites :
  • JDK 11 or later
  • Maven or Gradle
  • Spring Boot Development Environment (here IntelliJ IDEA)
What is the Circuit Breaker Pattern :

The Circuit Breaker pattern is inspired by electrical circuit breakers that prevent overloads by breaking the circuit. Similarly, in software, a circuit breaker prevents a service from making requests that are likely to fail. Instead of allowing a failing service to affect the entire system, the circuit breaker “opens” to stop requests and returns a fallback response or error message.

The circuit breaker has three states :

Closed : Requests pass through normally., both services which are interacting are up and running, circuit breaker is CLOSED. If the number of failures crosses a threshold, the circuit breaker transitions to the Open state.

Open : Requests are not allowed, and a fallback response is returned. After a timeout period, the circuit breaker transitions to the Half-Open state. Calling another micro service will fail so an exception will be returned. That means, the flow is interrupted.

Half-Open : A few requests are allowed to pass through. If these requests succeed, the circuit breaker returns to the Closed state. If they fail, the circuit breaker returns to the Open state. In this state, only a LIMITED number of remote API calls are allowed to pass through. If the failing calls count is greater than this limited number, breaker turns again into OPEN state. Otherwise it is CLOSED.

Circuit Breaker pattern
Why Resilience4j :

Resilience4j is a lightweight, easy-to-use library for implementing various fault tolerance patterns, including circuit breakers, in Java and Spring Boot applications. Unlike other libraries, Resilience4j is designed for functional programming and provides greater control over how these patterns are implemented.

Setting Up  Spring Boot Application : 

Create 2 Microservices.I’m going to implement a simple inter service communication scenario using two services called course-service and student-service.

Scenario :

I want to retrieve student details along with their enrolled course details using two microservices, course-service and student-service

student-service : This service will manage student-related data, such as student profiles and their enrollment details in courses. It will communicate with course-service to fetch details about the courses in which a student is enrolled.

course-service : This service will handle all the course-related data, such as course descriptions, instructors, and status.

microservices_scenario

Step 1: Add Resilience4j Dependencies

Add the following dependencies to your service: Here I added dependency in student-services: 

				
					<dependency>
   <groupId>org.springframework.cloud</groupId>
   <artifactId>spring-cloud-starter-circuitbreaker-resilience4j</artifactId>
</dependency>


				
			

Step 2: Configure Application Properties

configure your circuit breaker in application.properties: 

				
					#Resilinece4j Properties
spring.cloud.circuitbreaker.resilience4j.cb.Course-Service.registerHealthIndicator=true
spring.cloud.circuitbreaker.resilience4j.cb.Course-Service.failureRateThreshold=50
spring.cloud.circuitbreaker.resilience4j.cb.Course-Service.waitDurationInOpenState=10000
spring.cloud.circuitbreaker.resilience4j.cb.Course-Service.permittedNumberOfCallsInHalfOpenState=10
spring.cloud.circuitbreaker.resilience4j.cb.Course-Service.slidingWindowSize=100
spring.cloud.circuitbreaker.resilience4j.cb.Course-Service.minimumNumberOfCalls=5
spring.cloud.circuitbreaker.resilience4j.cb.Course-Service.automaticTransitionFromOpenToHalfOpenEnabled=100


				
			

Here’s a breakdown of the configuration :

  • slidingWindowSize : The number of calls to evaluate when determining the failure rate.
  • minimumNumberOfCalls : The minimum number of calls required before the circuit breaker can calculate a failure rate.
  • failureRateThreshold : The percentage of calls that must fail to trigger the circuit breaker.
  • waitDurationInOpenState : The time the circuit breaker will remain open before transitioning to half-open.
  • permittedNumberOfCallsInHalfOpenState : The number of calls allowed when the circuit breaker is in a half-open state.
  • automaticTransitionFromOpenToHalfOpenEnabled : Automatically transition from open to half-open state.

Step 3: Implement Circuit Breaker in Your Service

Apply the circuit breaker to a method in your service layer using the @CircuitBreaker annotation:

				
					@CircuitBreaker(name = "course", fallbackMethod = "fallbackMethod")
public StudentCourseDetailsResponse getStudentWithTheirCourseDetail(Long id) {
   StudentCourseDetailsResponse studentCourseDetailsResponse=new StudentCourseDetailsResponse();
   Course course = courseServiceFeignClient.getCourseDetails(id);
   Student student=studentRepo.findById(id).orElseThrow(null);
   studentCourseDetailsResponse.setCourse(course);
   studentCourseDetailsResponse.setStudent(student);
   return studentCourseDetailsResponse;
}

public StudentCourseDetailsResponse fallbackMethod(Long id, Throwable throwable) {
   StudentCourseDetailsResponse studentCourseDetailsResponse=new StudentCourseDetailsResponse();
   return studentCourseDetailsResponse;
}


				
			
  • The @CircuitBreaker annotation applies the circuit breaker to the callExternalService method.
  • The name attribute refers to the circuit breaker instance configured in application.propreties.
  • The fallbackMethod specifies the method to call when the circuit breaker is open.

Step 4: Define  controller.

Define the endpoint in the controller that will handle the response. 

				
					@GetMapping(value = "/get-student-course/{id}")
public ResponseEntity<Response> getStudentWithTheirCourseDetail(@PathVariable Long id) {
   try {
       StudentCourseDetailsResponse user = userServices.getStudentWithTheirCourseDetail(id);
       return new ResponseEntity<>(new Response(user,"student with course detail retrive", Boolean.TRUE), HttpStatus.OK);
   } catch (Exception e) {
       return new ResponseEntity<>(new Response("Error retrive details", Boolean.FALSE),
               HttpStatus.INTERNAL_SERVER_ERROR);
   }
}


				
			

Here it  will handle the request to retrieve student details along with their enrolled course details. This endpoint will utilize the Feign client to communicate with course-service. If course-service is unavailable, the fallback method defined earlier will be invoked, providing a default or error response.

Step 5: Monitoring and Metrics:

Resilience4j integrates seamlessly with Spring Boot Actuator. You can monitor circuit breakers by enabling Actuator:

				
					<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
				
			

Now, access metrics and health indicators via Actuator endpoints, such as /actuator/metrics and /actuator/health.

Step 6: Testing the Circuit Breaker

To test the circuit breaker, trigger failures in your external service. For example, shut down the external service, and observe how the circuit breaker transitions from closed to open, and then to half-open

Conclusion:

By implementing the Circuit Breaker pattern with Resilience4j in your Spring Boot microservices, you can build resilient and fault-tolerant systems. This approach ensures that a failure in one service doesn’t cascade and cause widespread issues across your entire application. With the proper configuration and monitoring in place, you can maintain high availability and reliability in your microservices architecture.There are other resilience patterns offered by Resilience4j, such as Rate Limiter, Retry, and Bulkhead.

Explore Our Services

Discover how we can help your business thrive, whether you’re running a small startup, an SME, or a large enterprise. We’re here to understand your unique needs and goals, offering the expertise and resources to support your journey to success.

Stay informed about our Spring Boot services and updates by subscribing to our newsletter—just fill in the details below to subscribe.

Loading
Loading...

Related Blogs

October 17, 2023
Enhance program organization and structure through Spring Boot Custom Annotations.
Custom Annotations in Spring Boot
June 6, 2024
CRUD Restful APIs with Spring Boot
CRUD Restful APIs with Spring Boot
June 7, 2024
Spring Boot With Swagger Integration
Spring Boot With Swagger Integration
Loading...

Featured Projects Portfolios

October 6, 2023
Dynamic E-commerce Platform_for Sales, Rentals and Inventory Management
Dynamic E-commerce Platform for sales, Rentals and Inventory Management

Digital Solutions for Your Business with IGNEK

4.9

5.0

5.0

5.0

LOGO

Making the world a better place through constructing elegant hierarchies

COMPANY

  • About
  • Career
  • Case Study
  • Blogs

SERVICES

  • Enterprise Portal Development
  • Dedicated Development Team
  • Back-end Development
  • Front-end Development
  • Digital Transformation
  • SaaS Development

HIRE US

  • Liferay
  • Spring Boot
  • ReactJS
  • Healthcare
  • MERN Stack
  • AEM

CONTACT

  • info@ignek.com
  • info@ignek.com
SALES
  • sales@ignek.com
  • (+91) 635 157 6580
CAREER
  • hr@ignek.com
  • (+91) 932 849 5160
  • E 910-912, Ganesh Glory 11, Jagatpur Road, SG Highway, Ahmedabad, Gujarat - 382470

© 2018-2025 IGNEK, Inc. All rights reserved

Linkedin Facebook X-twitter Instagram