Resilience4j circuit breaker behaviour in a distributed system
Codemia Knowledge Hub ·
A Resilience4j circuit breaker wraps a call, counts failures, and once the failure rate crosses a threshold it opens and rejects calls immediately for a set time before letting a few trial calls through. Each service instance keeps its own breaker state, so in a distributed system you need a shared store (such as Redis or a database) if instances must agree on whether the breaker is open.
Resilience4j is a fault tolerance library for Java 8 and functional programming. Unlike Netflix Hystrix, which is in maintenance mode, it is built on the Vavr library, which provides functional APIs that work well with Java lambdas.
How the circuit breaker pattern works
In a microservices architecture, services depend on other network services, and any of them can become a bottleneck or point of failure. A circuit breaker detects failures and stops the same failure from recurring over and over, which protects the rest of the system.
The breaker object wraps a function call and monitors it. When failures reach a threshold, the breaker trips, and for a preset timeout every call fails immediately. After the timeout, the breaker lets a limited number of test requests through. If they succeed, it resets; otherwise it trips again.
Configuring a Resilience4j circuit breaker
Resilience4j lets you configure breakers in detail:
This breaker trips when the failure rate reaches 50% over the most recent 7 calls in the closed state. It stays open for 1000 milliseconds, then allows 5 trial calls to decide whether to close again.
Sharing breaker state across instances
Without coordination, instances of the same service can disagree about the breaker state, which leads to inconsistent behavior. Distributed setups also raise questions about state management and how each service responds when the breaker trips.
A common fix is a centralized store, like Redis or a database, that holds the breaker state. Each instance checks and updates the store to decide whether to let a request through. For example:
- Service A on Instance 1 checks the central store, finds the breaker closed, and processes requests.
- As failures reach the threshold, Instance 1 updates the central store and sets the breaker to open.
- Service A on Instance 2 checks the central store, sees the breaker is open, and blocks requests.
What to plan for in a distributed deployment
| Key Aspect | Consideration |
| Synchronization | How to ensure consistent circuit breaker states across service instances. |
| Configuration Management | Central management of circuit breaker configurations for consistency. |
| Failure Response | Strategies to take when a circuit breaker trips (fallback methods, notifications). |
| Monitoring and Logging | Tracking breaker states changes and failures to analyze system health. |
Beyond the basics, two further strategies are worth considering:
- Adaptive thresholds: adjust failure rate thresholds dynamically based on historical data or predictive analysis.
- Intelligent fallbacks: return limited functionality when a downstream service is down instead of failing outright.
Related reading
- Resilience4j vs Hystrix. What would be the best for fault tolerance?
- Resource allocation in distributed systems
- REST API with websocket using Spring boot
- Restarting AWS lambda function to clear cache
- Restoring database - How to keep replication in place
- Retrieve history of past kafka consumers
- Retrieve the full name of a service in Kubernetes
- Retrieving original timestamp after replication using triggers
Course lessons on this
System design interview problems on this topic
- Design a Key Value Store: solution and walkthrough
- Design a Distributed Unique Id Generator: solution and walkthrough
- Design a Distributed Locking System: solution and walkthrough
- Design a Distributed Counter: solution and walkthrough

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseSystem Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.