Imagine you have a cool app on your phone that lets you buy things online. When you make a purchase, the app needs to communicate with a payment service to process your payment and generate an invoice. However, sometimes things don’t happen as expected. The payment service might be down or taking too long to respond due to some technical issues.
To make sure your shopping experience doesn’t get ruined by these hiccups, Resilience4j comes to the rescue. It’s like having a superhero that protects the app and keeps things running smoothly even when the payment service is acting up. Resilience4j is a fault tolerance library inspired by Hystrix. It is lightweight and easy to use and integrate into the application. It is designed to help developers write resilient and fault-tolerant applications.
In a microservice architecture, we must have fault tolerance because services are loosely coupled so if one service is down we must handle that scenario. Also sometimes we have to limit the number of calls to some APIs and retry calling another service after some time. Resilience4j provides these features. Let’s learn how to implement them in our project.
To start, let’s add some dependencies to our Spring Boot project;
<dependency> <groupId>io.github.resilience4j</groupId> <artifactId>resilience4j-spring-boot2</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-aop</artifactId> </dependency>
We will add the spring-boot-starter-actuator to monitor the application state through endpoints;
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency>
Related read: 19 Point Checklist: Starting a New Project in Spring Boot
A circuit breaker allows developers to build fault-tolerant applications by preventing cascading failures in applications. It breaks the circuit when it detects the system failures. It helps in preventing the call to the microservice which may be down or giving timeouts. We can also track circuit breaker status using actuator endpoints. The circuit breaker has 3 states;
We have to add some properties in the application.properties file;
resilience4j.circuitbreaker: instances: generateInvoice: registerHealthIndicator: true slidingWindowSize: 60 slidingWindowType: TIME_BASED permittedNumberOfCallsInHalfOpenState: 3 minimumNumberOfCalls: 10 waitDurationInOpenState: 5s slowCallRateThreshold: 100 slowCallDurationThreshold: 2000 failureRateThreshold: 50 @GetMapping("/generate-invoice") @CircuitBreaker(name="generateInvoice",fallbackMethod = "onGenerateInvoiceFailed") public ResponseEntity<Map<String,Object>> sampleApi() { ResponseEntity<Map<String,Object>> paymentDetails = new RestTemplate().getForEntity("http://localhost:8080/get-payment-details", Map.class); return invoiceService.generateInvoice(paymentDetails.getBody()); } private Map<String,Object> onGenerateInvoiceFailed() { Map<String,Object> res = new HashMap<>(); res.put("message", "Payment Service Down"); res.put("status", "ServiceDown"); }
The rate limiter is used to control the rate of incoming requests to a service. When the rate limit is reached then incoming requests are rejected or delayed until the rate drops below the limit. It can be configured by setting maximum requests in a given time.
resilience4j.ratelimiter: instances: sendMessages: limitRefreshPeriod: 1s limitForPeriod: 5 timeoutDuration: 3s registerHealthIndicator: true @PostMapping("/send-messages") @RateLimiter(name="sendMessages") public String sendMessage(@RequestParam String message) { return message; }
We can also add a fallback method so that we can return the appropriate response once the limit exceeds.
Bulkhead allows you to limit the number of concurrent calls to services, preventing system overloading. It offers 2 bulkheads one is a thread pool and another one is a semaphore to control resource allocation.
Semaphore is the default bulkhead. It uses the same use request thread and will not create a new thread. Where the thread pool bulkhead creates the new thread.
resilience4j.bulkhead: instances: service-name: maxConcurrentCalls: 2 maxWaitDuration: 5s @GetMapping("/country-names") @Bulkhead(name="countryNames") public ResponseEntity<List<String>> getCountryNames() { return coutryService.getNames(); }
A retry pattern is used to retry calling service after some time. We can also set how many times we can call the service along with the time in between the calls.
resilience4j.retry: instances: retryTest: maxAttempts: 3 waitDuration: 1s @GetMapping("/country-names") @Retry(name="retryTest") public String getCountryNames() { return countyService.getNames(); }
You can also consider alternatives such as Hystrix, the Netflix OSS (Open Source Software) Stack, Spring Cloud Circuit Breaker, Sentinel, and Akka.
These are just a few alternatives to Resilience4j, and the choice depends on your specific requirements, technology stack, and preferences. It’s essential to evaluate each alternative based on factors such as community support, maturity, active development, compatibility, and integration with your existing infrastructure and frameworks.
In this blog, we have learned how to use a circuit breaker when another service is down or taking a long time to respond and went through the states used in circuit breakers. Rate limiter which serves as our traffic controller, ensuring we don’t overwhelm services with a barrage of requests.
Additionally, we’ve delved into the concept of bulkheads, acting as sturdy partitions that prevent system overload by managing concurrent requests. Lastly, we’ve grasped the power of retrying requests, a resilient technique that gives our applications a second chance, with well-defined intervals, when an initial attempt fails.
Mindbowser played a crucial role in helping us bring everything together into a unified, cohesive product. Their commitment to industry-standard coding practices made an enormous difference, allowing developers to seamlessly transition in and out of the project without any confusion....
CEO, MarketsAI
I'm thrilled to be partnering with Mindbowser on our journey with TravelRite. The collaboration has been exceptional, and I’m truly grateful for the dedication and expertise the team has brought to the development process. Their commitment to our mission is...
Founder & CEO, TravelRite
The Mindbowser team's professionalism consistently impressed me. Their commitment to quality shone through in every aspect of the project. They truly went the extra mile, ensuring they understood our needs perfectly and were always willing to invest the time to...
CTO, New Day Therapeutics
I collaborated with Mindbowser for several years on a complex SaaS platform project. They took over a partially completed project and successfully transformed it into a fully functional and robust platform. Throughout the entire process, the quality of their work...
President, E.B. Carlson
Mindbowser and team are professional, talented and very responsive. They got us through a challenging situation with our IOT product successfully. They will be our go to dev team going forward.
Founder, Cascada
Amazing team to work with. Very responsive and very skilled in both front and backend engineering. Looking forward to our next project together.
Co-Founder, Emerge
The team is great to work with. Very professional, on task, and efficient.
Founder, PeriopMD
I can not express enough how pleased we are with the whole team. From the first call and meeting, they took our vision and ran with it. Communication was easy and everyone was flexible to our schedule. I’m excited to...
Founder, Seeke
Mindbowser has truly been foundational in my journey from concept to design and onto that final launch phase.
CEO, KickSnap
We had very close go live timeline and Mindbowser team got us live a month before.
CEO, BuyNow WorldWide
If you want a team of great developers, I recommend them for the next project.
Founder, Teach Reach
Mindbowser built both iOS and Android apps for Mindworks, that have stood the test of time. 5 years later they still function quite beautifully. Their team always met their objectives and I'm very happy with the end result. Thank you!
Founder, Mindworks
Mindbowser has delivered a much better quality product than our previous tech vendors. Our product is stable and passed Well Architected Framework Review from AWS.
CEO, PurpleAnt
I am happy to share that we got USD 10k in cloud credits courtesy of our friends at Mindbowser. Thank you Pravin and Ayush, this means a lot to us.
CTO, Shortlist
Mindbowser is one of the reasons that our app is successful. These guys have been a great team.
Founder & CEO, MangoMirror
Kudos for all your hard work and diligence on the Telehealth platform project. You made it possible.
CEO, ThriveHealth
Mindbowser helped us build an awesome iOS app to bring balance to people’s lives.
CEO, SMILINGMIND
They were a very responsive team! Extremely easy to communicate and work with!
Founder & CEO, TotTech
We’ve had very little-to-no hiccups at all—it’s been a really pleasurable experience.
Co-Founder, TEAM8s
Mindbowser was very helpful with explaining the development process and started quickly on the project.
Executive Director of Product Development, Innovation Lab
The greatest benefit we got from Mindbowser is the expertise. Their team has developed apps in all different industries with all types of social proofs.
Co-Founder, Vesica
Mindbowser is professional, efficient and thorough.
Consultant, XPRIZE
Very committed, they create beautiful apps and are very benevolent. They have brilliant Ideas.
Founder, S.T.A.R.S of Wellness
Mindbowser was great; they listened to us a lot and helped us hone in on the actual idea of the app. They had put together fantastic wireframes for us.
Co-Founder, Flat Earth
Ayush was responsive and paired me with the best team member possible, to complete my complex vision and project. Could not be happier.
Founder, Child Life On Call
The team from Mindbowser stayed on task, asked the right questions, and completed the required tasks in a timely fashion! Strong work team!
CEO, SDOH2Health LLC
Mindbowser was easy to work with and hit the ground running, immediately feeling like part of our team.
CEO, Stealth Startup
Mindbowser was an excellent partner in developing my fitness app. They were patient, attentive, & understood my business needs. The end product exceeded my expectations. Thrilled to share it globally.
Owner, Phalanx
Mindbowser's expertise in tech, process & mobile development made them our choice for our app. The team was dedicated to the process & delivered high-quality features on time. They also gave valuable industry advice. Highly recommend them for app development...
Co-Founder, Fox&Fork