Under heavy development
StitchAPI
GuidesResilience

Circuit breaker

Stop hammering a failing dependency by opening a circuit after repeated failures.

Add circuit when a dependency is failing and you want the stitch to stop hammering it — after a run of failures the breaker opens and calls fast-fail for a cooldown, giving the dependency room to recover before a single trial call probes it again.

Example

import {  } from 'stitchapi';

const  = ({
    : 'https://api.example.com',
    : '/orders',
    : { : 5, : 30_000 },
});

After 5 consecutive failures the breaker opens; for the next 30 seconds calls to orders() fast-fail instead of hitting api.example.com.

Options

The breaker moves through four states. It starts closed (calls pass through). After failureThreshold consecutive failures it trips open and calls fast-fail for cooldownMs. It then goes half-open and lets a single trial call through: a success closes it and clears the count, another failure re-opens it for a fresh cooldown. The breaker emits progress events with phase circuit.

failureThreshold and cooldownMs are both required — failureThreshold is the count of consecutive failures that trips the breaker open, and cooldownMs is the fast-fail window after opening, before a half-open trial is allowed.

halfOpenAfterMs controls when the half-open trial is permitted (default cooldownMs), letting you fast-fail and probe on different clocks.

key is a store namespace: give two stitches the same key plus a shared store to share one breaker, so a failing host opens the circuit for every stitch that talks to it. The default key is the stitch/host key.

While the breaker is open, a call fast-fails instead of reaching the dependency — see STITCH_CIRCUIT_OPEN for what callers see and how to handle it.

See Reference → Config types for every field.

See also

On this page