DEV Community

Cover image for Circuit Breaker Pattern: Preventing Cascading Failures in Distributed Systems
kevinrawal
kevinrawal

Posted on

Circuit Breaker Pattern: Preventing Cascading Failures in Distributed Systems

Imagine an e-commerce application.

The Order Service depends on the Payment Service to process orders.

Now, the Payment Service becomes unavailable.

The Order Service keeps sending requests. Each request waits for a timeout, fails, and may be retried.

Soon, the Order Service starts running out of connections, threads, or other resources.

A failure in one service can spread to another.

This is called a Cascading Failure.

How do we prevent this?

The Problem

Consider this architecture:

        User Requests
             |
             v
        Order Service
             |
             v
       Payment Service
             |
             X
        Service Down
Enter fullscreen mode Exit fullscreen mode

When the Payment Service is unhealthy:

  • Requests keep reaching it.
  • Calls may take a long time to time out.
  • Resources remain occupied.
  • Retries may increase the load.
  • The Order Service can become unhealthy too.

We need a way to stop repeatedly calling a service that is likely to fail.

Solution: Circuit Breaker

Think of an electrical circuit breaker.

When a fault occurs, the breaker trips and stops the flow of electricity.

The software Circuit Breaker works similarly.

It monitors calls to a downstream service and temporarily blocks requests when failures cross a configured threshold.

       Order Service
             |
             v
       Circuit Breaker
             |
        +----+----+
        |         |
        v         v
    Allow Call  Fail Fast
        |
        v
   Payment Service
Enter fullscreen mode Exit fullscreen mode

The Circuit Breaker has three states.

1. Closed — Normal Operation

The circuit is closed.

Requests are allowed to reach the Payment Service, and the breaker tracks the outcomes.

Order Service
      |
      v
 Circuit Breaker
   [ CLOSED ]
      |
      v
Payment Service
Enter fullscreen mode Exit fullscreen mode

If the service is healthy, the circuit remains closed.

But if failures exceed the configured threshold, the circuit opens.

2. Open — Stop Calling

When the circuit opens, calls to the Payment Service are blocked for a period of time.

Instead of waiting for a timeout, the Order Service can fail fast or use a fallback.

Order Service
      |
      v
 Circuit Breaker
    [ OPEN ]
      |
      X
  Call Blocked
Enter fullscreen mode Exit fullscreen mode

This protects the Order Service from wasting resources on calls that are likely to fail.

It also gives the Payment Service time to recover.

The circuit remains open during a configured cooldown period.

3. Half-Open — Test Recovery

After the cooldown period, the circuit transitions to Half-Open.

It allows a limited number of test requests to reach the Payment Service.

          [ OPEN ]
             |
       Cooldown Ends
             |
             v
        [ HALF-OPEN ]
             |
        Test Request
          /      \
         v        v
      Success   Failure
         |        |
         v        v
      CLOSED    OPEN
Enter fullscreen mode Exit fullscreen mode

If the test request succeeds, the circuit closes.

If it fails, the circuit opens again.

This prevents the system from immediately sending a large volume of requests to a service that may not have fully recovered.

How Does It Decide When to Open?

The Circuit Breaker tracks failures and compares them against a configured threshold.

For example:

Failure Threshold: 5 failures
Monitoring Window: 10 seconds
Open Duration: 15 seconds
Enter fullscreen mode Exit fullscreen mode

If 5 failures occur within the monitoring window, the circuit opens.

These are example values, not universal defaults.

Depending on the implementation, the breaker may track:

  • Consecutive failures.
  • Failure rate over a time window.
  • Slow calls.
  • A combination of failure rate and minimum request volume.

A timeout is often treated as a failure, but the exact policy depends on the application.

What Happens When the Circuit Is Open?

The request is rejected without calling the downstream service.

This is called Fail Fast.

But what should the application return to the user?

That depends on the business operation.

For example:

Operation Possible behavior
Product recommendations Return cached recommendations
Search Return partial or cached results
Payment Return a temporary failure or pending status
Notifications Queue the work for later

A fallback should be meaningful and safe.

For payments, we should not pretend the payment succeeded just because the service is unavailable.

Retry and Circuit Breaker: Working Together

Retry and Circuit Breaker solve different problems.

  • Retry: Give a transient failure another chance.
  • Circuit Breaker: Stop calling a dependency when it is repeatedly failing.

Consider this flow:

        Order Service
              |
              v
       Circuit Breaker
              |
              v
         Retry Policy
              |
              v
       Payment Service
Enter fullscreen mode Exit fullscreen mode

The Retry Policy handles transient failures.

The Circuit Breaker prevents repeated calls when the service is unhealthy.

The order of these components matters. A breaker outside a retry policy generally observes the final outcome of a retry sequence, while a breaker inside may observe individual attempts. Choose based on what you want the breaker to measure.

Also, retries should be bounded and use backoff. Otherwise, they can undermine the protection provided by the Circuit Breaker.

For a deeper look at retries, read the Retry Pattern article.

What Circuit Breaker Does Not Solve

Circuit Breaker improves resilience, but it does not guarantee successful operations.

It does not:

  • Fix the downstream service.
  • Guarantee a request succeeds.
  • Automatically retry every failed request.
  • Manage distributed transaction consistency.

If a business transaction spans multiple services and needs coordinated recovery, a pattern such as Saga may be appropriate.

Circuit Breaker protects service communication. Saga manages a distributed business transaction.

TL;DR

Circuit Breaker prevents repeated calls to an unhealthy service. It moves through Closed, Open, and Half-Open states to allow normal traffic, stop calls during failures, and test recovery.

Key takeaway: Don't keep calling a service that is clearly unhealthy. Fail fast, protect your resources, and give the dependency time to recover.

Top comments (1)

Collapse
 
userxnom profile image
Abdullah Naseer •

Really clear walkthrough, Kevin — the Order Service to Payment Service example makes the cascade concrete, and I liked that you said plainly that the 5 failures in 10 seconds with a 15-second open duration are example values, not defaults.

Two things stuck with me. First, your fallback table gets the payment row exactly right: return a temporary failure or a pending status, never pretend the payment succeeded. That is the row most people get wrong. Second, the ordering note — a breaker outside the retry policy, observing the final outcome rather than each attempt — is the detail I would steal for any write-up of this pattern.

The question your payment example leaves me with is about the half-open probe. You note a timeout is often treated as a failure, and a slow payment can still land after the timeout. If the test request in half-open is a real payment, how do you keep it from charging twice — is the probe a cheap read, like a health check, or a real payment guarded by an idempotency key so a retry can never charge twice?