What are the four types of gRPC service methods (unary, server streaming, client streaming, bidirectional streaming), and when is each appropriate?
Short answer
Unary (single request, single response) is the most common, equivalent to a typical REST call. Server streaming (single request, stream of responses) suits scenarios like subscribing to live updates or paginating a large result set efficiently.
Advertisement
AModel answer
Unary (single request, single response) is the most common, equivalent to a typical REST call. Server streaming (single request, stream of responses) suits scenarios like subscribing to live updates or paginating a large result set efficiently. Client streaming (stream of requests, single response) suits uploading data incrementally, like a large file in chunks. Bidirectional streaming (both sides stream independently and concurrently) suits real-time interactive scenarios like a chat application or live collaborative editing, where either side can send messages at any time.
service DataService {
rpc GetItem (ItemRequest) returns (ItemResponse); // unary
rpc StreamUpdates (SubscribeRequest) returns (stream Update); // server streaming
rpc UploadChunks (stream ChunkRequest) returns (UploadResult); // client streaming
rpc Chat (stream ChatMessage) returns (stream ChatMessage); // bidirectional
}Real-world example
A real-time stock-trading platform uses server streaming for live price updates (one subscription, continuous stream of price ticks) and bidirectional streaming for an order-matching negotiation channel where both client and server need to send messages independently.
Likely follow-ups
- How does .NET's IAsyncEnumerable<T> map naturally to gRPC streaming methods?
- What are the connection and resource lifetime implications of long-lived streaming calls?
Related topics