Circuit Breaker with Resilience4j

suzan balkaya
Sahibinden Technology
3 min readJan 19, 2023

We have activated a microservice for sahibinden.com, which can moderate classifieds with AI. This microservice is one of the first microservices created by the sahibinden.com microservice migration, and therefore, some problems to be resolved were encountered for the first time.

In this article, we will discuss why the Circuit Breaker Pattern was needed and how we used it.

Integration Resilience4j Circuit Breaker With Spring Boot

Topics Covered:
1. Problem
2. Solution
- What is Circuit Breaker and Resilience4j?
- How a Circuit Breaker works?
- How to implement Resilience4j?

The Problem

Imagine that you are developing a classified system. Let’s assume that this system receives 300 requests per minute. In order for these classifieds to be published and they must pass some checks and it should be checked whether they are suitable for publication. Classifieds validations of the controls can be done by another API.
The service we went to crashed or cases such as late response of the service It can cause problems that affect the whole system. To an unresponsive service in case of many requests it can cause excessive resource consumption and blocking of threads.
We can guess that these problems we are experiencing may cause our service to be unresponsive and extend our time to publish the Classifieds.

The Solution

— What is Circuit Breaker and Resilience4j

Our API may start getting errors from the different service it requests. Until this service is fixed, we should not send dropped requests.
We can use a circuit breaker for this. It should not be opened until we are sure that the service is running smoothly. This approach is called the circuit breaker pattern.
In summary, when errors or delays in a system exceed a certain threshold, it is the disconnection for the specified time so that our system is not blocked and the called system recovers itself.
Resilience4j is a library for integrating the circuit breaker pattern.
There are several open source libraries that can be used to integrate the circuit breaker such as Resilience4j, Hystrix , Failsafe.

— How a Circuit Breaker works?

Circuit Breakers have three states: Closed, Open, Half-Open.
If an error is received below the specified threshold from a service or a successful response is received for all requests, the circuit breaker is in the closed state.

If the errors received from the service exceed the specified threshold, the circuit breaker opens.

The circuit stays open for a predetermined time, after which time it switches to a half-open state, allowing some of the traffic.

— How to implement Resilience4j

Firstly, we need to make the dependency setup.

<dependency>
<groupId>io.github.resilience4j</groupId>
<artifactId>resilience4j-spring-boot2</artifactId>
<version>2.0.2</version>
</dependency>
resilience4j.circuitbreaker:
configs:
default:
slidingWindowSize: 100
permittedNumberOfCallsInHalfOpenState: 10
waitDurationInOpenState: 10000
failureRateThreshold: 60
eventConsumerBufferSize: 10
registerHealthIndicator: true
ignoreExceptions:
- BusinessExceptionClass

After the configurations are completed, we set the error message that we will return to default and for which services the circuit breaker will work is developed.
Make sure the method annotated with @CircuitBreaker should not be in same class of caller.

@CircuitBreaker(name = "sendClassifiedApprovalData", fallbackMethod = "sendClassifiedRequestCircuitBreakerFallback")
public List<String> sendClassifiedApprovalData(ClassifiedRequest classifiedRequest) {
return client.post(classifiedRequest).getBody();
}


private List<String> sendClassifiedRequestCircuitBreakerFallback(ClassifiedRequest classifiedRequest, Exception ex) throws Exception {
logger.error("Fallback logic");
throw ex;
}

That’s all …

--

--