Timeout Cascade Control
Set per-hop timeouts tighter than the caller budget so a slow leaf does not hold the root, and propagate cancellation up the chain.
On this page
Introduction
This guide covers cascading timeout control, a recurring concern in the resilience scenario. A single timeout at the edge does not help if a downstream call hangs just inside it; the edge waits the full budget and the user gets a slow failure. Setting per-hop timeouts tighter than the caller budget and propagating cancellation means a slow leaf is cut early and the root fails fast.
Per-hop tighter
Give each hop a timeout smaller than its caller budget so a slow leaf is cut before the root, leaving room for the rest of the chain.
The trade-off worth naming is that give each hop a timeout smaller than its caller budget so a slow leaf is cut before the root, leaving room for the rest of the chain adds a measurable step to the hot path, so measure it before assuming it is free. A reasonable approach is to validate once at the edge, fail closed with a structured error, and log the rejection reason so a spike in bad input is visible on a dashboard rather than silent. Pair the implementation with a test that asserts a valid payload passes and at least one representative violation is rejected with the expected message; this is the cheapest way to keep the guarantee honest as the schema evolves.
// root budget 1000ms; each hop takes a share
async function hop(deadline, fn) {
const remain = deadline - Date.now();
if (remain <= 0) throw new Error('deadline');
return await fn({ signal: AbortSignal.timeout(remain) });
}
Carry a deadline
Pass a deadline through the chain so each hop derives its timeout from the remaining budget, which bounds the total regardless of hop count.
The trade-off worth naming is that pass a deadline through the chain so each hop derives its timeout from the remaining budget, which bounds the total regardless of hop count adds a measurable step to the hot path, so measure it before assuming it is free. A reasonable approach is to validate once at the edge, fail closed with a structured error, and log the rejection reason so a spike in bad input is visible on a dashboard rather than silent. Pair the implementation with a test that asserts a valid payload passes and at least one representative violation is rejected with the expected message; this is the cheapest way to keep the guarantee honest as the schema evolves.
Propagate cancel
On timeout, abort in-flight children so they stop work instead of orphaning, which is the cleanup half of failing fast.
The trade-off worth naming is that on timeout, abort in-flight children so they stop work instead of orphaning, which is the cleanup half of failing fast adds a measurable step to the hot path, so measure it before assuming it is free. A reasonable approach is to validate once at the edge, fail closed with a structured error, and log the rejection reason so a spike in bad input is visible on a dashboard rather than silent. Pair the implementation with a test that asserts a valid payload passes and at least one representative violation is rejected with the expected message; this is the cheapest way to keep the guarantee honest as the schema evolves.
Fail fast at root
When a hop times out, fail the root fast with a useful error, so the user is not held for the full budget by one slow leaf.
The trade-off worth naming is that when a hop times out, fail the root fast with a useful error, so the user is not held for the full budget by one slow leaf adds a measurable step to the hot path, so measure it before assuming it is free. A reasonable approach is to validate once at the edge, fail closed with a structured error, and log the rejection reason so a spike in bad input is visible on a dashboard rather than silent. Pair the implementation with a test that asserts a valid payload passes and at least one representative violation is rejected with the expected message; this is the cheapest way to keep the guarantee honest as the schema evolves.
try { return await hop(deadline, fn); }
catch (e) { if (e.name === 'TimeoutError') throw new Error('upstream slow'); throw e; }
Log the hop
Record which hop timed out so the operator can find the slow leaf, which is the diagnostic value of per-hop timeouts.
The trade-off worth naming is that record which hop timed out so the operator can find the slow leaf, which is the diagnostic value of per-hop timeouts adds a measurable step to the hot path, so measure it before assuming it is free. A reasonable approach is to validate once at the edge, fail closed with a structured error, and log the rejection reason so a spike in bad input is visible on a dashboard rather than silent. Pair the implementation with a test that asserts a valid payload passes and at least one representative violation is rejected with the expected message; this is the cheapest way to keep the guarantee honest as the schema evolves.
Test the cut
Make a leaf hang, assert the root fails before its budget and the leaf is aborted, so the cascade is bounded.
The trade-off worth naming is that make a leaf hang, assert the root fails before its budget and the leaf is aborted, so the cascade is bounded adds a measurable step to the hot path, so measure it before assuming it is free. A reasonable approach is to validate once at the edge, fail closed with a structured error, and log the rejection reason so a spike in bad input is visible on a dashboard rather than silent. Pair the implementation with a test that asserts a valid payload passes and at least one representative violation is rejected with the expected message; this is the cheapest way to keep the guarantee honest as the schema evolves.
Sources
Sources: gRPC deadlines, AbortController.
FAQ
- Deadline or per-call timeout?
- A deadline carried through the call is better, because each hop derives its timeout from the remaining budget, so the total stays bounded regardless of hop count.
- What if a hop ignores cancellation?
- You cannot force it, but you can stop waiting on it; treat the call as failed and move on, so one rude hop does not hold the chain.
Report a problem with this articleOpens a pre-filled issue on GitHub.