ASP.NET Core › gRPC Services

How would you implement deadline/timeout propagation across a chain of gRPC calls to prevent cascading resource exhaustion when a downstream service is slow?

Advanced

Answer

gRPC's built-in deadline mechanism lets a client specify an absolute deadline for a call (via CallOptions.Deadline), and this deadline is automatically propagated by the framework to any downstream gRPC calls made within that call's context -- ensuring a slow downstream dependency doesn't cause an unbounded chain of calls to all hang indefinitely, instead causing the entire chain to fail fast once the original deadline passes, which is a significant advantage over needing to manually implement timeout propagation with plain HTTP calls.
// Client sets an explicit deadline
var callOptions = new CallOptions(deadline: DateTime.UtcNow.AddSeconds(5));
var reply = await client.SayHelloAsync(request, callOptions);

// If SayHello internally calls another gRPC service using the ServerCallContext's
// deadline, that downstream call automatically inherits the remaining time budget
Real-world example A multi-hop gRPC call chain (gateway to order service to inventory service to pricing service) automatically propagates a single 5-second deadline set at the gateway, causing the entire chain to fail fast together if any downstream service is slow, rather than each hop independently timing out and wasting resources on work whose result will be discarded anyway.

Common follow-ups: How do you handle deadline exceeded errors gracefully at each level of the chain?;What's the difference between a deadline and a cancellation token in this context?