Circuit Breakers for Downstream Failures
Protect services from cascading failure using opossum-style circuit breaker integration.
Why Cascading Failures Happen
In an enterprise NestJS backend, your service rarely lives alone. It calls a payment gateway, an auth provider, a search cluster, other microservices. When one downstream dependency slows down, every request waiting on it holds a connection, a thread, and a chunk of the event loop.
- A slow dependency exhausts your HTTP connection pool.
- Pending requests pile up; latency climbs everywhere.
- Your service becomes unhealthy and its own callers start failing.
This domino effect is a cascading failure. A circuit breaker is the pattern that stops the dominoes from falling.
The Circuit Breaker State Machine
A circuit breaker wraps a risky call and tracks its health through three states:
- CLOSED — calls flow through normally. Failures are counted.
- OPEN — too many failures occurred; calls are rejected instantly without touching the dependency. This gives the downstream time to recover.
- HALF_OPEN — after a cooldown, a few trial calls are allowed. If they succeed, the breaker closes; if they fail, it opens again.
The key insight: when the breaker is OPEN, you fail fast instead of waiting on a timeout for every request.
All lessons in this course
- Timeouts, Retries, and Bulkheads with Interceptors
- Circuit Breakers for Downstream Failures
- Distributed Tracing with OpenTelemetry
- Defining SLOs and Error Budgets