Circuit Breaker
A design pattern that prevents an application from repeatedly trying to execute an operation that is likely to fail, allowing it to recover without overwhelming the failing service.
Detailed Explanation
The circuit breaker pattern works like an electrical circuit breaker. When failures exceed a threshold, the circuit "opens" and subsequent requests fail fast without calling the failing service. After a timeout, the circuit enters "half-open" state and allows a test request. If it succeeds, the circuit closes; if it fails, it opens again.
Circuit breakers prevent cascade failures in distributed systems. Without them, a failing service can cause requests to pile up, consuming resources and causing failures in other services. Popular libraries: Hystrix (Netflix, legacy), Resilience4j (Java), Polly (.NET), and opossum (Node.js).
Why It Matters
Circuit breakers prevent cascade failures in distributed systems. They are essential for building resilient microservices architectures.
Real-World Example
A payment service is down. Without circuit breakers, every checkout request waits for the payment service to timeout (30 seconds), consuming server resources. With circuit breakers, after 5 failures, the circuit opens and requests fail fast (immediately), preserving resources for other services.
When to Use
For any inter-service communication in microservices. Especially critical for calls to external services (payment providers, third-party APIs) where failures are unpredictable.
Advantages
- Prevents cascade failures
- Fails fast without waiting for timeouts
- Provides fallback behavior
- Allows failing service to recover
- Improves system resilience
Disadvantages
- Adds complexity to service calls
- Requires tuning thresholds and timeouts
- Fallback behavior may not always be possible
- Circuit state management overhead
- Can mask underlying issues
Related Terms
Frequently Asked Questions
What is the difference between circuit breaker and retry?
Retries attempt the same operation again after failure. Circuit breakers stop attempting after a threshold and fail fast. Use both: circuit breaker to prevent cascade failure, retries with backoff for transient failures.
How do I configure circuit breaker thresholds?
Set failure threshold (e.g., 50% error rate opens the circuit), timeout (how long to wait in half-open state), and reset timeout (how long before retrying). Tune based on your service's failure characteristics.
What is a fallback?
A fallback is an alternative response when the primary operation fails. For a payment service fallback: return a "payment pending" status instead of failing the entire checkout. Fallbacks improve user experience during failures.
Do I need circuit breakers for a monolith?
Not typically. Circuit breakers are for inter-service communication in distributed systems. For monoliths, focus on error handling, retries, and graceful degradation within the application.
How do I test circuit breakers?
Unit test the state transitions (closed → open → half-open). Integration test with a failing dependency. Use chaos engineering to simulate failures in production. Verify that circuit breakers open and close correctly.