← Back to list

Building Fault-Tolerant Microservices with Circuit Breaker using Spring Boot & Resilience4j

In a microservices architecture, services often depend on each other. But what happens when one service goes down?

Sanjay Singh · 2026-04-14 01:50 · 53 claps · 3.0 min read paywalled
#circuit-breaker #spring-boot #microservice-architecture #resilience4j #microservices-pattern
Open on Medium ↗
Wiki topics: 🚀 · Self Improvement 🏛️ · Architecture

Building Fault-Tolerant Microservices with Circuit Breaker using Spring Boot & Resilience4j

In a microservices architecture, services often depend on each other. But what happens when one service goes down?

we’ll explore:

  • What is Circuit Breaker?
  • Why it is needed
  • How it works
  • Implementation using Spring Boot & Resilience4j
  • Real-time example

If you’re not a Medium subscriber, you can read the full interview-focused article using this friend’s link: [👉 Read the full article here](https://sanjaysingh-dev.medium.com/building-fault-tolerant-microservices-with-circuit-breaker-using-spring-boot-resilience4j-0a18a5daf7c1?sk=30de20792f6f7b5ade12f6d56d86dd9a)

Circuit Breaker Pattern in Microservices

Circuit Breaker Pattern in Microservices

Let’s stay connected!

If you found this helpful, follow me for more deep dives into Java, Spring Boot, and System Design.

**Read my technical blog on Medium:**

**Connect with me on LinkedIn:**

What is Circuit Breaker Pattern?

The Circuit Breaker Pattern prevents a system from making repeated requests to a failing service.

Think of it like an electrical circuit breaker:

  • If everything is fine → current flows
  • If failure happens → circuit trips (stops calls)
  • After some time → tries again

What is Circuit Breaker Pattern?

What is Circuit Breaker Pattern?

Why Do We Need It?

Without Circuit Breaker:

  • Continuous retries → system overload
  • Cascading failures
  • Poor user experience

With Circuit Breaker:

  • Fail fast
  • Provide fallback response
  • Improve system stability

Circuit Breaker States

Circuit Breaker States

1. Closed
Requests are allowed
Failures are monitored

2. Open

Requests are blocked immediately
Fallback logic is executed

3. Half-Open

Limited requests are allowed
If successful → moves to Closed
If failures continue → moves back to Open

Internal Working (Flow)

Request → Circuit Breaker
        → If Closed → Call Service
        → If Open → Fallback Method
        → If Half-Open → Trial Request

Why Resilience4j?

Resilience4j is a lightweight, modern fault-tolerance library designed for Java 8+ and Spring Boot.

Key Advantages

  • Lightweight (no heavy dependencies like Hystrix)
  • Functional programming style
  • Native Spring Boot integration
  • Supports: ***- Circuit Breaker
  • Retry
  • Rate Limiter
  • Bulkhead
  • Time Limiter***

Note: Netflix Hystrix is now deprecated, and Resilience4j is the recommended alternative.

Setting Up Spring Boot with Resilience4j

1. Add Dependencies

<dependency>
    <groupId>io.github.resilience4j</groupId>
    <artifactId>resilience4j-spring-boot3</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-aop</artifactId>
</dependency>

Configuring Circuit Breaker

Add the following configuration in application.yml:

resilience4j:
  circuitbreaker:
    instances:
      orderServiceCB:
        slidingWindowSize: 10
        failureRateThreshold: 50
        waitDurationInOpenState: 10s
        permittedNumberOfCallsInHalfOpenState: 3
        minimumNumberOfCalls: 5
        automaticTransitionFromOpenToHalfOpenEnabled: true

Configuration Explained

  • slidingWindowSize: Number of calls to evaluate
  • failureRateThreshold: Percentage of failures to open the circuit
  • waitDurationInOpenState: Time before moving to Half-Open
  • permittedNumberOfCallsInHalfOpenState: Test calls allowed
  • minimumNumberOfCalls: Minimum calls before evaluation

Implementing Circuit Breaker in Service Layer

Let’s assume our service calls an external Order Service.

Service Class

@Service
public class PaymentService {
@CircuitBreaker(name = "orderServiceCB", fallbackMethod = "orderFallback")
    public String processPayment(String orderId) {
        // Simulating remote service call
        if (Math.random() > 0.5) {
            throw new RuntimeException("Order service is down");
        }
        return "Payment processed for order " + orderId;
    }
    public String orderFallback(String orderId, Throwable ex) {
        return "Order service unavailable. Please try again later.";
    }
}

How Fallback Works

When:

  • Failure threshold is exceeded
  • Circuit is open

The fallback method is executed instead of calling the failing service.

Benefits of Fallback

  • Graceful degradation
  • Meaningful error messages
  • System stability

Monitoring Circuit Breaker Events

Resilience4j exposes actuator endpoints.

Enable Actuator

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

application.yml

management:
  endpoints:
    web:
      exposure:
        include: health,metrics,circuitbreakers

You can now monitor:

  • Circuit breaker state
  • Failure rate
  • Number of calls

Best Use Case

  • Use circuit breakers for remote calls only
  • Combine with timeouts and retries
  • Define meaningful fallback responses
  • Monitor circuit breaker metrics
  • Avoid over-using circuit breakers on internal method calls

cheet summery

cheet summery

Let’s stay connected!

If you found this helpful, follow me for more deep dives into Java, Spring Boot, and System Design.

**Read my technical blog on Medium:**

**Connect with me on LinkedIn:**


메타데이터
post_id
0a18a5daf7c1
slug
building-fault-tolerant-microservices-with-circuit-breaker-using-spring-boot-resilience4j-0a18a5daf7c1
url
https://medium.com/@sanjaysingh-dev/building-fault-tolerant-microservices-with-circuit-breaker-using-spring-boot-resilience4j-0a18a5daf7c1
canonical_url
https://medium.com/@sanjaysingh-dev/building-fault-tolerant-microservices-with-circuit-breaker-using-spring-boot-resilience4j-0a18a5daf7c1
author_url
https://medium.com/@sanjaysingh-dev
status
ok
fetched_at
2026-06-27 18:20:27