Circuit Breaker

Prevent cascading failures when a service is down

Introduction

In microservices architectures, services often depend on other services to complete requests. Failures in one service can cascade and bring down the entire system if not handled properly.

The Circuit Breaker Pattern prevents such failures from propagating by monitoring service calls and stopping requests to a failing service until it recovers.

Think of it like a circuit breaker in your home electrical system: when an overload occurs, the breaker trips to prevent damage. Similarly, in microservices, the pattern protects your system from cascading failures.

Problem Statement

  • One failing service can cause cascading failures in dependent services.
  • Continuous retries on a failing service waste resources and increase latency.
  • Without failure isolation, system availability and user experience suffer.

Example:

  • order-service calls payment-service.
  • If payment-service is down → repeated retries overload both order-service and the network.
  • Circuit Breaker prevents further calls until payment-service recovers.

Concept Overview

Definition:
A Circuit Breaker monitors calls to external services and trips when failures exceed a threshold. During the open state, calls are short-circuited to fail fast or use fallback logic.

Core Idea:

  • Closed: Calls flow normally.
  • Open: Calls fail immediately (service considered unhealthy).
  • Half-Open: Some calls are allowed to test if service recovered.

Diagram (simplified):

Client ---> [Circuit Breaker] ---> External Service
(Closed/Open/Half-Open)

When to Use

  • Services with dependent external or internal microservices.
  • Systems where failures can propagate across services.
  • High-latency or unreliable downstream services.
  • Prevent cascading failures in highly distributed systems.

When to Avoid

  • Simple, reliable service calls with minimal dependencies.
  • Small-scale systems where infrastructure complexity is unnecessary.
  • Services with automatic retries built-in and guaranteed uptime.

Architecture / Flow

  1. Client calls a service through the circuit breaker.
  2. Circuit breaker monitors success/failure rates.
  3. If failures exceed threshold → breaker opens.
  4. Calls fail immediately → fallback logic executed.
  5. After a timeout, breaker enters half-open → test call allowed.
  6. If service succeeds → breaker closes, normal traffic resumes.

Implementation (Step-by-Step)

Technology Choices

  • Resilience4j (Java) → modern replacement for Hystrix.
  • Hystrix (Netflix) → popular Java implementation, now in maintenance mode.
  • Polly (.NET) → resilience and transient fault handling.
  • Node.jsopossum library for circuit breaker.

Example: Node.js Circuit Breaker using opossum

const CircuitBreaker = require('opossum');
const axios = require('axios');

const options = {
timeout: 3000, // 3 seconds
errorThresholdPercentage: 50, // open breaker if 50% of requests fail
resetTimeout: 5000 // attempt reset after 5 seconds
};

const serviceCall = () => axios.get('http://payment-service:5000/pay');
const breaker = new CircuitBreaker(serviceCall, options);

breaker.fallback(() => 'Payment service currently unavailable.');

breaker.fire()
.then(console.log)
.catch(console.error);

  • Calls fail fast when service is unhealthy.
  • Fallback ensures graceful degradation.

Advantages

  • Prevents cascading failures in distributed systems.
  • Improves system stability and resilience.
  • Supports graceful degradation via fallback responses.
  • Reduces wasted resources on repeated failing calls.
  • Provides metrics for monitoring service health.

Disadvantages / Pitfalls

  • Misconfigured thresholds → breaker may trip too early or too late.
  • Adds complexity to service calls.
  • Can mask underlying issues if fallback logic is overused.
  • Requires monitoring → otherwise failures may go unnoticed.

Best Practices

  • Set appropriate failure thresholds based on real traffic patterns.
  • Implement fallback mechanisms to maintain user experience.
  • Combine with retry policies cautiously → avoid overwhelming failing services.
  • Monitor breaker states and metrics for anomalies.
  • Use half-open testing to gradually restore traffic to healthy services.

Common Mistakes to Avoid

  • Using circuit breaker without fallback logic → poor user experience.
  • Not tuning timeouts and thresholds → breaker triggers unnecessarily or too late.
  • Applying breaker on infrequent calls only → limited protection.
  • Ignoring monitoring and logging → system behavior becomes opaque.

Comparison with Alternatives

Without a circuit breaker, services rely on retry mechanisms or fail silently. While retries can help transient failures, they can also exacerbate system load during persistent outages, causing cascading failures. Fallback mechanisms alone do not prevent repeated calls to a failing service. Circuit breakers combine failure detection, call short-circuiting, and fallback strategies in a single pattern. Compared to bulkheads, which isolate failures at a system or resource level, circuit breakers focus on protecting dependent calls at the service-to-service communication level. Together, they create resilient, fault-tolerant systems.

Real-World Use Cases

  • Netflix Hystrix → pioneered circuit breaker in large-scale microservices.
  • Amazon → protects downstream services from overload during spikes.
  • Airbnb / Uber → prevent cascading failures in payment and booking services.

Expert Tips 🔥

  • Combine circuit breakers with retries carefully to avoid overwhelming failing services.
  • Use metrics and dashboards to monitor breaker activity.
  • Tune timeouts, error thresholds, and reset intervals based on service SLAs.
  • Integrate with service mesh for automatic failure detection and fallback routing.

Conclusion / Key Takeaways

The Circuit Breaker Pattern is a critical resilience pattern in microservices. It prevents cascading failures, ensures graceful degradation, and maintains system stability. By detecting failures early and stopping calls to unhealthy services, circuit breakers protect both the service and the system as a whole.

Think of it as the electrical circuit breaker of your microservices ecosystem: tripping when overload occurs to prevent total system collapse.