🔌 gRPC Intermediate

How does gRPC handle deadlines and timeouts?

Answer

gRPC uses deadlines (absolute timestamps) rather than timeouts (durations) to coordinate time limits across distributed calls. When a client sets a deadline, gRPC encodes it in the grpc-timeout metadata header. Every service in the call chain receives this deadline and must respect it — if the deadline passes, the RPC is automatically cancelled with status DEADLINE_EXCEEDED. Setting a deadline in Go: ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second); defer cancel(); resp, err := stub.GetUser(ctx, req). Deadline propagation is a key advantage of gRPC: when Service A calls Service B with a 5-second deadline, Service B knows how much time remains and can cancel its own downstream calls if insufficient time is left. This prevents cascading slow calls from accumulating. Best practice: always set deadlines on client calls.