Video in progress

Strong Middle 🧩 Microservices

How do you choose the circuit breaker's sliding window type and thresholds for prod?

If traffic is low or uneven, a time-based window is better; if traffic is steady, count-based works too. I set minimumNumberOfCalls and slowCallDurationThreshold from real metrics, otherwise the breaker either opens for nothing or never opens.

📖 Answer

Resilience4j has two types:

  • COUNT_BASED: the last N calls. In the daytime 100 calls accumulate in 2 seconds, at night in 20 minutes. At night the window keeps old results for a long time and the breaker reacts late.
  • TIME_BASED: the last N seconds. Regardless of traffic density, it reflects the current state.
resilience4j:
  circuitbreaker:
    instances:
      tax:
        sliding-window-type: TIME_BASED
        sliding-window-size: 30          # seconds
        minimum-number-of-calls: 20
        failure-rate-threshold: 50
        slow-call-duration-threshold: 2s
        slow-call-rate-threshold: 80
        record-exceptions:
          - java.io.IOException
          - java.util.concurrent.TimeoutException
        ignore-exceptions:
          - uz.shop.tax.InvalidTaxRequestException

Things that often get overlooked in practice:

  • ignore-exceptions. Business errors (400, "customer not found") aren't a service failure. If they get counted, one bad client opens the breaker and cuts service for everyone.
  • Slow calls. Many failures start not as errors but as slowness. I set slowCallDurationThreshold a little above the p99 latency.
  • The breaker instance is local to the pod. With 10 pods each keeps its own statistics. That's normal, but during testing the question comes up: "why did one pod open and the others not?"

I check the thresholds with a load test: by adding artificial delay to the lower service (with Toxiproxy, for example), I see how long it takes the breaker to open.