How do you configure the maximum message size for gRPC requests and responses, and why might the defaults need adjustment?
Short answer
gRPC imposes default maximum message sizes (4MB for both send and receive by default in the .NET implementation) to guard against unbounded memory usage from oversized messages -- for legitimate use cases involving larger payloads, these limits can be raised via…
Advertisement
AModel answer
gRPC imposes default maximum message sizes (4MB for both send and receive by default in the .NET implementation) to guard against unbounded memory usage from oversized messages -- for legitimate use cases involving larger payloads, these limits can be raised via MaxReceiveMessageSize and MaxSendMessageSize options on both the server (AddGrpc) and client (channel options) sides, though raising them significantly should be weighed against the same memory-pressure concerns that motivated the default limit.
// Server-side
builder.Services.AddGrpc(options => {
options.MaxReceiveMessageSize = 16 * 1024 * 1024; // 16MB
options.MaxSendMessageSize = 16 * 1024 * 1024;
});
// Client-side
var channel = GrpcChannel.ForAddress("https://localhost:5001", new GrpcChannelOptions {
MaxReceiveMessageSize = 16 * 1024 * 1024
});Real-world example
A data-sync gRPC service raises its default 4MB message size limit to 16MB on both server and client to accommodate occasional larger batch payloads, after initially hitting RESOURCE_EXHAUSTED errors under the unmodified default limit.
Likely follow-ups
- What error is returned when a message exceeds the configured limit?
- Would streaming be a better fit than raising the size limit for very large payloads?
Related topics