question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Proposition regarding the COUNT_BASED sliding window algorithm for CircuitBreaker

See original GitHub issue

Resilience4j version: 1.7.0

Java version: 18.0.1

Resilience4j CircuitBreaker COUNT_BASED sliding window algorithm uses fixed circular array with fixed size. To trigger aggregation and calculate the Failure Rate that circular array should be full. So assume that we set up slidingWindowSize = 10 and failureRateThreshold: 20. That means the CircuitBreaker should make STATE_TRANSITION to OPEN_STATE when 2 requests from 10 must be failed. In practice after application start the circular array is empty and we may have 10 failed request from 10 to OPEN the CircuitBreaker with failure rate 100% despite the setting in 20%, because we need to fill up that array until it`s full to trigger aggregation. Lets mark failed requests as ‘F’ and succeeded as ‘S’ [ _ _ _ _ _ _ _ _ _ _ ]

  1. _ _ _ _ _ _ _ _ _ F
  2. _ _ _ _ _ _ _ _ F F
  3. _ _ _ _ _ _ _ F F F
  4. _ _ _ _ _ _ F F F F
  5. _ _ _ _ _ F F F F F
  6. _ _ _ _ F F F F F F
  7. _ _ _ F F F F F F F
  8. _ _ F F F F F F F F
  9. _ F F F F F F F F F
  10. F F F F F F F F F F After 10th request the CircuitBreaker triggers aggregation and makes transition to OPEN state with actual failure rate of 100% instead of 20%

Lets consider another example [ _ _ _ _ _ _ _ _ _ _ ]

  1. _ _ _ _ _ _ _ _ _ F
  2. _ _ _ _ _ _ _ _ F F
  3. _ _ _ _ _ _ _ F F S
  4. _ _ _ _ _ _ F F S S
  5. _ _ _ _ _ F F S S S
  6. _ _ _ _ F F S S S S
  7. _ _ _ F F S S S S S
  8. _ _ F F S S S S S S
  9. _ F F S S S S S S S
  10. F F S S S S S S S S After 10th request the CircuitBreaker triggers aggregation and makes transition to OPEN state with actual failure rate of 20% as it was set up, but in fact this is not something expected. The CircuitBreaker OPENs just after the series of successful requests.

I would propose to initialize the circular array with artificial SUCCESS partial aggregations from scratch. [ S S S S S S S S S S ]

In that case two first FAILED requests will reach 20% failure rate and cause OPEN the CircuitBreaker and do not wait until 100% failure rate to just fill up the array. [ S S S S S S S S S S ]

  1. S S S S S S S S S F
  2. S S S S S S S S F F

or

[ S S S S S S S S S S ]

  1. S S S S S S S S S F
  2. S S S S S S S S F S
  3. S S S S S S S F S S
  4. S S S S S S F S S S
  5. S S S S S F S S S S
  6. S S S S F S S S S F In that case after 6th request we will reach 20% failure rate and cause OPEN the CircuitBreaker and do not wait until 20% failure rate to just fill up the array.

What do you think, guys? Does it make sense?

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
zbykovskyicommented, Jul 22, 2022

So assume that we set up slidingWindowSize = 10 and failureRateThreshold: 20. That means the CircuitBreaker should make STATE_TRANSITION to OPEN_STATE when 20 requests from 10 must be failed.

20 requests was just typo. Sorry

That means the CircuitBreaker should make STATE_TRANSITION to OPEN_STATE when 2 requests from 10 must be failed. 2 is 20% from 10

0reactions
zbykovskyicommented, Jul 22, 2022

But you have to configure minimumNumberOfCalls

It solved the problem. Thanks!

BTW: permittedNumberOfCallsInHalfOpenState is considered as slidingWindowSize for HALF_OPEN state

static CircuitBreakerMetrics forHalfOpen(int permittedNumberOfCallsInHalfOpenState,   
            CircuitBreakerConfig circuitBreakerConfig, Clock clock) {    
            return new CircuitBreakerMetrics(permittedNumberOfCallsInHalfOpenState,  
               CircuitBreakerConfig.SlidingWindowType.COUNT_BASED, circuitBreakerConfig, clock);    
 }

private CircuitBreakerMetrics(int slidingWindowSize,
        CircuitBreakerConfig.SlidingWindowType slidingWindowType,
        CircuitBreakerConfig circuitBreakerConfig,
        Clock clock) {
        if (slidingWindowType == CircuitBreakerConfig.SlidingWindowType.COUNT_BASED) {
            this.metrics = new FixedSizeSlidingWindowMetrics(slidingWindowSize);
            this.minimumNumberOfCalls = Math
                .min(circuitBreakerConfig.getMinimumNumberOfCalls(), slidingWindowSize);
        }
  ....
}

So minimumNumberOfCalls config property overwrites the permittedNumberOfCallsInHalfOpenState for HALF_OPEN state and works in the same way like slidingWindowSize and minimumNumberOfCalls for CLOSED state And it wasn`t obvious from documentation.

In any way thanks for your answer. It helped

Read more comments on GitHub >

github_iconTop Results From Across the Web

CircuitBreaker - resilience4j
The CircuitBreaker uses a sliding window to store and aggregate the outcome of calls. You can choose between a count-based sliding window and...
Read more >
How to Use Resilience4j to Implement Circuit Breaker?
In this tutorial, you will implement the circuit breaker in a product search ... sliding window type: count-based sliding window size: 10.
Read more >
Class CircuitBreakerConfig.Builder - javadoc.io
Configures the minimum number of calls which are required (per sliding window period) before the CircuitBreaker can calculate the error rate.
Read more >
Circuit breaker pattern simplified. Why do you need one in ...
The whole scenario is a real-life analogy of a circuit breaker. ... You can choose between a count-based sliding window and a time-based...
Read more >
Difference between sliding window size and minimum number ...
Let's assume you have minimumNumberOfCalls=3, slidingWindowSize = 10 and slidingWindowType = COUNT_BASED: That means the CircuitBreaker is ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found