What is gRPC bidirectional streaming? Designing two independent message flows
A gRPC bidirectional streaming RPC lets the client and server each send and receive multiple messages during one call. The two flows proceed independently: messages need not alternate, and each request is not automatically paired with a response. The application must define message meaning and correlation, flow control, and termination.
A gRPC bidirectional streaming RPC lets the client and server each send and receive multiple messages during one call. The two flows proceed independently: messages need not alternate, and each request is not automatically paired with a response. The application must define message meaning and correlation, flow control, and termination.
Declaration and message flow
Add stream to both request and response types in the .proto file:
service Collaboration {
rpc Exchange(stream ClientMessage) returns (stream ServerMessage);
}Once the RPC starts, both sides can read and write. Message order is preserved within each direction, but there is no single global ordering between client-to-server and server-to-client messages. A server may wait for all client messages before responding, or interleave reads and writes. The application contract should state who sends first and when the other side processes a message.
Time ↓
Client Server
│── change(sequence=7) ──────▶│
│◀──── ack(sequence=7) ───────│
│◀──── remote change(seq=12) ─│
│── change(sequence=8) ──────▶│For example, in a collaborative editor, a client might send local changes while the server sends acknowledgments or changes from other participants. This illustrates a message contract. A real service must define document identity, versions, correlation IDs, duplicate handling, and resynchronization after reconnecting. Bidirectional streaming alone does not provide delivery guarantees, durable storage, or automatic replay.
Reading, writing, and flow control
Because a stream can remain open for a long time, avoid designs where one side keeps writing while the other does not read. gRPC flow control regulates streaming data so a fast sender does not overwhelm a slower receiver. The framework manages it by default, but a write call may wait. A returned Write does not mean the message has crossed the network or reached the peer application.
If both sides synchronously write a lot before reading, they can deadlock. Check the chosen language API for how to read and write concurrently, how many concurrent operations it permits, and how to bound queues and handle blocked writes when the receiver is slow. Concurrency models differ by language, so do not generalize one implementation's rules. Bound application queues and message sizes to limit memory growth.
Correlation and reconnecting
Per-direction ordering does not automatically correlate requests with responses across directions. If a result must match a particular request, include an identifier such as request_id or sequence and define how to handle missing, duplicate, and delayed messages. If a session can reconnect, design resynchronization around a last confirmed version or cursor. These are service choices, not guarantees of the gRPC stream.
Half-close, completion, and cancellation
When the client signals that it has no more messages to send, the server's response stream does not necessarily end immediately. A half-close of one sending direction, the end of the peer's stream, and completion of the whole RPC are separate events. Specify who may finish sending, when the server emits its final result, and when the final gRPC status indicates success or failure.
An expired deadline, client cancellation, or I/O error can cancel an RPC. Cancellation does not automatically stop application work on the server or roll back changes already made. Long-running handlers must observe cancellation and clean up; where possible, propagate it to upstream RPCs and other work started for the original call. A retry may not reveal how far the previous call progressed, so operations with side effects need duplicate-protection rules.
Comparing RPC shapes
| Shape | Message flow | Suitable when |
|---|---|---|
| Unary | 1 request → 1 response | A lookup or command ends in one call |
| Server streaming | 1 request → multiple responses | Results for one request arrive over time |
| Client streaming | Multiple requests → 1 response | Many inputs produce one final aggregate result |
| Bidirectional streaming | Both sides send multiple messages | An interactive session needs independent flows |
Consider bidirectional streaming when both sides need to keep sending and each direction must progress independently. Do not choose it only because there is a lot of data; confirm that the service can implement correlation, backpressure, termination, cancellation, and reconnection behavior.
Design checklist
Confirm both `.proto` `stream` declarations match the actual data flow.
Define message IDs, ordering, duplicate and missing-message handling, and resynchronization.
Check the selected language's concurrent read/write rules and flow-control behavior.
Bound message size, application queues, and send rate; decide how to handle slow receivers.
Handle half-close, stream end, final status, deadlines, and cancellation separately.
Ensure long-running work observes cancellation and cleans up downstream work.
Verify connection duration, deadlines, disconnects, and recovery through the production proxy and load balancer.
Sources
[gRPC core concepts and RPC lifecycle](https://grpc.io/docs/what-is-grpc/core-concepts/)
[gRPC flow control guide](https://grpc.io/docs/guides/flow-control/)
[gRPC cancellation guide](https://grpc.io/docs/guides/cancellation/)
[gRPC Go basics tutorial](https://grpc.io/docs/languages/go/basics/)