gRPC & Streaming
Remote calls as HTTP/2 streams: unary, server streaming, and the head-of-line blocking gRPC inherits from TCP.
An interactive Networking lesson: 18 steps, about 32 minutes, on a live simulation in your browser.
The shop's page is assembled from small services: the cart, the price, the stock count, the user. Each one is a tiny question with a tiny answer, asked thousands of times a second between programs that trust each other.
REST can do this: one HTTP request per question, text JSON back. But every call pays for text headers, text bodies, and a request-response pattern that only ever carries one answer. For service-to-service chatter that is a lot of packaging for very little content.
What you will learn
Many small calls
- Death by a thousand calls: gRPC is remote procedure calls on HTTP/2 streams: function calls in code, numbered frames on the wire.
- The contract comes first: In gRPC the .proto file is the contract: both sides generate their code from it, so drift becomes a compile error.
Unary: one stream
- One call, one stream: A unary call is one HTTP/2 stream: one request out, one response back, multiplexed with everything else on the connection.
- The second call is cheap: New calls on an open HTTP/2 connection cost no handshake: each one is the next odd-numbered stream.
- Binary on the wire: Every gRPC message wears a 5-byte header, 1 flag byte plus 4 length bytes, then that many bytes of protobuf.
Streaming patterns
- Four patterns, one idea: Unary, server, client and bidirectional streaming differ only in who sends how many messages on one stream.
- One request, many responses: Server streaming is one request opening a stream that many responses share, until the server closes it.
- The mirror image, honestly: Client streaming is server streaming with the direction flipped; bidirectional lets both sides send whenever they like.
The honest limitation
- Break it: lose one segment
- TCP knows no streams: gRPC inherits TCP head-of-line blocking: one lost segment stalls every call sharing the connection.
Deadlines and operations
- A call with a deadline: A deadline is a client-side budget: past it, the call is abandoned no matter how close the answer is.
- Status codes and safe retries: The status code decides the retry: UNAVAILABLE may repeat, and only calls safe to run twice actually do.
- Drill: list the services
- Drill: force HTTP/2
- Break it: the server stops
- When gRPC fits: gRPC fits services talking to services. Browsers get REST, grpc-web, or a translating gateway.
Recap & playground
- Cheat sheet
- Playground