← Back to Questions
Microservices

What is Circuit Breaker?

Learn What is Circuit Breaker? with simple explanations, real-time examples, interview tips and practical use cases.

What is Circuit Breaker in Microservices?

Circuit Breaker is a fault-tolerance design pattern used in Microservices Architecture to prevent continuous failures and protect systems from cascading breakdowns.

It helps applications handle failures gracefully when one microservice becomes slow, unavailable, or overloaded.

Circuit Breaker is widely used in:

  • Microservices Architecture
  • Distributed Systems
  • Cloud-native applications
  • API communication
  • Event-driven systems

Simple Understanding of Circuit Breaker

Circuit Breaker in software works similarly to an electrical circuit breaker in homes.

Electrical Circuit Breaker

If excessive electricity flows:

  • Circuit breaker cuts the connection
  • Prevents fire and hardware damage

Software Circuit Breaker

If a microservice continuously fails:

  • Circuit breaker stops sending requests temporarily
  • Prevents system overload
  • Allows recovery time

Why Circuit Breaker is Needed

In Microservices Architecture, services communicate over networks.

Network communication may fail because of:

  • Service crashes
  • High traffic
  • Timeouts
  • Slow database queries
  • Network latency

Without Circuit Breaker:

  • Services continuously retry failed calls
  • Threads become blocked
  • System resources get exhausted
  • Entire application may crash

Problem Without Circuit Breaker

Order Service
      |
      v
Payment Service (DOWN)

If Order Service continuously sends requests:

  • Threads wait endlessly
  • CPU and memory usage increase
  • System becomes slow
  • Cascade failures occur

What is Cascade Failure?

Cascade failure happens when one service failure spreads to multiple dependent services.

Example

Order Service
      |
      v
Payment Service (FAILED)
      |
      v
Notification Service

One failure affects the entire request chain.


How Circuit Breaker Solves the Problem

Circuit Breaker monitors failures.

When failures cross a threshold:

  • Circuit opens
  • Requests stop temporarily
  • Fallback responses are returned

Circuit Breaker Flow

Client
   |
   v
Order Service
   |
   X
Circuit Breaker Open
   |
Payment Service Requests Blocked

Main Goals of Circuit Breaker

  • Prevent system overload
  • Reduce cascading failures
  • Improve fault tolerance
  • Provide graceful degradation
  • Improve system stability

Circuit Breaker States

Circuit Breaker works using three states:

  • Closed State
  • Open State
  • Half-Open State

1. Closed State

Normal state.

Requests flow normally between services.

Flow

Order Service ---> Payment Service

Failures are monitored continuously.


2. Open State

If failures exceed threshold:

  • Circuit opens
  • Requests stop temporarily

Flow

Order Service
      |
      X
Payment Service Blocked

Fallback responses are returned immediately.


3. Half-Open State

After waiting period:

  • Few test requests are allowed

If requests succeed:

  • Circuit closes again

If requests fail:

  • Circuit returns to open state

Circuit Breaker State Diagram

          Failures Increase
Closed ----------------------> Open
   ^                              |
   |                              |
   |                              |
   |------ Half-Open <------------|
         Recovery Test

Real-Time Example

Suppose an e-commerce platform contains:

  • Order Service
  • Payment Service
  • Inventory Service

Scenario

Payment Service becomes slow because of database issues.

Without Circuit Breaker:

  • Thousands of requests continue
  • Threads become blocked
  • Order Service also becomes slow

With Circuit Breaker

After multiple failures:

  • Circuit opens
  • Requests stop temporarily
  • Fallback message shown to users

Fallback Response

"Payment Service Temporarily Unavailable"

Circuit Breaker with Fallback

Fallback methods provide alternative responses during failures.

Example

Payment Failed
      |
      v
Return Cached Response

Spring Boot Circuit Breaker Example

Resilience4j is commonly used in Spring Boot.


Dependency

<dependency>
    <groupId>
        io.github.resilience4j
    </groupId>

    <artifactId>
        resilience4j-spring-boot3
    </artifactId>
</dependency>

Feign Client Example

