distributedsystem/microservice/grpc
Tldr
gRPC Type Client Sends Server Sends Best Use Case Unary RPC 1 request 1 response Simple API calls, fetching data Server Streaming 1 request Many responses Real-time updates, notifications Client Streaming Many requests 1 response Batch processing, log collection Bidirectional Streaming Many requests Many responses Chat, live data feeds, real-time collaboration
gRPC support Different types of RPC communication patterns.
- Unary RPC - Simple Request-Response
- Server Streaming RPC - One to Many Response
- Client Streaming RPC - Many to One Request
- Bidirectional Streaming RPC - Many to Many Streaming
Unary RPC
- The client sends a single request to the server.
- The server processes the request and returns a single response.
- Works just like a normal function call in a local program.

service OrderService {
rpc GetOrder (OrderRequest) returns (OrderResponse);
}
message OrderRequest {
int32 order_id = 1;
}
message OrderResponse {
string order_status = 1;
}Server Streaming
- The client sends one request.
- The server responds with a stream of multiple messages instead of a single response.
- The client reads messages one by one until the stream is complete
service OrderService {
rpc ListOrders (OrderListRequest) returns (stream OrderResponse);
}Useful when the server needs to send large amounts of data in chunks, such as:
- Real-time stock price updates
- Log streaming
- Sending large datasets in smaller parts

Client Streaming RPC
- The client streams multiple messages to the server.
- The server processes them and responds with a single message.
service OrderService {
rpc CreateOrders (stream OrderRequest) returns (OrderSummary);
}Best for batch processing, such as:
- Uploading a large number of transactions at once
- Log aggregation, where logs are continuously sent to the server
- Sensor data collection, where the client streams multiple sensor readings

Bidirectional Streaming
- The client streams data to the server.
- The server processes and streams responses back in real time.
- Both streams operate independently.
service ChatService {
rpc Chat (stream ChatMessage) returns (stream ChatMessage);
}
message ChatMessage {
string user = 1;
string message = 2;
}- Live chat applications
- Video conferencing
- Collaborative editing (Google Docs-like functionality)

How it Works ?
Bidirectional streaming in gRPC internally uses HTTP/2 to establish a persistent, full duplex connection between the client and the server.
- Opens an HTTP/2 connection to the server.
- Creates a single HTTP/2 stream for the RPC method.
- Uses multiplexing, meaning multiple requests can be sent and received on the same connection without blocking.
- Client sends a gRPC request to the server, opening a stream.
- Server reads the incoming messages while keeping the stream open.
- Server sends responses back on the same stream, asynchronously.
- Client and server can send & receive messages independently (full-duplex).
- When either side is done sending, it sends a “half-close” signal.
- Once both sides are done, the stream closes and resources are freed.
