Proposition regarding the COUNT_BASED sliding window algorithm for CircuitBreaker
See original GitHub issueResilience4j 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’ [ _ _ _ _ _ _ _ _ _ _ ]
- _ _ _ _ _ _ _ _ _ F
- _ _ _ _ _ _ _ _ F F
- _ _ _ _ _ _ _ F F F
- _ _ _ _ _ _ F F F F
- _ _ _ _ _ F F F F F
- _ _ _ _ F F F F F F
- _ _ _ F F F F F F F
- _ _ F F F F F F F F
- _ F F F F F F F F F
- 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 [ _ _ _ _ _ _ _ _ _ _ ]
- _ _ _ _ _ _ _ _ _ F
- _ _ _ _ _ _ _ _ F F
- _ _ _ _ _ _ _ F F S
- _ _ _ _ _ _ F F S S
- _ _ _ _ _ F F S S S
- _ _ _ _ F F S S S S
- _ _ _ F F S S S S S
- _ _ F F S S S S S S
- _ F F S S S S S S S
- 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 ]
- S S S S S S S S S F
- S S S S S S S S F F
or
[ S S S S S S S S S S ]
- S S S S S S S S S F
- S S S S S S S S F S
- S S S S S S S F S S
- S S S S S S F S S S
- S S S S S F S S S S
- 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:
- Created a year ago
- Comments:5 (2 by maintainers)

Top Related StackOverflow Question
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
It solved the problem. Thanks!
BTW:
permittedNumberOfCallsInHalfOpenStateis considered asslidingWindowSizefor HALF_OPEN stateSo
minimumNumberOfCallsconfig property overwrites thepermittedNumberOfCallsInHalfOpenStatefor HALF_OPEN state and works in the same way likeslidingWindowSizeandminimumNumberOfCallsfor CLOSED state And it wasn`t obvious from documentation.In any way thanks for your answer. It helped