![]() |
VOOZH | about |
Microservice architectures offer numerous advantages such as scalability, flexibility, and resilience. They introduce the complexities, particularly around inter-service communication. One of the common issues in a microservice ecosystem is the risk of cascading failures when one service fails then potentially bringing down the entire system. This is where the Circuit Breaker pattern comes into play.
The Circuit Breaker pattern can be designed pattern used to detect failures and encapsulate the logic of preventing the failure from constantly recurring, during maintenance, temporary external system failures, or unexpected system difficulties. By using Circuit Breakers, we can prevent the application from performing the operations that are likely to fail.
The Circuit Breaker is the mechanism that prevents the application from repeatedly trying to execute the operation that is likely to fail the application. It monitors the number of recent failures and when the threshold is reached, breaks the circuit and stops the operations for the predetermined period. To recover, it gives the failing service time.
Circuit Breakers can be essential in the microservices architecture to prevent the cascading the failures and improve the system resilience. In context of the Spring Boot, two popular tools for implementing the Circuit Breakers are Hystrix and Resilience4j.
Hystrix is the latency and fault tolerance library developed by the Netflix. While its widely used, it's worth nothing that Hystrix is no longer actively maintained. However, many existing the projects still use it.
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>Add the @EnableHystrix annotation to the main application class.
Use the @HystrixCommand annotation to wrap the methods you want to protect with the Circuit Breaker.
@HystrixCommand(fallbackMethod = "fallback")
public String callExternalService() {
return restTemplate.getForObject("http://external-service/api/resource", String.class);
}Add the below configuration in the application.yml:
hystrix:
command:
default:
execution:
isolation:
thread:
timeoutInMilliseconds: 1000Resilience4j is the lightweight, easy to use the fault tolerance library designed for the Java 8 and functional programming. It can be considered the modern alternative to Hystrix.
<dependency>
<groupId>io.github.resilience4j</groupId>
<artifactId>resilience4j-spring-boot2</artifactId>
<version>1.7.1</version>
</dependency>
<dependency>
<groupId>io.github.resilience4j</groupId>
<artifactId>resilience4j-circuitbreaker</artifactId>
<version>1.7.1</version>
</dependency>Resilience4j is auto configuration, so we dont need to add any specific annotation to enable it.
We can add the following configuration to the application.yml file.
resilience4j.circuitbreaker:
instances:
backendA:
registerHealthIndicator: true
slidingWindowSize: 10
minimumNumberOfCalls: 5
failureRateThreshold: 50
waitDurationInOpenState: 10000
permittedNumberOfCallsInHalfOpenState: 3Annotate the service with the @CircuitBreaker.
@CircuitBreaker(name = "backendA", fallbackMethod = "fallback")
public String callExternalService() {
return restTemplate.getForObject("http://external-service/api/resource", String.class);
}Both the Hystrix and resilience4j offers the monitoring capabilites. For the Resilience4j, we can use the Spring Boot Actuator to expose the metrics.
Add the below dependency:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>Add the below configuration to the application.yml.
management:
endpoints:
web:
exposure:
include: '*'We can access the metrics at the /actuator/metrics
Implementing the Circuit Breakers in the Spring Boot application is straightforward with the help of the libraries like Resilience4j. Circuit Breakers enhances the resilience of the microservices by preventing the cascading failures and giving the failing services time to recover.