Resiliance4J
- Anand Nerurkar
- Mar 22, 2024
- 1 min read
Updated: Jun 11, 2024
Resilience4j is a lightweight fault tolerance library designed for functional programming. Resilience4j provides higher-order functions (decorators) to enhance any functional interface, lambda expression or method reference with a Circuit Breaker, Rate Limiter, Retry or Bulkhead. You can stack more than one decorator on any functional interface, lambda expression or method reference. The advantage is that you have the choice to select the decorators you need and nothing else.
Resilience4j 2 requires Java 17.
Resiliance4J pattern
==
In API Gateway, the burst limit represents the target maximum number of concurrent request submissions that API Gateway will fulfill
The burst limit defines the number of requests your API can handle concurrently. The rate limit defines the number of allowed requests per second. This is an implementation of the Token bucket implementation.
Let us assume below scenario
Service B is down or very slow to respond, in this case we can make use of resilance4j library
and implement below options
Retry
CircuitBreaker
Rate Limiter
BulkHead Circuit Breaker
code snippet- happy path
need to add resilance4j,aop,actuator dependency in pom.xml for serviceA,B
serviceB;
ServiceA:
Assume service A,B are working fine , in this case output would be
serviceB is called by ServiceA
Assume ServiceB is down, this is where it will cascade failure to service A , in such scenario, we will make use of ckt breaker pattern as below
serviceA:
enable management endpoit as below
application.yml
==
enable cktbreaker
==
we have defined 10 request in a given time, 50 % threshold- minmum 5 request to be failed, then cktbreaer fallback will kick in.
Retry
Rate Limiter
BulkHead
Comments