codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Subjects
🏗️

System Design

24 chapters

1System Design Basics2Vertical vs Horizontal Scaling3CAP Theorem4Load Balancers & Algorithms5Proxy Servers (Forward & Reverse)6Caching Strategies & Eviction7Content Delivery Networks (CDNs)8Database Replication9Database Sharding & Partitioning10Database Scaling & Sharding11Consistent Hashing12Choosing Databases (SQL vs NoSQL)13Message Queues (Kafka, RabbitMQ)14Microservices Architecture15API Gateways16Rate Limiting Algorithms17Long Polling vs WebSockets vs SSE18Heartbeat & Health Checks19Bloom Filters & Probabilistic Data Structures20Leader Election in Distributed Systems21Event-Driven Architecture22Distributed Locking23Circuit Breaker Pattern24Case Study: Design URL Shortener
SubjectsSystem Design

Circuit Breaker Pattern

Updated 2026-05-05
3 min read

Circuit Breaker Pattern

In a microservices architecture, Service A depends on Service B. If Service B becomes slow or unresponsive, Service A's threads will pile up waiting for responses, eventually exhausting Service A's resources and causing it to fail too. This failure then cascades to Services C, D, and E that depend on Service A. A single failing service brings down the entire system.

The Circuit Breaker Pattern prevents this cascading failure by wrapping calls to external services in a monitoring object that can short-circuit requests when the downstream service is unhealthy.

1. How It Works (The Three States)

Closed State (Normal Operation)

All requests pass through to the downstream service normally. The circuit breaker monitors the failure rate of these requests.

  • If the failure rate stays below a threshold (e.g., less than 50% failures in the last 10 requests), the circuit remains Closed.
  • If the failure rate exceeds the threshold, the circuit trips and transitions to the Open state.

Open State (Fail Fast)

The circuit breaker immediately rejects all requests without even attempting to call the downstream service. It returns a fallback response (e.g., cached data, a default value, or a friendly error message) instantly.

  • This gives the failing downstream service time to recover without being bombarded with requests.
  • After a configurable timeout (e.g., 60 seconds), the circuit transitions to the Half-Open state.

Half-Open State (Testing Recovery)

The circuit breaker allows a limited number of test requests to pass through to the downstream service.

  • If these test requests succeed, the downstream service has recovered. The circuit transitions back to Closed.
  • If the test requests fail, the downstream service is still unhealthy. The circuit transitions back to Open for another timeout period.

2. Benefits

  • Prevents Cascading Failures: A single unhealthy service is isolated and cannot bring down the entire system.
  • Fail Fast: Instead of waiting 30 seconds for a timeout from a dead service, the circuit breaker returns a fallback in milliseconds.
  • Automatic Recovery: The Half-Open state automatically tests for recovery, eliminating the need for manual intervention.
  • Reduced Load on Failing Service: By stopping requests, the failing service gets breathing room to recover.

3. Implementation

Popular libraries for implementing the Circuit Breaker pattern:

  • Resilience4j (Java): A lightweight fault tolerance library designed for Java 8 and functional programming.
  • Hystrix (Java): Netflix's original circuit breaker library (now in maintenance mode, succeeded by Resilience4j).
  • Polly (.NET): A .NET resilience and transient-fault-handling library.
  • Istio Service Mesh: Can implement circuit breaking at the infrastructure level without any application code changes.


PreviousDistributed LockingNextCase Study: Design URL Shortener

Recommended Gear

Distributed LockingCase Study: Design URL Shortener