Module 5 - Architecture Patterns

Circuit Breaker

Prevent cascading failures by failing fast when a dependency is unhealthy.

1The Electrical Analogy

Simple Analogy
Your house has a circuit breaker. When there's an electrical fault, it trips to prevent fire. Once fixed, you flip it back on. Software circuit breakers work the same way-they "trip" when a service is failing to prevent system-wide damage.

A Circuit Breaker monitors calls to a service. When failures exceed a threshold, it "opens" and fails fast instead of waiting for timeouts.

2Three States

Closed

Normal operation. Requests pass through. Failures are counted.

Open

Circuit tripped. Requests fail immediately without calling service.

Half-Open

Testing recovery. Limited requests allowed. Success → close, failure → reopen.

3Why It Matters

Prevents Cascading Failures

One failing service doesn't take down the whole system.

Fails Fast

No waiting for timeouts. Immediate error response.

Gives Service Time to Recover

Reduces load on struggling service.

Enables Graceful Degradation

Return cached data or fallback response.

4Implementation

Failure ThresholdNumber/percentage of failures to trip (e.g., 5 failures in 10 seconds)
Timeout DurationHow long circuit stays open before trying half-open
Recovery ProbesNumber of successful calls in half-open to close circuit

Popular libraries: Resilience4j (Java), Polly (.NET), Hystrix (deprecated but influential).

5Key Takeaways

1Circuit Breaker stops calling a failing service
2Three states: Closed (normal), Open (failing fast), Half-Open (testing)
3Prevents cascading failures across services
4Use with fallbacks for graceful degradation
5Essential pattern for resilient microservices

?Quiz

1. Service A calls Service B. B is down. Without circuit breaker, what happens?

2. Circuit is Open. What happens to new requests?