Spring Cloud Circuit Breaker Interview Questions and Answers

Master Spring Cloud Circuit Breaker with interview questions covering Resilience4j, Circuit Breaker states, Retry, Timeout, Bulkhead, Rate Limiter, Fallback methods, and production resilience patterns.


Spring Cloud Circuit Breaker Interview Questions and Answers

Introduction

In a distributed microservices architecture, network failures are inevitable.

For example:

  • Payment Service is down.
  • Notification Service responds slowly.
  • Loan Service times out.
  • Database becomes unavailable.
  • Third-party APIs fail intermittently.

Without fault tolerance, failures quickly spread across services, causing cascading failures.

Spring Cloud integrates with Resilience4j, which provides several resilience patterns:

  • Circuit Breaker
  • Retry
  • Time Limiter
  • Bulkhead
  • Rate Limiter
  • Fallback

These patterns help build reliable, highly available enterprise systems.


Resilience Architecture

flowchart LR

CustomerService --> Resilience4j

Resilience4j --> CircuitBreaker

CircuitBreaker --> PaymentService

CircuitBreaker --> Fallback

PaymentService --> PostgreSQL

Q1. What is a Circuit Breaker?

Answer

A Circuit Breaker prevents repeated calls to a failing service.

Instead of continuously sending requests,

it temporarily blocks requests until the service recovers.

Benefits

  • Prevent cascading failures
  • Reduce resource consumption
  • Faster failure response
  • Improve system stability

Q2. Why do we need a Circuit Breaker?

Without Circuit Breaker

flowchart LR

CustomerService --> PaymentService

PaymentService --> Timeout

Timeout --> ThreadBlocked

ThreadBlocked --> ApplicationFailure

With Circuit Breaker

flowchart LR

CustomerService --> CircuitBreaker

CircuitBreaker --> PaymentService

CircuitBreaker --> Fallback

The application fails fast instead of waiting for long timeouts.


Q3. What are the Circuit Breaker states?

Resilience4j defines three states.

State Description
Closed Requests are allowed
Open Requests are blocked
Half-Open Limited requests are allowed to test recovery

State Transition

stateDiagram-v2

Closed --> Open : Failure Threshold Reached

Open --> Half_Open : Wait Duration Completed

Half_Open --> Closed : Successful Calls

Half_Open --> Open : Failure

Q4. How do you configure a Circuit Breaker?

Dependency

<dependency>

<groupId>

org.springframework.cloud

</groupId>

<artifactId>

spring-cloud-starter-circuitbreaker-resilience4j

</artifactId>

</dependency>

Example

@CircuitBreaker(

name="payment",

fallbackMethod="fallback")

public Payment

getPayment(){

}

The annotated method is automatically protected.


Q5. What is a Fallback Method?

A fallback method executes when the primary service fails.

Example

public Payment

fallback(

Exception ex){

    return new Payment(

    "Service Unavailable");

}

Benefits

  • Graceful degradation
  • Better user experience
  • Controlled failure handling

Q6. What is Retry?

Retry automatically repeats failed requests.

Example

@Retry(

name="payment")

Typical use cases

  • Temporary network issues
  • Short-lived service outages
  • Third-party API instability

Avoid retries for non-idempotent operations unless carefully designed.


Q7. What is TimeLimiter?

TimeLimiter limits how long an operation may execute.

Example

@TimeLimiter(

name="payment")

Timeout Flow

flowchart LR

Request --> PaymentService

PaymentService --> Response

PaymentService --> Timeout

Timeout --> Fallback

Requests exceeding the configured timeout fail immediately.


Q8. What is Bulkhead?

Bulkhead isolates resources to prevent one failing component from affecting others.

Example

@Bulkhead(

name="payment")

Bulkhead Pattern

flowchart LR

CustomerRequests --> CustomerThreadPool

PaymentRequests --> PaymentThreadPool

NotificationRequests --> NotificationThreadPool

If Payment Service becomes overloaded,

Customer Service remains responsive.


Q9. What is Rate Limiter?

Rate Limiter controls the number of requests allowed during a time window.

Example

@RateLimiter(

name="payment")

Rate Limiting

flowchart LR

