What are the four types of gRPC communication (unary, server streaming, client streaming, bidirectional)?
Learn the four gRPC RPC types: unary, server streaming, client streaming, and bidirectional streaming, with .proto examples and real use cases.
Expected Interview Answer
gRPC supports four RPC types: unary (one request, one response), server streaming (one request, a stream of responses), client streaming (a stream of requests, one response), and bidirectional streaming (independent streams both ways).
You choose the type in the .proto file by adding the stream keyword to the request, the response, both, or neither. Unary is the familiar request-response call, while the streaming variants exploit HTTP/2's full-duplex channel to send many messages over a single RPC — useful for large datasets, uploads, and real-time exchanges like chat or live telemetry.
- Right shape for each use case, from simple calls to live feeds
- Streaming avoids buffering large payloads in memory
- Bidirectional mode supports real-time, interactive protocols
- All four share one efficient HTTP/2 connection
- Declared declaratively in the .proto contract
AI Mentor Explanation
Unary is a single delivery answered by a single shot. Server streaming is one appeal that triggers a long replay of many camera angles from the umpire. Client streaming is a batter facing many balls before one final scoring decision. Bidirectional is live commentary where both commentators talk back and forth continuously throughout the over.
Step-by-Step Explanation
Step 1
Unary RPC
Client sends one request and gets one response, like a normal function call over the network.
Step 2
Server streaming RPC
Client sends one request; the server returns a stream of many messages the client reads sequentially.
Step 3
Client streaming RPC
Client sends a stream of messages, then the server replies once after processing them all.
Step 4
Bidirectional streaming RPC
Both sides send independent message streams over the same connection at the same time.
Step 5
Declare in .proto
Add the stream keyword before the request type, the response type, both, or neither to pick the mode.
What Interviewer Expects
- Naming all four RPC types correctly
- Explaining how the stream keyword selects each mode
- Giving a fitting use case for each type
- Linking streaming to HTTP/2's full-duplex channel
- Knowing bidirectional streams are independent, not lockstep
Common Mistakes
- Forgetting client streaming exists
- Thinking bidirectional means strictly alternating request-response
- Placing the stream keyword on the wrong side in .proto
- Assuming streaming requires a new connection per message
Best Answer (HR Friendly)
“gRPC offers four ways to communicate: a simple one-in one-out call, a call that streams many replies back, a call that streams many messages in and gets one reply, and a fully two-way live stream. You pick the style that fits the task, from a quick lookup to a real-time chat.”
Code Example
service DataService {
// Unary: one request, one response
rpc GetItem (ItemRequest) returns (ItemReply);
// Server streaming: one request, many responses
rpc ListItems (ListRequest) returns (stream ItemReply);
// Client streaming: many requests, one response
rpc UploadItems (stream ItemRequest) returns (UploadSummary);
// Bidirectional streaming: independent streams both ways
rpc Chat (stream ChatMessage) returns (stream ChatMessage);
}Follow-up Questions
- Which HTTP/2 feature makes bidirectional streaming possible?
- When would you choose client streaming over repeated unary calls?
- How do you signal the end of a client stream?
- How does backpressure work in gRPC streaming?
- Can a single service mix all four RPC types?
MCQ Practice
1. Which RPC type sends one request and receives many responses?
Server streaming takes a single request and returns a stream of response messages.
2. How do you make a response a stream in a .proto file?
Placing the stream keyword before the response type declares a server (or bidirectional) streaming RPC.
3. What is true of bidirectional streaming?
In bidirectional streaming, client and server send independent message streams concurrently on the same HTTP/2 connection.
Flash Cards
What is a unary RPC? — One request and one response — a standard network function call.
What is server streaming? — One request, then a stream of many response messages from the server.
What is client streaming? — A stream of request messages from the client, then one server response.
What is bidirectional streaming? — Client and server send independent message streams concurrently over one connection.