How to Implement gRPC Reflection and Deadline Propagation
Enable gRPC server reflection for dynamic clients and propagate deadlines across calls, so latency budgets and discovery work end to end.
On this page
Introduction
gRPC services are defined by protobuf schemas, and a client normally compiles the schema into stubs. Server reflection lets a client fetch the schema at runtime instead, which is how tools like grpcurl and generic clients discover a service without the .proto files. Deadlines are the other half of a robust gRPC setup: a client sets a deadline on a call, and the deadline must propagate across calls so a slow downstream does not consume the whole budget before the outermost caller times out.
What Server Reflection Is
Server reflection is a gRPC service that the server exposes, through which a client can list services and fetch their protobuf descriptors at runtime. A dynamic client uses it to build a request without the compiled stubs, which is what cli tools and generic clients rely on.
It is a discovery aid, not a replacement for the schema. The authoritative schema is still the .proto; reflection makes it available to tools that do not have it. Enable it in development for tooling; consider whether to leave it on in production, where it exposes the API surface to anyone who can reach the port.
// Enable server reflection (grpc-go).
import "google.golang.org/grpc/reflection"
srv := grpc.NewServer()
orders.RegisterOrderServiceServer(srv, &orderServer{})
reflection.Register(srv) // exposes the reflection service
// now grpcurl can describe and call the service without the .proto files:
// grpcurl -plaintext localhost:50051 list
// grpcurl -plaintext localhost:50051 describe orders.OrderService
Using Reflection as a Client
A client that supports reflection queries the server for the descriptor of a service or method, then constructs the request dynamically. This is how a generic tool calls an arbitrary gRPC service without prior knowledge of its schema, and how grpcurl describes one.
For your own typed client, you usually compile the stubs instead; reflection is for tooling, testing and dynamic clients. The value is that you can call a service without its .proto, which helps during development and for exploring an unfamiliar API.
# grpcurl uses server reflection to explore and call a service
grpcurl -plaintext localhost:50051 list # list services
grpcurl -plaintext localhost:50051 list orders.OrderService # list methods
grpcurl -plaintext -d '{"id":"42"}' \
localhost:50051 orders.OrderService/GetOrder # call one
# no .proto files required: the descriptor came from reflection
Reflection in Production
Reflection exposes the service’s schema to any client that can reach the port, which is useful for tooling and an information disclosure otherwise. Decide deliberately: enable it where the network is trusted or the API is public, disable it where it exposes an internal surface to untrusted clients.
An internal service behind authentication may keep reflection on for operators and debugging; a public endpoint may turn it off. Either way, the decision is per service and documented, not a default that happens to be whatever the framework does.
# reflection in production: decide per service
internal service (trusted network) -> on, for operators/tools
public endpoint (untrusted clients) -> off, to avoid schema disclosure
# it exposes the API surface to anyone who can reach the port; be deliberate
Deadlines on a Call
A deadline is an absolute time by which the call must complete. The client sets it (or a timeout, which becomes a deadline), and if the server has not responded by then, the call is cancelled. Without a deadline, a stuck call holds a connection and a caller indefinitely, which is how a slow dependency becomes a hung request.
Every call should have a deadline, and the deadline should be shorter than the caller’s own remaining budget, leaving room for the caller to handle the failure. A deadline longer than the caller’s budget means the caller times out first and never learns the outcome, wasting the downstream work.
// Client sets a deadline on the call.
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
defer cancel()
resp, err := client.GetOrder(ctx, &pb.GetOrderRequest{Id: "42"})
if err != nil { /* DEADLINE_EXCEEDED or other */ }
// ctx carries the deadline; a call without one can hang indefinitely
Propagating the Deadline
When a service handles a call, it should use the incoming context (which carries the deadline) for its own outgoing calls, so the downstream shares the same budget. A handler that starts a fresh context ignores the caller’s deadline and gives the downstream unlimited time, which breaks the budget.
The deadline propagates through the context automatically when you pass it to outgoing calls: the downstream’s deadline is the earlier of the incoming deadline and its own. A downstream that receives an already-expired deadline should fail immediately with DEADLINE_EXCEEDED rather than doing work, because the caller has gone.
// Server handler: reuse the incoming context (with its deadline) downstream.
func (s *orderServer) GetOrder(ctx context.Context, req *pb.GetOrderRequest) (*pb.Order, error) {
// ctx carries the caller's deadline; pass it to the downstream call
user, err := s.users.GetUser(ctx, &users.GetUserRequest{Id: req.UserId})
if err != nil { return nil, err } // deadline propagates automatically
return s.buildOrder(req.Id, user), nil
}
// do NOT use context.Background() here: it discards the caller's deadline
Deadlines, Retries and Cancellation
A retry must fit within the remaining deadline: retrying after the deadline expires does nothing, and a retry with a fresh deadline extends the work beyond what the caller expects. Budget the retries inside the deadline, or make the first attempt fast and fail rather than retrying past the budget.
When the deadline expires, the call is cancelled: the context is done, and the server should stop work it no longer needs to do. A server that ignores cancellation keeps consuming resources for a caller that has gone, which is the cancellation discipline applied to gRPC.
# deadlines + retries
[ ] retries fit inside the remaining deadline (or fail fast instead)
[ ] a retry must not extend past the caller's budget
[ ] on deadline expiry: context done => stop work
# a server that ignores cancellation burns resources for a caller that is gone
Common Pitfalls and Conclusion
Do not call without a deadline, or a stuck call hangs a caller. Do not start a fresh context in a handler, which discards the caller’s deadline. Do not retry past the deadline. Do not leave reflection on by default on an untrusted endpoint. And do not treat a deadline as an error to retry blindly; the caller may have gone.
Server reflection lets a dynamic client discover a gRPC service without its compiled schema, and deadline propagation connects every call in a chain to one budget. Enable reflection for tooling where it does not disclose an internal surface, set a deadline on every call, propagate it through the handler’s outgoing calls, budget retries within it, and stop work when it expires. Then discovery is easy for tools and a slow downstream cannot outlive the caller’s patience.
Sources: gRPC: Server reflection, gRPC: Deadlines, grpc-go: reflection package.
FAQ
- Should server reflection be on in production?
- It depends: it helps operators and tools on a trusted network, but it exposes the API schema to any client that can reach the port. Enable it where that is acceptable, disable it on untrusted public endpoints, and decide per service rather than by default.
- Why does my downstream call time out after the caller has already failed?
- Because the handler started a fresh context instead of using the incoming one, so the downstream did not inherit the caller's deadline. Pass the incoming context to outgoing calls so the deadline propagates and the downstream shares the same budget.
Report a problem with this articleOpens a pre-filled issue on GitHub.