gRPC

Software Development

Empowering Microservices with gRPC's Efficiency

Hello everyone. In this article, we will examine the gRPC library designed for communication between services. We will briefly discuss how efficiency, one of the most important criteria today, is addressed within gRPC and the methods used for data exchange. Let's delve into how gRPC works and briefly explore the differences with the standard API mechanism.

What is gRPC?

gRPC is an open-source library that enables communication between services through remote procedure calls. With remote procedure calls, a method on the server side is executed by a client, and the output is sent back to the client. Communication between the client and server within gRPC is carried out over the HTTP/2 protocol. This allows the utilization of methods introduced by gRPC for increased efficiency.

HTTP/2 protocol

The increasing use of the internet and evolving needs, coupled with the growing size of data, has rendered the HTTP/1 protocol inadequate, necessitating the development of this protocol. HTTP/2 is built on the widely used HTTP/1 protocol today and supports HTTP/1 rules backward compatibly. So, if we were to ask what the difference between them is, let's examine the following:

The HTTP/1 protocol requires a separate connection to be established for each request. To display a web page, a separate connection must be opened, and a request must be made for each necessary file. In contrast, in HTTP/2, only one connection is opened, and all requests are sent over this connection. Therefore, the time spent establishing connections is much shorter.

The communication data format in the HTTP/1 protocol is readable in text form. In other words, communication data consists of characters, and 1 byte is sent for each character. This situation significantly increases the size of the transmitted data. In HTTP/2, the data is converted to binary form, compressed, and segmented into packets. Each packet is assigned a unique identifier. This allows multiple separate requests to be sent simultaneously over a single connection. On the receiving end, data packets are combined based on these identifiers.

gRPC & HTTP/2

In gRPC, as mentioned earlier, communication between services is facilitated using the HTTP/2 protocol. During communication with the HTTP/2 protocol, the "Protocol Buffer" data format developed specifically for gRPC is employed. The data to be sent is converted to binary format based on their types and then sequentially transmitted to the receiver.

Definition and Usage of Data Structures and Service Functions

Firstly, the data formats and functions to be used in gRPC services are defined in "proto" files. We can think of proto files as drafts. Within these files, the functions to be used, along with the parameters to be sent to these functions and the return data, should be specified separately.

The function declarations to be used for the service can be seen in the above image.
  • For the ProductGet function, a message will be sent to the service, and a message will be returned from the service (Unary).
  • For the ProductList function, a message will be sent to the service, and multiple messages will be returned from the service (Server Streaming).
  • For the ProductDeleteRange function, multiple messages will be sent to the service, and a message will be returned from the service (Client Streaming).
  • For the ProductSearch function, multiple messages will be sent to the service, and multiple messages will be returned from the service (Bidirectional Streaming).

In the above image, you can see the data structures that will be used in the defined functions. Each data structure should specify the data types and the order of the data within the structure. The data is then converted to binary format based on the specified data orders.

After preparing our proto file, it's time for the "Protocol Buffer Compiler." All we need to do is run the appropriate Proto Compiler for the language we are using. For example, in the Visual Studio environment, if we are using C#, when we build our project, functions and data classes that we will use for our service, based on the content of our proto file, are provided to us within a class with the same name as the one we gave to our service. All we need to do is code the service functions in the generated service class according to what they are supposed to do.

In summary, gRPC is a framework developed to accelerate communication between services and reduce resource consumption. Google is its most well-known user, as it was initially developed for their own services. Later, it was released as open-source. It appears as a robust solution for data transfer in environments where multiple services communicate with each other.

REST vs gRPC