Resilience4j
Circuit Breaker
Distributed Systems
Fault Tolerance
Microservices Architecture

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:

java
1CircuitBreakerConfig config = CircuitBreakerConfig.custom()
2    .failureRateThreshold(50)
3    .waitDurationInOpenState(Duration.ofMillis(1000))
4    .permittedNumberOfCallsInHalfOpenState(5)
5    .slidingWindowSize(7)
6    .build();
7
8// Create a CircuitBreakerRegistry with a custom global configuration
9CircuitBreakerRegistry registry = CircuitBreakerRegistry.of(config);
10
11// Use the registry to create a circuit breaker instance
12CircuitBreaker circuitBreaker = registry.circuitBreaker("myService");

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:

  1. Service A on Instance 1 checks the central store, finds the breaker closed, and processes requests.
  2. As failures reach the threshold, Instance 1 updates the central store and sets the breaker to open.
  3. 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 AspectConsideration
SynchronizationHow to ensure consistent circuit breaker states across service instances.
Configuration ManagementCentral management of circuit breaker configurations for consistency.
Failure ResponseStrategies to take when a circuit breaker trips (fallback methods, notifications).
Monitoring and LoggingTracking 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.

Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design