Clients --> RateLimiter

RateLimiter --> AllowedRequests

RateLimiter --> RejectedRequests

Benefits

  • Protect downstream systems
  • Prevent abuse
  • Maintain service availability

Q10. Circuit Breaker Best Practices

Use Circuit Breakers on External Calls

Especially REST APIs and third-party services.


Configure Appropriate Timeouts

Avoid waiting indefinitely.


Use Retry Carefully

Retry only transient failures.


Implement Meaningful Fallbacks

Return cached or default responses when possible.


Monitor Circuit Breaker Metrics

Integrate with

  • Micrometer
  • Prometheus
  • Grafana

Banking Example

flowchart TD

CustomerService --> CircuitBreaker

CircuitBreaker --> PaymentService

PaymentService --> PostgreSQL

CircuitBreaker --> CachedResponse

CircuitBreaker --> Prometheus

Prometheus --> Grafana

The customer receives a graceful response even if Payment Service is temporarily unavailable.


Common Interview Questions

  • What is a Circuit Breaker?
  • Why do we need a Circuit Breaker?
  • What are Circuit Breaker states?
  • What is Resilience4j?
  • What is a Fallback method?
  • What is Retry?
  • What is TimeLimiter?
  • What is Bulkhead?
  • What is Rate Limiter?
  • Circuit Breaker best practices?

Quick Revision

Topic Summary
Circuit Breaker Prevent repeated failures
Closed Normal operation
Open Block requests
Half-Open Test recovery
Retry Retry failed requests
TimeLimiter Timeout protection
Bulkhead Resource isolation
Rate Limiter Control request rate
Fallback Graceful degradation
Resilience4j Fault tolerance library

Circuit Breaker Lifecycle

sequenceDiagram
Customer Service->>Circuit Breaker: Request
Circuit Breaker->>Payment Service: Forward Request
Payment Service-->>Circuit Breaker: Failure
Circuit Breaker->>Circuit Breaker: Increase Failure Count
Circuit Breaker-->>Customer Service: Fallback Response
Note over Circuit Breaker: Circuit Opens After Threshold
Customer Service->>Circuit Breaker: New Request
Circuit Breaker-->>Customer Service: Immediate Fallback
Note over Circuit Breaker: Wait Duration Expires
Customer Service->>Circuit Breaker: Trial Request
Circuit Breaker->>Payment Service: Test Request
Payment Service-->>Circuit Breaker: Success
Circuit Breaker-->>Customer Service: Normal Response

Production Example – Banking Payment Platform

A banking payment platform communicates with an external payment provider.

Scenario

  • Payment Gateway becomes unavailable.
  • Customer Service continues receiving requests.
  • Resilience4j Circuit Breaker detects repeated failures.
  • After the configured failure threshold, the circuit transitions to OPEN.
  • New requests immediately receive a fallback response instead of waiting for timeouts.
  • After the wait duration, the circuit enters HALF-OPEN.
  • A few test requests verify whether the Payment Gateway has recovered.
  • If successful, the circuit returns to the CLOSED state.
flowchart LR

CustomerService --> Resilience4j

Resilience4j --> PaymentGateway

PaymentGateway --> ExternalBank

Resilience4j --> CachedPaymentStatus

Resilience4j --> Micrometer

Micrometer --> Prometheus

Prometheus --> Grafana

This architecture protects the application from cascading failures while maintaining availability and providing operational visibility.


Key Takeaways

  • Circuit Breaker is a resilience pattern that prevents repeated calls to failing services and protects distributed systems from cascading failures.
  • Resilience4j is the recommended fault-tolerance library for modern Spring Cloud applications.
  • Circuit Breakers transition through Closed, Open, and Half-Open states based on service health.
  • Fallback methods provide graceful degradation when downstream services are unavailable.
  • Retry, TimeLimiter, Bulkhead, and Rate Limiter complement Circuit Breakers to improve system resilience.
  • Configure appropriate timeout values and retry only transient, idempotent operations.
  • Monitor Circuit Breaker metrics using Micrometer, Prometheus, and Grafana to detect service degradation.
  • Combining these resilience patterns results in highly available, fault-tolerant, production-ready Spring Cloud microservices.