Search code examples
protocol-buffersgrpchttp2grpc-java

Why does gRPC uses Length Prefixed Messages?


I am trying to understand how does gRPC sends stream messages using HTTP2 ? The link https://github.com/grpc/grpc/blob/master/doc/PROTOCOL-HTTP2.md says,

"The repeated sequence of Length-Prefixed-Message items is delivered in DATA frames"

My understanding from reading this statement is that each (protobuf) message is encapsulated by gRPC in a HTTP2 Data Frame and sent over. If thats so, then why do gRPC need to append Length Prefix to the message, can't it simply read the complete Data Frame Body and assume it to be one complete message ?

What am I missing here ?


Solution

  • The missing part is that there is not guarantee that one DATA frame contains only one gRPC message. gRPC messages is a higher layer stream that works as payload for HTTP/2. It may be implemented in the way you described in the official gRPC libraries, but in terms of protocol it is totally valid to receive some DATA frame with more than one gRPC PDU.