Lesson 6 of 9 · 26m
Deadlines, Cancellation, and Retries That Do Not Multiply Damage
Propagate deadlines across hops, honor cancellation quickly, and build retry policies only around idempotency and explicit status semantics.
The fastest way to turn a small dependency slowdown into an outage is to let every RPC wait forever and then retry it everywhere. gRPC gives you deadlines and cancellation because distributed systems need a way to stop paying for work nobody wants anymore. Use them.
Deadlines are budgets, not decoration
A deadline is the absolute time by which the whole call must finish. A timeout is a duration at one call site. In a service chain, the budget must shrink as it crosses hops:
client deadline: now + 2000ms
edge service reserve: 250ms
downstream deadline: now + 1500ms
database/query budget: even smallerMature gRPC implementations propagate deadline metadata automatically. Your job is not to reset it thoughtlessly at every layer.
Cancellation is cooperative
When a client cancels or a deadline expires, the server should stop expensive work:
async def GenerateReport(request, context):
for chunk in expensive_scan():
if context.cancelled():
raise RuntimeError("cancelled")
yield chunkUse the language-native cancellation mechanism rather than polling a generic wrapper in real code. The lesson is architectural: check at IO boundaries, queue waits, loops, and before launching more downstream work.
Timeout does not mean failure
DEADLINE_EXCEEDED means the caller stopped waiting. The operation may still have committed. This is why mutating RPCs need idempotency:
message CreateInvoiceRequest {
string invoice_id = 1;
string idempotency_key = 2;
Customer customer = 3;
}The server records the key and returns the original result for a retry. Without that, retrying creation can duplicate charges, invoices, users, or jobs.
Retries belong near the edge, not everywhere
A safe retry policy specifies:
retryable statuses UNAVAILABLE, sometimes ABORTED
maximum attempts usually 2-4 total
backoff exponential with jitter
deadline interaction retries fit inside caller budget
idempotency required for mutationsIf five services each retry three times, one dependency hiccup becomes hundreds of downstream attempts. This is retry amplification. Prefer one retrying layer plus clear ownership.
Hedging and cancellation need care
Hedged requests — sending a duplicate after a latency threshold — can reduce tail latency for read-only calls. They also multiply load. If you hedge, cancel losers quickly and never hedge non-idempotent mutations.
Field notes
- Do not retry
INVALID_ARGUMENT; the same bad input will still be bad. - Retry
UNAVAILABLEcarefully; persistent overload becomes worse under automatic retries. - Record per-attempt timing and final status separately, or dashboards hide amplification.
- Test cancellation during queue wait, mid-stream, and immediately after commit.
- A user-facing timeout is product behavior; an internal deadline is resource protection. They are related but not identical.
Was this lesson helpful?