@FeignClient(name = "PAYMENT-SERVICE")
public interface PaymentClient {

    @GetMapping("/payments/process")
    String processPayment();
}

Circuit Breaker Example

@Service
public class OrderService {

    @Autowired
    private PaymentClient paymentClient;

    @CircuitBreaker(
        name = "paymentService",
        fallbackMethod = "paymentFallback"
    )
    public String placeOrder() {

        return paymentClient.processPayment();
    }

    public String paymentFallback(Exception ex) {

        return "Payment Service Temporarily Unavailable";
    }
}

Configuration Example

resilience4j:
  circuitbreaker:
    instances:
      paymentService:
        failureRateThreshold: 50
        waitDurationInOpenState: 10s
        slidingWindowSize: 5

Explanation of Configuration

Property Description
failureRateThreshold Failure percentage to open circuit
waitDurationInOpenState Time before half-open testing
slidingWindowSize Number of recent requests monitored

Advantages of Circuit Breaker

1. Prevents Cascade Failures

Stops failure propagation across services.


2. Improves Fault Tolerance

System remains stable during failures.


3. Reduces Resource Usage

Prevents unnecessary retries and blocked threads.


4. Faster Failure Response

Users receive immediate fallback responses.


5. Better User Experience

Graceful degradation improves reliability.


Challenges of Circuit Breaker

1. Additional Complexity

Failure handling logic becomes more complex.


2. Configuration Tuning

Incorrect thresholds may create false openings.


3. Monitoring Requirements

Circuit breaker metrics should be monitored carefully.


Circuit Breaker vs Retry

Feature Circuit Breaker Retry
Purpose Stop requests temporarily Retry failed requests
Failure Handling Prevents overload Attempts recovery
Resource Usage Reduces resource usage May increase traffic
Best For Continuous failures Temporary failures

Circuit Breaker with Kafka

Event-driven systems also use circuit breakers.

If consumer service fails:

  • Kafka retains messages temporarily
  • Service resumes processing after recovery

Circuit Breaker in Kubernetes

Kubernetes works together with service meshes like Istio.

Istio provides:

  • Circuit breaking
  • Traffic routing
  • Failure recovery

Real-Time Company Example

Netflix heavily uses Circuit Breaker patterns.

Netflix Hystrix was one of the earliest popular circuit breaker implementations.

Today:

  • Resilience4j
  • Istio
  • Spring Cloud Circuit Breaker

are widely used.


Best Practices for Circuit Breaker

  • Use proper timeout settings
  • Implement meaningful fallback responses
  • Monitor failure rates continuously
  • Combine with retries carefully
  • Use event-driven communication where possible

Interview Ready Answer

Circuit Breaker is a fault-tolerance design pattern used in Microservices Architecture to prevent continuous failures from affecting the entire system. When a service experiences repeated failures, the circuit breaker opens and temporarily blocks further requests to that service. This prevents resource exhaustion, reduces cascade failures, and allows the failed service time to recover. Circuit Breaker works using Closed, Open, and Half-Open states and is commonly implemented using tools like Resilience4j, Hystrix, and Istio in Spring Boot microservices.


Frequently Asked Questions

Why is Circuit Breaker important in microservices?

Because it prevents cascading failures and improves system stability.

What happens when Circuit Breaker opens?

Requests to the failed service are temporarily blocked.

What is fallback in Circuit Breaker?

Fallback provides alternative responses during service failures.

Which library is commonly used in Spring Boot?

Resilience4j is commonly used for Circuit Breaker implementation.

What is the difference between Retry and Circuit Breaker?

Retry attempts failed requests again, while Circuit Breaker stops requests temporarily after repeated failures.

Why this Microservices question is important?

This interview question helps candidates understand real-time backend development concepts, practical problem solving, coding fundamentals, system design basics and production-ready application behavior.

Practice this question carefully for Java backend roles, Spring Boot developer interviews, microservices interviews, company interviews and full-stack developer preparation.

About the Author

Naresh Kumar is a Senior Java Backend Engineer with experience building enterprise applications using Java, Spring Boot, Microservices, Docker, Kubernetes and Cloud technologies.