Search code examples
javaspring-cloudcircuit-breakerresilience4j

Spring Circuit Breaker - Resilience4j - how to configure?


I have a quick question regarding Spring Cloud Circuit Breaker with Resilience4J (not Resilience4J alone).

Both projects are pretty awesome. However, currently, we ended in the fallback too often. That means, when the 3rd party service is actually fine, we still end up going to the fallback.

This is probably my own issue, hence, I would like to ask a question for a particular configuration.

I would like to tell the current Configuration to do the following: The circuit breaker. (I am going to use good, bad, and half good/half bad states.

  • When state is good: After a configurable number (let's take 5 for example) failed requests (something is wrong on the 3rd party API), put to half good/half bad.

  • When state is half good/half bad: Once in half good/bad, if more than half (configurable) of the requests are success, put if back to good state.

  • When state is half good/half bad: If more than half of the requests are still failing, put to bad state.

  • When state is bad: After two consecutive good requests, put it back to half good half bad.

  • For the time out, a request should answer within 4 seconds. It is the SLA the 3rd party gave us. Above that, probably something is wrong.

  • For each failed request, a retry of 3 (configurable) times.

May I ask how to achieve that please in Spring Webflux?

Thank you


Solution

  • This is not possible:

    When state is bad: After two consecutive good requests, put it back to half good half bad.

    When the CircuitBreaker is open, it does not permit any calls.

    You can achieve this easily if you use resilience4j-spring-boot2 and resilience4j-reactor.

    publisher
    .transform(TimeLimiterOperator.of(timeLimiter))
    .transform(CircuitBreakerOperator.of(circuitBreaker))
    .transform(RetryOperator.of(retry))
    

    Our Spring Boot starter allows you to configure TimeLimiter, CircuitBreaker and Retry in your external configuration file. You can even just use Annotations on your methods. No need to add the Reactor operators manually.

        @TimeLimiter(name = "id")
        @CircuitBreaker(name = "id")
        @Retry(name = "id")
        public Flux<String> fluxSuccess() {
            return Flux.just("Hello", "World");
        }
    

    And our Spring Boot starter adds metrics ;)

    See: https://resilience4j.readme.io/docs/getting-started-3