This content originally appeared on DEV Community and was authored by Koichi
Introduction
gRPC is an open-source framework for high-performance remote procedure calls created by Google. It is commonly used for Server-To-Server communication in Microservice architecture because it allows a server to connect with another server even written in a different programming language.
What is RPC
RPC stands for Remote Procedure Call. It is a protocol that allows a client to call a function on a server and get a response back. In this context, A client can be a server that wants to send a request to another server.
A client takes a gRPC Stub, a kind of instance that provides functions/methods of a server, and calls a function from the instance.
Advantages of gRPC
One advantage is data transportation. gRPC is built on an HTTP/2 protocol that enhances data transfer performance and efficiency.
gRPC also uses Protocol Buffers, a data serialization format, to encode data. Protocol Buffers are smaller in size than JSON, which means that the data transfer is faster.
HTTP/2
HTTP/2 is a new version of the HTTP protocol that allows multiple requests over a single connection. It also enables to send of binary data, which is a more efficient way to send than text data.
Protocol Buffers
Protocol Buffers are also called Protobuf. You can define a data schema with a .proto
file:
syntax = "proto3";
message Example {
int32 id = 1;
string name = 2;
}
The numbers 1
and 2
is the field number, different from their actual values.
This example corresponds with this JSON structure:
{
"id": ,
"name":
}
Protobuf supports multiple language platforms and this feature enables gRPC to let servers written in several languages connect.
I also wrote a post about Protocol Buffers, so if you want to know more about it, please check it out.
What are Protocol Buffers
And this is an example of .proto
file for gRPC:
message Example {
int32 id = 1;
string name = 2;
string header = 3;
string body = 4;
}
message ExampleRequest {
Example example = 1;
}
message ExampleResponse {
string result = 1;
}
service ExampleService {
rpc callExample(ExampleRequest) returns (ExampleResponse) {};
}
// function ExampleService
@Override
public void callExample(ExampleRequest request, StreamObserver<ExampleResponse> responseObserver) {
Example example = request.getExample();
// do Something
}
const client = getClient();
const exampleRequest:ExampleRequest = {/** some props **/}
const response = this.client.callExample(exampleRequest);
This diagram shows how gRPC works:
4 Types of API calls
gRPC has realized several types of API calls:
- Unary RPC
- Server streaming RPC
- Client streaming RPC
- Bidirectional streaming RPC
Unary API
Unary API is a basic API call that consists of 1 request and 1 response. It is the most common type of API call.
The definition of the API call is as follows:
message Example {
int32 id = 1;
string name = 2;
string header = 3;
string body = 4;
}
message ExampleRequest {
Example example = 1;
}
message ExampleResponse {
string result = 1;
}
service ExampleService {
rpc Example(ExampleRequest) returns (ExampleResponse) {};
}
The message
keyword is used to define a new message type called Example. The service
keyword defines a type of service that is supposed to be used by a client, and the rpc
keyword defines a remote procedure call method.
Server streaming RPC
Streaming Server API is an API call that consists of 1 request and multiple responses. The server sends multiple responses to the client. The proto file uses the stream
keyword to define a streaming data flow that is multiple data sendings.
message ExampleServerStreamingRequest {
Example example = 1;
}
message ExampleServerStreamingResponse {
string result = 1;
}
service ExampleService {
rpc ExampleServerStreaming(ExampleServerStreamingRequest) returns (stream ExampleServerStreamingResponse) {};
}
Client streaming RPC
Streaming Client API is opposite to the Streaming Server API, which allows clients to send multiple requests. The stream
keyword is attached to the request in the rpc
declaration.
message ClientStreamingExampleRequest {
Example example = 1;
}
message ClientStreamingExampleResponse {
string result = 1;
}
service ExampleService {
rpc ClientStreamingExample(stream ClientStreamingExampleRequest) returns (ClientStreamingExampleResponse) {};
}
Bidirectional streaming RPC
Bi-directional Streaming API is an API call that allows both the client and the server to send multiple requests and responses.
message ExampleBiDirectionRequest {
Example example = 1;
}
message ExampleBiDirectionResponse {
string result = 1;
}
service ExampleService {
rpc ExampleBiDirection(stream ExampleBiDirectionRequest) returns (stream ExampleBiDirectionResponse) {};
}
gRPC-web
gRPC is mainly used in server-to-server communication, but it can also be used in client-to-server communication. gRPC-web is a gRPC implementation for web browsers. It is a JavaScript library that allows you to call gRPC services from a web browser. It supports Unary and Streaming Server API calls.
Conclusion
gRPC gives advantages to data transfer performance and efficiency and also enables several types of communication styles.
gRPC is a good choice if your servers require to communicate with other servers frequently.
Reference:
This content originally appeared on DEV Community and was authored by Koichi
Koichi | Sciencx (2023-03-13T00:34:14+00:00) Introduction to gRPC. Retrieved from https://www.scien.cx/2023/03/13/introduction-to-grpc/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.