Back to blog

A downstream call hangs: how long do you wait before giving up?

Thắng Đoàn
Thắng Đoàn

FotoFlow's payment provider gets slow: answers in 30 seconds instead of 50 milliseconds. The order service calls it and simply waits, no limit. Each waiting call holds a thread, and within a minute all 200 are stuck. Pages that have nothing to do with payments also stop, because no threads are left for anything. Trouble spreads upstream through anything that waits without a limit. That is a cascading failure, and the slowness of one dependency became an outage of everything.

Give up in time, degrade on purpose

Every outgoing call gets a timeout: if payments has not answered in 800 milliseconds, give up and free the thread. Then degrade gracefully: accept the order as payment pending and charge it from a queue shortly after. The order service stays up for everything else.

Set timeouts from measured latency, a bit above the dependency's p99, and always shorter than your own caller's timeout, or you just move the waiting.

Retry the blips, not the storm

Many failures are momentary: a dropped packet, a server restarting. One retry a moment later fixes most of them. But retry the wrong things: a 400 Bad Request will fail identically forever. Retry timeouts and 503s only.

Then payments becomes genuinely overloaded, and every caller retries three times immediately. Each failed request becomes four, the struggling service gets 4x its normal load, and the retries themselves keep it down. Retries multiply across layers too: 3 retries at each of 3 layers is up to 64 calls for one click. Your own clients have become the denial-of-service.

The fix is patience with randomness: exponential backoff waits 100 ms, then 200, then 400, and jitter spreads thousands of clients across the wait so they do not strike in the same instant. Cap the attempts, and cap the share of traffic that may be retries, about 10%, so the dependency gets room to recover. Most cloud SDKs already do backoff with jitter; the bug is usually layers stacking retries on top of each other.

Stop calling the dead

Even with timeouts, calling a dead service wastes 800 ms to learn nothing. A circuit breaker watches failures, and past a threshold it opens: calls fail instantly without trying. Every 30 seconds it lets one trial call through, half-open; success closes it again and traffic returns gradually, not as a flood.

Resilience libraries and service meshes provide all of this off the shelf. The parts that need thought are the two below.

What this does not solve

Payments charges the card, but the response is lost on the way back. The order service times out, retries, and the customer pays twice. The fix is an idempotency key: a unique id per order sent with every attempt. Payments remembers the result per key and returns it again for duplicates instead of charging again. Any operation that may be retried must be idempotent: doing it twice has the same effect as doing it once.

The last piece is the budget itself. The browser gives up after 2 seconds; if the order service has already spent 1.8, starting 800 ms of payment work is pointless, nobody will read the answer. A deadline travels with the request, and each service passes the remaining time on, stopping when there is none. And bulkheads split the thread pools: 40 threads for payments, 40 for inventory, the rest for everything, so one slow dependency fills only its own compartment. Timeouts limit how long a call can hurt you; bulkheads limit how many calls can.

Your payment provider answers in 30 seconds. How long does your order service wait, and what else could have used those threads?
Three layers each retry three times. How many calls does one user click become when the bottom layer dies?

The hang is contained. Next in the series: Everything is slow and nobody knows why: what do you measure?

Sources

AWS: Exponential Backoff and Jitter

Google SRE Book

gRPC: deadlines

Share:

Recommended for you

Enjoyed this article?

Subscribe for new articles. No spam. Unsubscribe anytime.

No pasting. This is the challenge.0/200

Typed live, never pasted.