Course contentsShow
← System Design
Lesson 889 of 1,91923. API Gateway PatternFree lesson

Circuit Breaking and Fallbacks

Implementing circuit breakers at the gateway to handle backend failures gracefully with fallback responses.

Circuit Breaking and Fallbacks

What you'll learn: How to implement circuit breakers at the API gateway to handle backend failures gracefully with fallback responses.

The Problem

When a backend service fails or becomes slow, your API gateway shouldn't keep sending requests into a black hole. Without protection, you'll waste resources, slow down the entire system, and create a poor user experience as timeouts pile up.

How Circuit Breakers Work

Think of a circuit breaker like an electrical circuit breaker in your home. When too much current flows (too many failures), it "trips" and stops sending electricity (requests) until conditions normalize.

At the gateway, a circuit breaker monitors backend health by tracking failures:

  • Closed state: Normal operation, requests flow through
  • Open state: Too many failures detected, requests immediately fail without trying the backend
  • Half-open state: After a timeout, the gateway tests if the backend recovered

Fallback Responses

When the circuit opens, instead of returning raw errors, the gateway can provide fallback responses:

  • Cached data from previous successful requests
  • Default static responses
  • Simplified responses with degraded functionality
  • Friendly error messages with retry-after hints

This pattern gracefully degrades your system rather than completely failing.

Implementation at the Gateway

The gateway tracks metrics like error rates, timeout counts, and response times per backend service. When thresholds breach (e.g., 50% errors over 10 requests), it opens the circuit for that service. The gateway then returns fallbacks while periodically testing if the backend recovered.

Key Takeaway: Circuit breakers at the API gateway prevent cascading failures by stopping requests to unhealthy backends and returning fallback responses, protecting your system's overall stability while backend services recover.