Course contentsShow
← Cloud and Infrastructure
Lesson 2581 of 2,98955. Microservices on CloudFree lesson

Circuit Breaker Pattern

Implementing circuit breakers to prevent cascading failures when downstream services become unavailable.

Circuit Breaker Pattern

You'll learn how to automatically stop calling failing services before they drag your entire system down.

The Core Idea

When one microservice depends on another, what happens if the downstream service becomes slow or completely unavailable? Without protection, your service will keep trying to call it, waiting for timeouts, consuming resources, and potentially cascading the failure across your entire system.

The Circuit Breaker Pattern acts like an electrical circuit breaker in your home. When it detects repeated failures calling a downstream service, it "trips" and immediately returns errors for subsequent calls without even attempting them. After a cooling-off period, it allows a few test requests through. If they succeed, normal operation resumes; if not, the breaker trips again.

How It Works

The circuit breaker maintains three states:

Closed (normal): Requests pass through. Failures are counted.

Open (tripped): Requests fail immediately without calling the downstream service. Prevents resource exhaustion.

Half-Open (testing): After a timeout, allows limited requests through to test if the service has recovered.

Why It Matters

Imagine calling a payment service that's down. Without a circuit breaker, every checkout attempt waits 30 seconds for a timeout, tying up threads and memory. With a circuit breaker, after detecting the service is down, you fail fast (in milliseconds), free up resources, and can return a meaningful error to users or fall back to alternative behavior.

This pattern complements the Saga Pattern you've already learned—where sagas handle distributed transaction rollback, circuit breakers prevent attempts that will obviously fail.

Key Takeaway: Circuit breakers prevent cascading failures by automatically detecting unhealthy downstream services and failing fast, preserving system resources and enabling graceful degradation.