distributedsystem/microservice/grpc

‘Chapter 4. gRPC: Under the Hood’
—Kasun Indrasiri, “gRPC”
Procedure Calls

RPC Flow

RPC works over the network

The client stub creates an HTTP POST request with the encoded message.In gRPC, all requests are HTTP POST requests with content-type prefixed with `application/grpc`. The remote function (/ProductInfo/getProduct) that it invokes is sent as a separate HTTP header.

Why gRPC doesn’t use other methods?

  • HTTP/2 streams handle bi-directional communication, so there’s no need for separate methods.
  • gRPC messages are binary (Protobuf), not form-encoded, so they don’t fit well in a URL query string.
  • HTTP caching (GET) is not applicable because gRPC streams messages.

gRPC does not rely on traditional HTTP request/response, but rather uses HTTP/2 frames:

RFC 7540 - Hypertext Transfer Protocol Version 2 (HTTP/2)

HTTP/2 FramePurpose in gRPC
HEADERSContains metadata (method name, content type, etc.).
DATAContains serialized Protocol Buffers (actual gRPC message).
RST_STREAMUsed to cancel a request.
WINDOW_UPDATEFlow control for large messages.

HTTP/2

In HTTP/2, all communication between a client and server is performed over a single TCP connection that can carry any number of bidirectional flows of bytes. To understand the HTTP/2 process, you should be familiar with the following important terminology:

  • Stream: A bidirectional flow of bytes within an established connection. A stream may carry one or more messages.
  • Frame: The smallest unit of communication in HTTP/2. Each frame contains a frame header, which at a minimum identifies the stream to which the frame belongs.
  • Message: A complete sequence of frames that map to a logical HTTP message that consists of one or more frames. This allows the messages to be multiplexed, by allowing the client and server to break down the message into independent frames, interleave them, and then reassemble them on the other side.