distributedsystem/microservice/grpc

HTTP

Tldr

gRPC TypeClient SendsServer SendsBest Use Case
Unary RPC1 request1 responseSimple API calls, fetching data
Server Streaming1 requestMany responsesReal-time updates, notifications
Client StreamingMany requests1 responseBatch processing, log collection
Bidirectional StreamingMany requestsMany responsesChat, live data feeds, real-time collaboration

gRPC support Different types of RPC communication patterns.

  1. Unary RPC - Simple Request-Response
  2. Server Streaming RPC - One to Many Response
  3. Client Streaming RPC - Many to One Request
  4. Bidirectional Streaming RPC - Many to Many Streaming

Unary RPC

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.
  1. Client sends a gRPC request to the server, opening a stream.
  2. Server reads the incoming messages while keeping the stream open.
  3. Server sends responses back on the same stream, asynchronously.
  4. Client and server can send & receive messages independently (full-duplex).
  5. When either side is done sending, it sends a “half-close” signal.
  6. Once both sides are done, the stream closes and resources are freed.