Why this matters
When a client sends a request to a server, the circuit breaker monitors failure rates to detect persistent issues.
It prevents clients from wasting resources on a failing service and gives the server time to recover.
This pattern protects both client performance and server stability.
How it works
The circuit breaker counts well-formed requests and failure exceptions.
When the number of exceptions exceeds a threshold, it concludes the failure is not transient.
The client stops sending requests for a defined period.
This pause allows the server to recover from overload.
State machine
The circuit breaker operates through three states:
- Closed: Allows requests to pass through.
- Open: Blocks all requests and returns an exception.
- Half open: Sends one trial request to test service availability.
Initially, the circuit breaker is in the closed state.
When failure count exceeds a threshold, it transitions to open.
After a configured duration, it moves to half open.
If the trial request fails, it returns to open.
If the trial request succeeds, it transitions to closed.
If another exception occurs (not the monitored one), it stays in half open and retries once.
Configuration
To set up a circuit breaker, specify:
- The exception type to monitor.
- The number of consecutive failures before opening.
- The duration the circuit remains open.
Example: Five consecutive failures trigger opening; one-minute open duration.
Implementation
Use established open-source libraries like Resilience4j or Poly.
Implement the circuit breaker with a thread-safe instance.
Multiple threads must share the same instance to measure overall failure rates.
Use locking during state reads and recalculations.
Handling rejected requests
When a request is rejected by the open circuit breaker, one of these actions is taken:
- Buffer
- Failover
- Callback
- Apply back pressure up the stack
- Cancel
Questions
- What is the purpose of the half-open state?
- How does the circuit breaker distinguish between transient and persistent failures?
- Why is thread safety essential in a circuit breaker?
- What happens if a trial request returns a non-monitored exception?
- How does the circuit breaker avoid overloading the server?
- What is the role of a health verification endpoint?
- Can a circuit breaker be configured without a timer?
- What is the minimum number of failures required to open a circuit?
Key takeaways
- A circuit breaker detects persistent failures and stops client requests.
- It uses three states: closed, open, and half open.
- Configuration includes monitored exception, failure threshold, and open duration.
- It relies on a thread-safe instance shared across threads.
- Trial requests in half-open state test service recovery.
- Rejected requests are handled via predefined actions.
- Open-source libraries like Resilience4j or Poly are used.
- Health endpoints may be used for periodic service checks.
- The circuit breaker prevents client resource waste and server overload.