Skip to content
Muhammet Şafak
tr

When an external service goes down

Timeout, retry and circuit breaker: how three lines of defense stop a slow external service from locking up your whole application.

Not down, just slow

An SMS provider slowed down. The whole application stopped.

Response time climbed to 30 seconds; within half an hour even endpoints with nothing to do with SMS were timing out.

Cascading failure

How slowness spreads

  1. Step 1: The process locks upworker / PHP-FPM process tied up
  2. Step 2: Time disappears30 s call = 30 s busy
  3. Step 3: The pool fillsnew requests wait in line
  4. Step 4: The healthy dieunrelated endpoints time out

5 s

First defense: timeout

total timeout

Not the default 30 seconds, but 5: Http::timeout(5)->connectTimeout(2). The values are the source's suggested example; this number should be a decision.

Second defense: bounded retry

Three attempts, two waits

wait, ms

  • Wait 1200
  • Wait 2400
Example values: 200 × 2ⁿ⁻¹ ms, plus random_int(0, 100) ms of jitter. Three attempts have two waits between them; if errors persist, the problem isn't transient.

~3.4 min

Put a ceiling on backoff

12th wait without a cap

delay = min(cap, base × factorⁿ⁻¹). Without a cap, base 100 ms × 2 reaches about 3.4 minutes by the 12th attempt.

Thundering herd

Jitter types

delay = min(cap, base × factorⁿ⁻¹). Without a cap, base 100 ms × 2 reaches about 3.4 minutes by the 12th attempt.
TypeRangeBehavior
Full[0, delay]Widest spread; one client may retry very early
Equal[delay/2, delay]Keeps a floor, still spreads the herd
Decorrelated[base, previous × 3]Capped; follows the last wait, a strong default
Noneno jitterFine for a single client, dangerous for a fleet

Third defense: circuit breaker

Stop making the call

Even a 5-second timeout is 5 seconds per process; in the open state that drops to 0.

Fail-fast

  1. 01Closedcalls pass, errors are counted
  2. 02Openthe external service isn't called, instant error
  3. 03Half-opena single trial: if it succeeds, closed

Fail-fast beats slow-fail

What you do while the circuit is open

  • Queue itIf the SMS doesn't have to go out right away, leave it to be processed once the circuit closes.
  • Serve stale dataIf the exchange-rate service is down, show the last known rate.
  • Degrade gracefullySaying "Recommendations can't load right now" beats crashing the page.

Share and download

Search the site

Start typing to search posts, projects and pages.

Escto closePowered by Pagefind