Circuit Breaker & Retries

Protecting a system from a failing dependency so the failure doesn't spread.

What is it?

When one service calls another (common in microservices), the other service may be slow or down. Retrying the failing call over and over can make things worse. It adds load exactly when the failing service can least handle it, and the caller can go down too as requests pile up waiting for answers that never come.

A circuit breaker guards against this. After enough failures it "trips" and stops calling the failing dependency for a while, failing fast instead. That gives the struggling service room to recover.

Explain like I'm 10

It's like the circuit breaker in a house: instead of letting a dangerous surge keep flowing and risk a fire, it trips and cuts the circuit. Once it's safe, it's reset and power flows again.

Examples

A simple circuit breaker wrapping a risky call

let failureCount = 0;
let open = false;
let openedAt = null;

async function callWithBreaker(fn) {
  if (open) {
    if (Date.now() - openedAt < 30000) {
      throw new Error("Circuit open — failing fast");
    }
    open = false; // try again after the cooldown
  }

  try {
    const result = await fn();
    failureCount = 0; // success resets the count
    return result;
  } catch (error) {
    failureCount++;
    if (failureCount >= 5) {
      open = true;
      openedAt = Date.now();
    }
    throw error;
  }
}

How it works

A circuit breaker tracks recent failures for a dependency. It has three states:

  • Closed: failures are below a threshold, and requests flow through normally.
  • Open: failures crossed the threshold, so calls fail immediately, without a network call, for a cooldown period.
  • Half-open: after the cooldown, a few test requests go through. If the dependency has recovered, the breaker closes again.

Why does it exist?

Blind retries waste time and resources on calls likely to fail anyway, and enough waiting requests can bring down the caller too. A circuit breaker fails fast instead, keeping the caller stable and giving the failing dependency room to recover.

When to use it

Wrap calls to any external dependency (another microservice, a third-party API) whose failure shouldn't take your service down with it, especially in systems with many service-to-service calls.

When not to use it

When a failure should just fail and be reported, with no point retrying (like a clearly invalid request), a circuit breaker adds complexity for nothing. It's for temporary, recoverable failures.

Common mistakes

  • Retrying a failing call immediately and forever, with no backoff or circuit breaker, adding load just when the dependency is struggling.

  • Setting the threshold or cooldown so tight that the breaker trips on brief blips instead of real, sustained failures.

  • Having no sensible fallback when the circuit is open, so users just see a confusing error.

Practice exercises

  1. Easy:

    Explain in your own words why blindly retrying a failing call can make an outage worse, not better.

  2. Medium:

    Describe the three states of a circuit breaker (closed, open, half-open) and what moves it between them.

  3. Hard:

    Explain what a good 'fallback' could look like while a circuit breaker is open, for a feature like product recommendations.

Interview questions

What problem does a circuit breaker solve?

It stops a caller from hammering a failing dependency. Failing fast keeps the caller stable and gives the dependency room to recover.

What are the three states of a circuit breaker?

Closed (requests flow normally), open (requests fail immediately without trying the call), and half-open (a few test requests go through to check whether the dependency has recovered).

Why is blind, unlimited retrying dangerous?

It piles load on an already struggling dependency and makes the caller back up waiting for responses, which can spread the failure further.