What is gRPC?
Remote Procedure Call, abbreviated as RPC and gRPC is a framework which developed by Google. And Google was using it for decades and in 2015 Google decided to make it open source and since then gRPC is started to be used by other companies too.
Lets dive in and learn what it is, why do we use it and what kind of advantages it has for services?
From gRPC Documentation: “In gRPC, a client application can directly call a method on a server application on a different machine as if it were a local object, making it easier for you to create distributed applications and services.” So gRPC is a way to handle Client and server communication like a REST api but of course it has a big difference.
The main advantage is coming from being binary. Rest api is using HTTP/1 protocol and its JSON OR XML but gRPC is using HTTP/2 protocol and its sending the data as binary.
If you are saying Why that can be an advantage because:
For each integer value we are using one bit but in a string every letter is 1 byte and a byte is 8 bit so when that’s the case sending data as binary creates a big advantage. This approach conserves resources, such as CPU usage, by being more efficient. Another significant advantage arises from gRPC’s streaming capabilities. In Rest api we send a request and it returns response but with gRPC we can make client and server stream or we can send both of them stream together. That increase the performance and supports for streaming.
Protobuf
Grpc uses protobuf(PROtocol BUFfers) as IDL(Interface Declaration Language) and protobufs are extensible mechanism for serializing and deserializing the data. And Protobuf messages are defined in .proto files.
a protobuf file example:
syntax = "proto3";
import "google/protobuf/duration.proto";
import "google/protobuf/timestamp.proto";
message Meeting {
string subject = 1;
google.protobuf.Timestamp start = 2;
google.protobuf.Duration duration = 3;
}
Protobuf is language-neutral and thats why its quite easy to communicate between different services. Even if the server and client parts has written in different software languages. With .proto file, server and client knows what will be coming and this proto type is supporting almost all languages and its quite easy to learn declaration on proto types.
With gRPC we can directly access a server function and use it in Client. And we can do that operation as unary, Server streaming, Client Streaming and bi-directional streaming. These are 4 different type to create relation between Client and Server with gRPC.
gRPC Concepts: Unary, Server Streaming, Client Streaming and Bi-directional Streaming.
Thats the .proto file we will use to understand gRPC Concepts. There is a request, a response and a service which represents which request will be sent and what will be returning for this request:
service HelloService {
rpc SayHello (HelloRequest) returns (HelloResponse);
}
message HelloRequest {
string greeting = 1;
}message HelloResponse {
string reply = 1;
}
Unary: Unary operations involve a single request and response
Server Streaming: In the Server Streaming concept, the request comes from the client in one go, but the response from the server is streamed. This is declared in the .proto file’s service section using the stream keyword, as shown below:
service HelloService {
rpc SayHello (HelloRequest) returns (stream HelloResponse);
}
Client Streaming: In the Client Streaming concept, the request is streamed from the client, but the response is sent in one go. This is declared similarly in the .proto file’s service section using the stream keyword:
service HelloService {
rpc SayHello (stream HelloRequest) returns (HelloResponse);
}
Bi-directional Streaming:
In Bi-directional Streaming, both the request and response are streamed. This is achieved by using the stream keyword for both the request and response in the .proto file’s service section:
service HelloService {
rpc SayHello (stream HelloRequest) returns (stream HelloResponse);
}
So that was the .proto file implementation to show gRPC concepts. Its usable on almost every software language.
So now we have made the explanation of gRPC, Protobuf and gRPC concepts. And we showed an example for .proto file’s declaration for different gRPC concepts. I will be adding an example with .Net so If you want to see the usage of gRPC on a completed project. you can check my Articles.
I hope it was understandable for you all. If you liked the Article do not forget to claps and follow on Medium to hear more from me.