What is gRPC and how it works?

What is gRPC and how it works?

gRPC is a way for different software programs to talk to each other over the internet.gRPC is a technology for implementing RPC APIs that uses HTTP 2.0 as its underlying transport protocol. You might expect that gRPC and HTTP would be mutually exclusive, since they are based on opposite conceptual models. gRPC is based on the Remote Procedure Call (RPC) model, in which the addressable entities are procedures, and the data is hidden behind the procedures. HTTP works the inverse way

For example, imagine you are using an app on your phone that helps you find nearby restaurants. The app on your phone is the program making the call, and the program waiting for the call is the server that stores information about all the restaurants. When you open the app and search for restaurants, your phone is using gRPC to ask the server for a list of nearby restaurants. The server then sends that information back to your phone, and the app displays it on the screen for you.

Another example is a streaming service like Netflix. When you watch a movie on your TV, your TV is making a call to the Netflix server, asking for the movie to be streamed to your TV. The server then sends the movie to your TV in small pieces, and your TV puts those pieces together to play the movie.

How is gRPC different from REST API?

You must be familiar with the concept of Representational state transfer (REST) API. REST is a means of retrieving or manipulating a service’s data. A REST API is generally built on the HTTP protocol, using a URI to select a resource and an HTTP verb (e.g., GET, PUT, POST) to select the desired operation. In which we send some data, the data is processed on the backend and a response is returned.

GET /items/9 HTTP/1.1
Accept: application/json

The response will be something like this

HTTP/1.1 200 OK
Content-Type: application/json

{ id: 9, name: "Kiran", sku: "bag", price: { amount: 100, currencyCode: "USD"  }  }

gRPC uses the Protocol Buffers data serialization format, which is known for its efficiency and small payload size. This makes it well-suited for use in high-performance, low-latency environments, such as microservices and mobile applications.

To get started with gRPC, you will need to define your API using the Protocol Buffers language. This involves creating a .proto file that describes the methods and data structures that will be used in your API. Once you have defined your API, you can use the Protocol Buffers compiler to generate the necessary code for your client and server applications.

Example proto file

syntax = "proto3";

package item;

service ItemCatalog {
    rpc GetItemDetails (ItemDetailsRequest) returns (ItemDetailsReply);
}

message ItemDetailsRequest {
    int32 id = 1;
}

message ItemDetailsReply {
    int32 id = 1;
    string name = 2;
    string sku = 3;
    Price price = 4;
}

message Price {
    float amount = 1;
    string currencyCode = 2;
}

Now the difference is clear, let's talk about how does gRPC work?

gRPC working

To get started with gRPC, you will need to define your API using the Protocol Buffers language. This involves creating a .proto file that describes the methods and data structures that will be used in your API. Once you have defined your API, you can use the Protocol Buffers compiler to generate the necessary code for your client and server applications.

On the server-side, gRPC uses a concept called "services" to handle incoming requests. Each service is defined in the .proto file and maps to a specific set of methods that can be called by the client. The server uses a service implementation to handle these requests and return the appropriate response.

On the client-side, gRPC uses a concept called "stubs" to invoke the methods on the server. Each stub corresponds to a specific service, and is generated automatically by the Protocol Buffers compiler. The client uses the stub to make requests to the server, and to receive and parse the responses.

gRPC architecture

The gRPC architecture is based on a client-server model, where the client sends a request to the server and the server sends back a response. The communication between the client and server is done over a standard HTTP/2 connection, which allows for multiplexing of multiple requests and responses over a single connection.

Here's a high-level overview of how the client and server communicate in a gRPC system:

  1. The client creates a request message and sends it to the server using a gRPC stub.

  2. The server receives the request and uses a gRPC service handler to process it.

  3. The server creates a response message and sends it back to the client using the gRPC stub.

  4. The client receives the response and processes it.

The gRPC framework provides a set of APIs for both the client and server to handle the request and response messages, as well as the underlying HTTP/2 connection. The framework also provides automatic serialization and deserialization of the request and response messages using Protocol Buffers, which is a highly efficient and compact data format.

In summary, gRPC is a high-performance, open-source framework for building remote procedure call (RPC) APIs. It uses Protocol Buffers for efficient data serialization, and supports a wide variety of programming languages. It also provides features such as bi-directional streaming and flow control, making it well-suited for use in high-performance, low-latency environments such as microservices and mobile applications.