Circuit Breaker Design
what are transient and non-transient failures?
Transient failures refer to temporary or short-lived issues in a system that typically resolve themselves after a certain period. These failures are often caused by temporary conditions such as network glitches, momentary resource unavailability, or other intermittent issues.
Non-transient failures refer to issues that are not temporary and do not automatically resolve themselves. These failures typically require explicit intervention to fix the problem. An example of a non-transient failure is when a database server is down and cannot be automatically recovered by the system.
cascading failures:
When a critical service goes down, it can have an impact on other services that rely on it, triggering additional failures in the related or dependent services.
Circuit Design Pattern:
This pattern is employed in cloud environment, it deals with the non-transient failures in the microservices.
In our microservices we can implement this pattern for fault tolerance /Resilience systems.
let's understand stages in circuit breaker.
Closed state: Requests are accepted, and responses are processed normally.
Open State: if the system failures exceed the threshold count, then circuit breaker transitions to the open state.
Half-Open: if there is time out in the open state which forwards the request to half-open state. It waits for certain time which we configure manually, if it not recoverable in that time then it goes to open state if it recoverable it goes to closed state.
In Circuit Breaker we will write a fallback logic to avoid the sending the requests to the failure system, we not only prevent unnecessary calls to a failing system but also enhance the overall resilience of our applications.
Circuit Breaker Implement in microservice.
package in.spring.rest;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import io.github.resilience4j.circuitbreaker.annotation.CircuitBreaker;
@RestController
public class MsgRestController {
@GetMapping("/")
@CircuitBreaker(name="app",fallbackMethod="getDb")
public String getMsg() {
System.out.println("Redis starting");
int i=10/0;
return "get from redis";
}
public String getDb(Throwable t) {
System.out.println("db started");
return "from db";
}
}
Configuartion file.
spring:
application.name: resilience4j-demo
management:
endpoints.web.exposure.include:
- '*'
endpoint.health.show-details: always
health.circuitbreakers.enabled: true
resilience4j.circuitbreaker:
configs:
default:
registerHealthIndicator: true
slidingWindowSize: 10
minimumNumberOfCalls: 5
permittedNumberOfCallsInHalfOpenState: 3
automaticTransitionFromOpenToHalfOpenEnabled: true
waitDurationInOpenState: 5s
failureRateThreshold: 50
eventConsumerBufferSize: 10