Skip to content
mentoor

Topic 3 of 5 · 5 min read

Circuit breakers and load shedding for providers

Goal: Read a provider error-rate log and decide when the breaker should open and which traffic to shed.

A circuit breaker stops sending calls to a provider that is failing, so users fail fast and the provider gets room to recover. Load shedding then decides which traffic to keep when capacity is short.

Check yourself · 1 of 3

What does a breaker do in the half-open state?

Choose one answer

Why it matters

When a provider degrades, every call waits for a timeout and then retries. Multiply that by all your users and workers, and your own retries become the load that keeps the provider down. This is a retry storm. A breaker turns a slow failure for everyone into an instant, handled failure.

The three states

  1. Closed. Normal. Calls go through while you count failures in a rolling window.
  2. Open. Failures crossed the threshold, for example half of the last 20 calls failed or timed out. Calls are rejected immediately, and your code goes to the fallback or degraded mode.
  3. Half-open. After a cool-down, let a few probe calls through. If they succeed, close the circuit. If one fails, open it again for a longer time.

Count the right failures: timeouts, 5xx and 429. Do not count a 400 caused by your own bad request.

Retries without storms

  • Retry only transient errors, a small number of times.
  • Use exponential backoff with jitter (random spread), so thousands of clients do not retry at the same instant.
  • Respect Retry-After on a 429.
  • Use a retry budget: retries may add at most a fixed fraction, for example 10 percent, on top of normal traffic.

Shedding load

When the fallback is also slow or a quota is exhausted, you cannot serve everything. Give each request a priority and drop from the bottom: background summaries and batch jobs first, free-tier chat next, paid users and anything that completes a purchase last. Reject early with a clear status so clients can back off.

if (breaker.state(provider) === "open") {
  if (req.priority === "low") return reject(503, "try_later");
  return callFallback(req);
}

Before and after

Weak: the provider returns 529 for 20 minutes. Each of 200 workers retries 5 times with a fixed 1 second delay. Queue depth grows, memory climbs, and your service dies before the provider recovers.

Fixed: after 10 failures out of 20 calls the breaker opens for 30 seconds. Chat goes to the fallback, nightly summaries are rejected and requeued, and one probe per worker group tests recovery.

What goes wrong

  • A breaker per process only. Each instance learns separately, so the provider sees many probes. Share state or accept the cost.
  • Thresholds on too few calls. Two failures out of three at 3 a.m. opens the circuit for nothing. Require a minimum request count.
  • No half-open step, so the circuit stays open after the provider has recovered, or closes and floods it again.
  • Shedding by arrival order instead of by priority.

Examples

Reading an error-rate log

A log shows calls per minute and failures: 12:00 100 calls, 2 failed; 12:01 100 calls, 41 failed; 12:02 100 calls, 78 failed. The breaker with a threshold of 50 percent over a 2-minute window should open at 12:02, when about 60 percent of 200 calls have failed, not at the single bad minute.

Who gets shed

Traffic during an outage: checkout assistant, free-tier chat, nightly product-description generation. Keep the checkout assistant on the fallback, serve free chat a short queue message, and pause the nightly job until the breaker closes.

Key points

  • A breaker has three states: closed, open and half-open with probe calls.
  • Open on a failure rate over a rolling window with a minimum number of calls, not on a single error.
  • Count timeouts, 5xx and 429 as failures; do not count your own 400 errors.
  • Combine backoff, jitter, Retry-After and a retry budget so retries do not become a storm.
  • Shed by priority: batch and background work first, purchases and paid users last.

Common mistakes

  • Retrying immediately with a fixed delay from every client, which creates a synchronized storm.
  • Opening the breaker on a tiny sample, or counting client-side 400 errors as provider failures.
  • Closing the breaker with no probe step.
  • Shedding randomly or first-come-first-served instead of by business priority.

Check yourself · 2 of 3

What is the main job of jitter in retry logic?

Choose one answer

Check yourself · 3 of 3

The provider is overloaded and capacity is short. Which traffic should be shed first?

Choose one answer

Practice items, not counted towards your mastery.

Start here

More for this topic