21 Nov 2023
There are four types of RPC supported by gRPC:
- Unary RPC: This is the simplest form of RPC where the client sends a single request to the server and receives a single response in return. It is a traditional request-response model.
Client Server
| Request |
+-------------->|
| Response |
|<--------------+
In a Unary RPC, the client sends a single request to the server, represented by the arrow pointing from the client to the server. The server processes the request and sends back a single response to the client, completing the communication.
- Server Streaming RPC: In this type of RPC, the client sends a single request to the server, but the server can stream multiple responses back to the client. The client reads the responses as they arrive.
Client Server
| Request |
+-------------->|
| |
| Response 1 |
| Response 2 |
| Response 3 |
| ... |
| Response N |
|<--------------+
In a Server Streaming RPC, the client sends a single request to the server. The server, however, can send multiple responses back to the client. The responses are streamed one after another, allowing the client to read and process each response as it arrives.
- Client Streaming RPC: Here, the client can stream multiple requests to the server, and the server responds with a single response. This is useful when the client needs to send a large amount of data to the server.
Client Server
| Request 1 |
| Request 2 |
| Request 3 |
| ... |
| Request N |
+-------------->|
| |
| |
| Response |
|<--------------+
In a Client Streaming RPC, the client is allowed to stream multiple requests to the server. The server processes these requests and sends a single response back to the client. This is particularly useful when the client needs to send a large amount of data to the server.
- Bidirectional Streaming RPC: In bidirectional streaming, both the client and server can send a stream of messages to each other. This allows for more interactive communication where both sides can independently send messages.
Client Server
| Request 1 |
| Request 2 |
| Request 3 |
| ... |
| Request N |
+---------------->|
| |
| Response 1 |
| Response 2 |
| Response 3 |
| ... |
| Response N |
|<----------------+
Bidirectional Streaming RPC allows both the client and server to independently send a stream of messages to each other. This bidirectional communication enables interactive and real-time exchanges between the client and server. Messages can be interleaved, and both sides can continue sending messages throughout the communication session.