BrunoBonacci / safely

Safely is a Clojure's circuit-breaker library for handling retries in an elegant declarative way.
https://cljdoc.org/d/com.brunobonacci/safely
Apache License 2.0
219 stars 9 forks source link

Query regarding default threshold value #8

Closed alza-bitz closed 3 months ago

alza-bitz commented 1 year ago

Hi Bruno,

Referring to the state machine for the circuit breaker here https://github.com/BrunoBonacci/safely/blob/master/README.md#circuit-breaker I was surprised to find that the default value for failure-threshold is 0.5?

What does it mean for this value to be less than 1, or even a non-integer value? My understanding of the circuit breaker pattern is that it keeps a state counter for the number of failed requests. This counter is then used in a rule for determining any state change from closed to open, by comparing the counter with the threshold.

So I'm struggling to understand this default value...

Thanks 🙏

BrunoBonacci commented 1 year ago

Hi @alza-bitz

The failure-threshold is the percentage (ratio) of failed calls compared to the overall calls. The value 0.5 means 50%.

see https://github.com/BrunoBonacci/safely/blob/master/src/safely/core.clj#L536-L540

If the value is too low, it will trigger too often. If the value is too high, it might attempt calls that would almost certainly fail. The correct value has to be decided by keeping in mind the volume or rate of calls. The default value is a good middle-ground value that works also when you have a low-volume number of calls.

When calls are issued the circuit breaker keeps track of how many requests are successful and how many are failed in the recent past. If more than 50% of the calls are failed then it trips the circuit open, otherwise, it keeps it closed.

I hope it helps

Bruno

BrunoBonacci commented 1 year ago

Hi @alza-bitz

please let me know if you have any further questions or I can close the ticket.