Streaming RPCs
You'll learn to
- -Tell the four RPC types apart: unary, server streaming, client streaming and bidirectional
- -Match each type to a use case, and see the messages interleave in a bidirectional call
- -Explain why closing a stream is its own lifecycle event that both sides must handle
A unary RPC, one request and one response, is what every gRPC example so far has used. But gRPC, built on HTTP/2, supports three streaming modes that REST has no clean equivalent for. A stream lets either side send many messages over one long-lived connection, not exactly one each.
An Analogy: Four Kinds of Conversation
A unary call is asking a question and getting an answer. Server streaming is asking a radio station for the news and then listening to a stream of reports. Client streaming is dictating a long letter that the other person turns into one summary at the end. Bidirectional streaming is a phone call: both people talk and listen at once, and neither waits for the other to finish.
Server Streaming: One Request, Many Responses
service StockService {
rpc WatchPriceUpdates (WatchRequest) returns (stream PriceUpdate);
}The client sends one WatchRequest, and the server keeps the connection open, pushing a PriceUpdate each time the price changes. It suits the same live feed the GraphQL subscriptions chapter covered, but over gRPC's native streaming instead of a WebSocket-based protocol.
Client Streaming: Many Requests, One Response
service UploadService {
rpc UploadTelemetry (stream TelemetryPoint) returns (UploadSummary);
}The client streams many TelemetryPoint messages, such as sensor readings, as they become available. The server processes them as they come and replies with one UploadSummary once the client says it has finished. It fits a client with a large or ongoing volume of data that does not need an answer until the end.
Bidirectional Streaming: Both Sides, Independently
service ChatService {
rpc Chat (stream ChatMessage) returns (stream ChatMessage);
}Both sides stream over the same connection, and the two streams are independent. The server does not wait for the client to finish before it starts replying, and either side sends at its own pace. It is the closest gRPC gets to a true two-way conversation, the natural fit for chat, where messages flow in both directions at once and not in strict turns.
The demo models the four types with Python generators, which are functions that hand back one value at a time. Watch the bidirectional part: each message the client sends is answered before the next one is even produced. The last lines show a client closing a stream early.
Stream Lifecycle
A stream has a lifecycle that a single call does not. It opens, carries zero or more messages over time, and then closes. Either side can close it, and closing means "I am done sending", not necessarily "the whole call is over". In a bidirectional call, one side may finish long before the other. Handling that close signal properly on both sides is what stops a connection leaking when neither side realizes it should have ended.
A stream holds a connection open for its whole lifetime, a very different resource commitment from a unary call. At scale it needs the same connection-management thinking as WebSocket-based GraphQL subscriptions, not just "gRPC handles it automatically".
Interview Signal is part of Pro
See a real weak answer next to a real strong one for this exact topic.
Quiz is part of Pro
Test what you just read with a short quiz, and bank the XP.
Design Stream Engine in the API Design Lab's gRPC & Event-Driven act.
Discussion0
Join the Discussion
Sign in to leave comments, reply to others, or like insights.
No comments yet. Be the first to start the thread!