Luke a Pro

Luke Sun

Developer & Marketer

🇺🇦
EN||

Circuit Breaker Pattern

| , 2 minutes reading.

Circuit Breaker Pattern

Introduction: The “Cascading Failure”

In a microservices architecture, Service A calls Service B. If Service B is slow, Service A’s threads will start to wait. Soon, Service A runs out of memory and crashes. Then Service C (which calls A) also crashes. This is a Cascading Failure.

The Circuit Breaker acts like an electrical fuse. If a service starts failing, the “breaker” trips, stopping all calls to that service and giving it time to recover, while returning a graceful error to the user.

What Problem does it solve?

  • Input: Success/Failure signals from service calls.
  • Output: The state of the connection (Closed, Open, or Half-Open).
  • The Promise: Stability. It prevents a single failing component from taking down the entire system.

Core States (How it Works)

  1. Closed (Healthy): Everything is normal. Traffic flows to the service.
  2. Open (Failed): If the failure rate crosses a threshold (e.g., 50% failures in 1 minute), the circuit Trips (Opens). All future calls fail instantly with an error. No actual requests are sent to the failing service.
  3. Half-Open (Checking): After a timeout (e.g., 30 seconds), the circuit allows a limited number of test requests.
    • If they succeed: The circuit goes back to Closed.
    • If they fail: It returns to Open.

Fallback: The “Plan B”

When the circuit is Open, the system should not just show an ugly “500 Error.” It should use a Fallback:

  • Default Value: “Currently unavailable, please try again later.”
  • Static Cache: Showing the last known good data.
  • Simplified Logic: Turning off a “Recommended Products” section but keeping the main product details visible.

Typical Business Scenarios

  • ✅ Payment Gateway: If Stripe is down, the Circuit Breaker trips, and the UI tells the user “Payments are temporarily down,” preventing them from clicking “Submit” 100 times.
  • ✅ Microservices: Protecting a heavy search service from being hammered when its database is under load.
  • ✅ External APIs: Avoiding unnecessary costs or quota depletion on an API that is currently returning errors.

Performance & Complexity

  • Overhead: Very low. It’s just a state machine with a few counters.
  • Library: Netflix Hystrix (Legacy), Resilience4j, or Istio’s built-in circuit breaking.

Summary

"The Circuit Breaker is the 'Safety Fuse' for the cloud. It stops you from throwing more wood onto a fire, giving your failing services a chance to breathe and recover."