Can you explain the different states of a Circuit Breaker (closed, open, half-open)?

A Circuit Breaker moves through three distinct states — closed, open, and half-open — to manage calls to a potentially failing dependency and give it time to recover.

Key Points: • In the closed state, everything operates normally: requests pass through to the dependency and the breaker just tracks the success/failure rate. • Once failures exceed a configured threshold within a time window, the breaker trips to the open state, immediately failing all calls without even attempting them. • After a configured wait duration, the breaker moves to half-open, allowing a small number of trial requests through to test whether the dependency has recovered. • If those trial requests succeed, the breaker closes again and normal traffic resumes; if they fail, it reopens and the wait period restarts. • This state machine is what prevents both wasted calls to a known-dead dependency and permanent avoidance of a dependency that has actually recovered.

Example: If the Inventory Service starts timing out repeatedly, the breaker trips open after, say, five consecutive failures; after 30 seconds it goes half-open and lets one request through — if that succeeds, it closes and normal traffic resumes, but if it fails, it reopens for another 30 seconds.

Code Example:

resilience4j.circuitbreaker:
  instances:
    inventoryService:
      failureRateThreshold: 50
      waitDurationInOpenState: 30s
      permittedNumberOfCallsInHalfOpenState: 3
      slidingWindowSize: 10

Interview Tip: A concise interview answer is:

"Closed means requests flow normally while failures are tracked; once the failure rate crosses a threshold, it trips to open and fails calls immediately without even trying the dependency. After a wait period it goes half-open and lets a few test requests through — success closes the breaker again, failure sends it back to open."