Your order screen calls a shipping-quote service, a stock service and a payment service for every order. One morning the shipping service does not go down; it just gets slow, and every call hangs for thirty seconds before timing out. The order screen freezes, and soon unrelated pages stop responding too. The way your code waited, not a bug, took your system down. The circuit breaker pattern, timeouts, retries and resilient integration techniques exist to stop a slow external service from spreading into your own system.
All example figures are illustrative; they are not measurements of a specific service or an industry average.
What Is the Circuit Breaker Pattern, and How Do Timeouts and Retries Fit In?
All three manage a synchronous call (a request where the caller waits for the answer) when things go wrong. A timeout sets the longest wait. A retry repeats the call after a transient failure. A circuit breaker watches the error rate of recent calls and, past a limit, stops making the call at all until the service recovers; it is named after the fuse in a house.
The topic is the call that must be made while a user waits. For handing work off, see message queues; for event notifications, webhooks; for the basics, API integration.
We recommend reading: Our integration services
Why Does Your System Crash When an External Service Slows Down?
Because in a synchronous call every waiting request holds a thread, and the thread pool is limited. The number of calls in flight is requests per second multiplied by the wait time (Little's law): concurrent calls = requests/s × wait time.
Example: the system receives 25 requests per second and the pool has 50 threads. When the service is healthy a call takes 0.12 s, so 25 × 0.12 = 3 calls are in flight and the pool is 6% full. When the service hangs and the timeout is thirty seconds, you need 25 × 30 = 750 threads, but there are only 50, so the pool fills in 50 ÷ 25 = 2 seconds. From that moment even requests unrelated to this service cannot find a thread.
| Wait time | Concurrent calls needed | Pool load (50) | Result |
|---|---|---|---|
| Healthy: 0.12 s | 25 × 0.12 = 3 | 6% | Comfortable |
| Breaker open: 0.005 s | 25 × 0.005 = 0.125 | 0.25% | Fast error, pool empty |
| Timeout 1.2 s | 25 × 1.2 = 30 | 60% | Slow but standing |
| Timeout 2 s | 25 × 2 = 50 | 100% | At the edge; a small rise breaks it |
| Timeout 30 s | 25 × 30 = 750 | 1,500% | Pool full in 2 s |
For how this equation behaves as load grows, see our software scalability guide.
How Do You Choose a Timeout?
A timeout should be a value at which almost all healthy calls finish but a hung call does not hold a thread for long. The starting point is not a guess but the measured latency distribution.
For the example service, take a median (p50) of 120 ms, p95 of 350 ms, p99 of 800 ms and p99.9 of 1,200 ms. If you set the timeout equal to p99 (800 ms), you kill 1% of healthy calls yourself: 900 of the 25 × 3,600 = 90,000 calls per hour. Set at p99.9 (1,200 ms), false timeouts drop to 0.1%, or 90 calls per hour. A sensible start is 1.5 times p99 or about p99.9, then tuned with real traffic.
- Separate connect and read time: for instance 300 ms to connect and 900 ms to read the response (1,200 ms total). If the connection cannot be made, a long wait is pointless.
- Do not trust defaults: many client libraries default to an unlimited timeout.
When Do Retries Help and When Do They Hurt?
Retries help only with transient errors; with permanent errors or overload they make things worse. First look at the type of error.
| Error | Retry? | Why |
|---|---|---|
| Connection dropped, timeout | Yes (if idempotent) | May be transient |
| 503 service unavailable | Yes, with backoff | The service may be recovering |
| 429 too many requests | Yes, honouring the wait time | The other side states how long to wait |
| 400, 422 invalid request | No | The same request gives the same error |
| 401, 403, 404 | No | Will not pass until access or the record is fixed |
The retry storm
If everyone retries at the same moment while a service is struggling, load rises and the service never recovers. Take 1,000 clients each making one call per second to an external service: normal load is 1,000 calls/s and the service capacity is 1,200 calls/s. If every client retries three times on failure (4 attempts in total), load becomes 4 × 1,000 = 4,000 calls/s, which is 4,000 ÷ 1,200 ≈ 3.3 times capacity. When layers are nested the multiplier grows: if three layers each make three attempts, that is 3 × 3 × 3 = 27 times, or 27,000 calls/s.
The remedy has three parts. First, growing waits (exponential backoff): the wait doubles before each attempt. Second, random jitter: a random moment inside the wait is chosen so clients do not move in lockstep. Third, a retry budget: retries are capped at 10% of first calls; with 1,000 calls that is at most 100 retries, a total of 1,100 calls/s, below capacity.
| Retry | Random wait | Average | Load with 1,000 clients |
|---|---|---|---|
| 1 | 0–100 ms | 50 ms | 1,000 ÷ 100 = 10 calls/ms |
| 2 | 0–200 ms | 100 ms | 1,000 ÷ 200 = 5 calls/ms |
| 3 | 0–400 ms | 200 ms | 1,000 ÷ 400 = 2.5 calls/ms |
Without jitter, all 1,000 clients would call at exactly the 100th ms. The three waits add up to 50 + 100 + 200 = 350 ms on average and 100 + 200 + 400 = 700 ms in the worst case.
Calls that cannot be repeated, and the time budget
A call that does harm if done twice, such as a payment, is not retried without a unique request key. Illustrative: of 1,200 payment attempts a day, 0.5% (6) time out and half of those were actually processed. Blind retries cause 3 duplicate charges × ₺850 = ₺2,550 per day. With a key, the other side treats the second request as the same operation; for the concurrency side see our double-selling guide.
The time you promised the user also limits retries. With a 3,000 ms total budget: the first attempt times out at 1,200 ms, at most 100 ms is waited, and the second attempt takes another 1,200 ms; the total is 1,200 + 100 + 1,200 = 2,500 ms, leaving 500 ms for your own work. A third attempt would run to 2,500 + 200 + 1,200 = 3,900 ms; it does not fit the budget, so it is not made. The rule: if the remaining time is shorter than the timeout, do not retry. In a budgeted system the worst-case load is 2×, not 4×.
How Does a Circuit Breaker Work?
A circuit breaker counts the outcomes of recent calls to a dependency and, once a limit is crossed, returns an error immediately without sending the call. A dead service is no longer waited on and gets room to breathe. It has three states.
| State | What it does | Transition (example settings) |
|---|---|---|
| Closed (normal) | Calls pass, outcomes are counted | Opens if the error rate over the last 20 calls reaches 50% (10 errors); at least 20 calls are needed |
| Open | No call is made; an immediate error or fallback is returned | After 30 seconds it moves to half-open |
| Half-open | Allows only 3 trial calls | Closes if all 3 succeed; reopens if one fails |
The count includes slow calls, not just errors: for example the breaker opens if more than 50% of calls take longer than 1,000 ms. A service often slows down before it fails; a breaker that counts only errors reacts late.
The breaker is kept per dependency: when the shipping service trips, stock calls are unaffected.
The arithmetic of a ten-minute outage
Take 25 calls per second and an outage of 10 minutes (600 s): 15,000 calls in total. If the service refuses connections immediately, 20 calls fill in 20 ÷ 25 = 0.8 s and the breaker opens; if calls wait for the timeout, the decision comes at 0.8 + 1.2 = 2.0 s. Every 30 seconds 3 trial calls are made (60 over 20 cycles). Slow failures number 20 + 60 = 80, or 0.53%; the remaining 15,000 − 80 = 14,920 calls (99.47%) return in about 5 ms with a fast error or a fallback.
Without a breaker, all 15,000 calls wait 1.2 s and hold 15,000 × 1.2 = 18,000 thread-seconds; average pool use is 18,000 ÷ 600 = 30 threads (60%). With a breaker, 80 × 1.2 + 14,920 × 0.005 ≈ 171 thread-seconds are held; average use is about 0.3 threads (0.6%).
When the service recovers, the next trial call succeeds within 30 seconds at most, 15 seconds on average, and the breaker closes. Raising traffic gradually (10%, 50%, 100%) avoids flattening the recovering service again.
Why Do You Need Bulkheads?
If all dependencies share one thread pool, the slowest one consumes everyone's resources. Bulkheads, like the watertight compartments of a ship, give each dependency its own share.
Example: split 50 threads into 10 for shipping quotes, 25 for stock and 15 for payment. If the shipping service hangs, it exhausts only its own 10 threads; the remaining 40 (80%) keep working, and the other calls need 17.5 × 0.12 = 2.1 threads. In a shared pool with a thirty-second timeout, shipping calls arrive at 25 × 0.3 = 7.5 per second and fill the pool in 50 ÷ 7.5 ≈ 6.7 seconds.
Call chains grow longer in microservices.
How Do You Design a Fallback Response?
While the circuit is open, instead of silently showing an error, an incomplete but useful response is returned where possible. A fallback does not suit every call; rank dependencies by criticality first.
- Last good value from a cache: a shipping tariff is cached for 15 minutes and shown marked as an "estimate".
- A default value or switching a feature off: a best-sellers list when recommendations are missing, or the review section hidden.
- A clear error: an honest error message for operations that cannot be guessed, such as payment.
The numbers: assume 150 orders per hour (1,200 orders a day over an 8-hour busy window). If the shipping service is down for 20 minutes, 150 × 20 ÷ 60 = 50 orders are affected. With an average basket of ₺850, blocking them puts 50 × ₺850 = ₺42,500 of revenue at risk. If the cached tariff deviates from the real fee by 6% on average and the average shipping fee is ₺90, the deviation is at most 50 × ₺5.4 = ₺270. Stale data is accepted only where it does not turn into loss; for stock, which can cause overselling, the stale lifetime is very short.
How Do You Give a Time Budget to Services in a Chain?
If one service calls another and that one calls a third, each hop's timeout must be shorter than the previous one; otherwise the outer caller gives up while the inner ones keep working. Say the user was promised 3,000 ms and there are three hops; each hop leaves about 600 ms for its own work.
| Hop | Timeout | Calculation |
|---|---|---|
| Client → API | 3,000 ms | The time promised to the user |
| API → stock service | 2,400 ms | 3,000 − 600 (the API's own work) |
| Stock service → warehouse service | 1,800 ms | 2,400 − 600 |
| Warehouse service → database | 1,200 ms | 1,800 − 600 |
This way the innermost error occurs first and rises with a meaningful message. Retries are made in one layer only, the outermost or the innermost; two attempts at every hop would raise the load to 2³ = 8 times. The remaining time is passed down in the request header.
How Do You Run Health Checks and Fault-Injection Tests?
A circuit breaker looks at real traffic (passive); a health check asks a dependency at regular intervals whether it is up (active). Example setting: a 10 s interval, and an instance is pulled from traffic after 3 consecutive failures; detection takes at most 3 × 10 = 30 s.
Fault injection means deliberately breaking a dependency to watch how the system reacts; it is done in a test environment, without real customer data.
| Scenario | Expected behaviour |
|---|---|
| +2 s latency on all calls | Timeout at 1.2 s; breaker opens at about 2.0 s, pool does not exceed 60% |
| 50% error rate | Breaker opens at the limit (10 ÷ 20), fallback takes over |
For the process during a real incident, see our incident management guide.
Which Metrics Show Resilience?
For monitoring see software monitoring, and for error queues see integration operations.
Four metrics are enough: retry rate (at most 10% of first calls), the time the breaker stays open, pool load (alert at 70%) and the p99 ÷ timeout ratio. In the example, 800 ÷ 1,200 = 66.7%; near 100%, false timeouts rise.
What Does Resilience Work Cost and When Does It Pay Back?
Illustrative example: the team spends 8 person-days (8 × ₺9,000 = ₺72,000); yearly upkeep is 2 person-days, ₺18,000. Assume 6 outages a year in external services and that, without protection, each stops the order flow for 40 minutes: at 150 orders per hour that is 100 orders × ₺850 = ₺85,000, or ₺510,000 over 6 events. With protection the loss falls to 10%, ₺51,000. The gain is 510,000 − 51,000 = ₺459,000; after upkeep the net is ₺441,000 per year = ₺36,750 per month; payback is 72,000 ÷ 36,750 ≈ 2.0 months.
Sensitivity: with only 1 event a year the gain is ₺76,500, the net is ₺58,500 per year = ₺4,875 per month and payback is 72,000 ÷ 4,875 ≈ 14.8 months.
How Do You Build Resilient Integration in Six Steps?
- List the dependencies: which is critical and which can wait?
- Write timeouts from measurements: from p99 and p99.9.
- Limit retries: transient errors, growing waits, jitter, a 10% budget, a request key.
- Add a circuit breaker and bulkheads: one per dependency.
- Design fallbacks: what is shown, and how much staleness is acceptable?
- Test with fault injection and measure.
What Are Common Mistakes in Resilience Work?
- Setting no timeout: the default unlimited wait is the most common cause of outages.
- Retrying every error without jitter or limits: synchronised repeats cause storms, and layers multiply.
- A single global circuit breaker or an untested fallback path: one stops all calls, the other shows its own bug for the first time during an outage.
We recommend reading: What is an API gateway? Its jobs, latency cost and decision guide
We recommend reading: How to do performance testing
Resilient Integrations with İnovabil
At İnovabil our focus is building software from scratch, tailored to the company: custom software, ERP, CRM, e-commerce, web, mobile and integration. For a solution that connects to external systems, we recommend writing the timeout, the retry limit and the fallback behaviour down during requirements; what happens during an outage is a business decision.
We built CRM, purchasing and mobile for Avcılar İnşaat; a purchasing system for İZBAN; logistics and shipment planning with wholesale order management for Karyak; warehouse management and a complaint and support system for Sleepy; and a prepaid cargo system for Kredili Kargo. List your external services and we will draw up a dependency inventory together.
We recommend reading: Our software development process: step by step from analysis to launch
Frequently Asked Questions
What is a circuit breaker, in short, and what is it for?
A protection pattern that watches the error or slowness rate of recent calls to a dependency and, once the rate crosses a limit, stops sending calls for a while. It stops waiting on a dead service, protects the thread pool and gives the service time to recover.
How many seconds should a timeout be?
It is chosen from the measured latency distribution; in the example, 1,200 ms when p99 is 800 ms. Setting it equal to p99 cuts about 1% of healthy calls. Set connect and read time separately and do not exceed the total time promised to the user.
Is retrying always a good idea?
No. Only for transient errors such as dropped connections, timeouts and 503, with growing waits, jitter and a budget such as 10%. In the example, three unlimited retries raise load fourfold. Invalid requests and permission errors are not repeated.
What does the user see while the circuit is open?
It depends on the dependency: the last cached value, default content, a disabled feature or a clear error. For something like a shipping quote, a stale tariff marked as an estimate fits; for payment, an honest error and a suggestion to try again.
How do you test resilience patterns?
By deliberately breaking the dependency in a test environment: extra latency, a 50% error rate, a total outage, a connection that does not answer and a partial recovery. In each scenario you measure when the breaker opens, the pool load and the fallback behaviour. Real customer data is not used.