Circuit breakers: keep your app up when a partner API fails
Short answer: When a service your app depends on, such as a shipping-rate API, an address lookup, a loyalty platform or a supplier's stock feed, stops responding, every request that waits on it gets slow, and eventually your own app runs out of workers and falls over too. A circuit breaker watches calls to that service; after a run of failures it "trips" and fails fast for a while instead of waiting, then lets a few trial calls through to see if the service has recovered. Pair it with a fallback for each integration, so customers can still order when a non-essential partner is down.
How one slow partner takes down your app
Picture a checkout that calls a shipping-rate API to show delivery prices. One afternoon the API does not fail cleanly; it just stops answering. Each checkout request now waits until its timeout, say 30 seconds. Web workers pile up waiting. New visitors queue behind them. Soon product pages and the homepage are slow too, even though they never call the shipping API, because the server is busy waiting.
Martin Fowler describes this risk in his article on the circuit breaker pattern: with many callers waiting on an unresponsive supplier, you can run out of critical resources, leading to cascading failures across multiple systems. The article credits Michael Nygard's book Release It with popularising the pattern to prevent this kind of cascade.
How a circuit breaker works
The idea mirrors an electrical breaker. Microsoft's Azure Architecture Center describes it as a state machine with three states:
- Closed. Requests go through normally. The breaker counts recent failures, and if they exceed a threshold within a time window, it switches to open.
- Open. Requests fail immediately with an error, without calling the service, and a timer starts.
- Half-open. When the timer expires, a limited number of requests are allowed through. If they succeed, the breaker assumes the fault is fixed and returns to closed; if not, it goes back to open.
Fowler's article adds a useful operational point: you will usually also want an alert when the breaker trips.
Retries and circuit breakers together
Retries and circuit breakers solve different problems. Microsoft's guidance notes that brief, transient faults are best handled with a strategy such as the retry pattern, while faults that take longer to fix need something that stops the application from repeatedly trying an operation likely to fail.
In practice: retry a couple of times with backoff for blips, and let the breaker cut off calls when the service is clearly down. Make sure retries count towards the breaker's failure tally, and that an open breaker stops retries immediately.
Design a fallback for every integration
Failing fast is only half the job. Decide what the customer sees when each partner is unavailable:
- Shipping rates: show a standard flat rate, or "delivery cost confirmed by email", and let the order proceed.
- Address lookup: fall back to manual address fields.
- Loyalty points: complete the order and award points later from a queue.
- Stock feed: show the last known stock with "availability confirmed on dispatch", or switch to "call to order" for low-stock items.
- Payment provider: usually no safe fallback; show a clear message and, if you have a second provider, offer it.
Not every dependency should be optional. Write down for each one whether the app can continue without it and what changes.
Choosing thresholds
- Timeouts first. A breaker cannot help if each call waits a minute. Set short, deliberate timeouts on every outbound call.
- Failure threshold: for example, open after half the calls in the last 30 seconds fail, with a minimum number of calls so one error at a quiet time does not trip it.
- Open duration: long enough for a partner to recover, often tens of seconds to a few minutes.
- Half-open trials: a handful of requests, not a flood.
Tune these with real data from your logs, and review after incidents. It also helps to rehearse: in staging, point an integration at an address that never answers and watch the breaker open, the fallback appear and the alert arrive, before a real outage tests it for you.
Implementation options
Most languages have well-tested libraries for circuit breakers, often combined with retries, timeouts and bulkheads that limit how many workers any one dependency can use. Some service meshes and API gateways provide breakers at the network level. For a small app, a library around each outbound HTTP client is usually enough. Keep one breaker per dependency, not one shared breaker, so a failing shipping API does not block the address lookup.
Make it visible
- Alert when a breaker opens and when it closes again.
- Show breaker states on your monitoring dashboard.
- Log fallback use, so you know how many orders went through on flat-rate shipping.
- Tell support staff what customers see in each fallback mode.
A worked example
Picture a hypothetical Brisbane homewares retailer whose checkout called a courier's rate API. During a courier outage, checkout requests hung until timeout and the entire site slowed to a crawl during a sale weekend.
The developers set a five-second timeout on the courier call, added a circuit breaker that opens after repeated failures and retries a few trial calls after a minute, and a fallback that shows a standard delivery charge with a note that the exact cost may be adjusted downwards. An alert goes to the on-call developer when the breaker opens. During the next courier outage, checkout stayed fast and orders continued; the team reviewed the fallback orders afterwards.
FAQ
Is a circuit breaker only for microservices?
No. Any app calling external APIs benefits, including a single website calling a few partners.
What if the dependency is essential?
Fail fast with a clear message rather than hanging. That still protects the rest of the site.
Can a breaker open by mistake?
Yes, with thresholds set too tight. Require a minimum number of calls and tune from real traffic.
If one partner's outage has ever slowed your whole site, tell us which services your app relies on.