+5 votes
190 views
in Know how by (242k points)
reopened
gRPC: Pioneer of Client-Server Communication of the Future

1 Answer

+3 votes
by (1.6m points)
edited
 
Best answer

What is gRPC?
This is how gRPC works: multilingual and efficient thanks to Protocol Buffers and HTTP / 2
The gRPC workflow and first steps with Protocol Buffers
HTTP / 2: optimized streaming
Advantages and disadvantages of gRPC
Comparison between gRPC and REST
In which cases is gRPC used?

image

gRPC: Pioneer of Client-Server Communication of the Future

Network technology continues to advance at breakneck speed. In order to meet the increasingly demanding demands of distributed computing systems, a new system called gRPC has been developed for Remote Procedure Calls or remote procedure calls. The? G? It is from Google, since the company played a decisive role in the development of the system. We explain what gRPC is, how it works and where it is used..

Index
  1. What is gRPC?
  2. This is how gRPC works: multilingual and efficient thanks to Protocol Buffers and HTTP / 2
  3. The gRPC workflow and first steps with Protocol Buffers
  4. HTTP / 2: optimized streaming
  5. Advantages and disadvantages of gRPC
  6. Comparison between gRPC and REST
  7. In which cases is gRPC used?

What is gRPC?

gRPC is a modern remote procedure call system that processes communication in distributed client-server structures especially efficiently thanks to innovative process engineering. It operates at the process level, like its predecessor, the RPC system. A characteristic element of inter-process communication using gRPC is the principle of transparency : collaboration between (partly very) distant instances is so close and simple that no difference is perceived compared to a local communication between internal processes of a machine.

Developed by Google in 2015, today the Cloud Native Computing Foundation is in charge of its distribution and development. gRPC is an open source element , that is, the source code is accessible for other developers to make modifications and participate in its development..

By default, gRPC executes the transport of data flows between remote computers using HTTP / 2 and manages the structure and distribution of the data using the Protocol buffers developed by Google. The latter are saved in the form of plain text files with the extension .proto .

GRPC is often defined as a kind of  framework . One of the outstanding features of frameworks is that they provide developers with a programming framework to save time and work. The standardized structure of gRPC already includes different functions and elements that do not have to be programmed over and over again each time they are needed. The gRPC framework also defines standardized interfaces to access certain sources (eg databases)..

This is how gRPC works: multilingual and efficient thanks to Protocol Buffers and HTTP / 2

Protocol Buffers (Protobuf) fulfill several functions in the gRPC system: they serve as an interface description language (Interface Definition Language, IDL) and describe an interface independently of any language, that is, they are not linked to a programming language specific (eg Java or C). They also define the services to be used and the functions available. For each function, it can be indicated which parameters are sent with a query and what response value to expect.

From a remote perspective, Protocol Buffers serve as the underlying message exchange format that determines the structures, types, and objects of messages. Are the elements that make the client and the server? Understand? and that they function as a functional unit and as efficient as possible, even at a great distance.

GRPC process a request in case of a database query (..? Eg search stock ? For an item in the store) is as follows:

  • Before the search can be carried out, certain preparations must be made. On the server side, first a gRPC service and server are implemented on the basis of Protocol Buffers. The client making the query, for its part, generates the corresponding stub . If available, the client application calls the corresponding function (? Search query stock of an article?) On the stub .
  • The query is then sent to the server over the network. Once the query is received with the help of a suitable service interface, this is when the gRPC server starts the actual search for the product in the database.
  • The server then sends a response to the client's stub which, in turn, passes the response value to the original query instance (eg? Number of articles found?).

Both client-side and server-side applications can be written in different programming languages. gRPC is able to overcome these barriers through interfaces and special translation mechanisms.

For the round-trip transport of data flows between machines (Proto Request and Proto Response), HTTP / 2 is integrated into specific network protocols such as TCP / IP or UDP / IP. The streams transmit compact binary data that arises in the serialization ( marshalling ) typical of remote procedure calls. To process these totally abstract data structures, in the next steps on the server side and on the client side, the streamed content is re-deserialized ( unmarshalling ).

image
Message exchange via gRPC in a client-server structure

The gRPC workflow and first steps with Protocol Buffers

The gRPC workflow is divided into four steps:

  1. Definition of the service contract for inter-process communication: the services to be applied and the basic response types and parameters that can be accessed remotely are determined.
  2. Generation of the GRPC code of the file .proto : special compilers (? Tools command line called protoc) generate the operating code with the corresponding classes for a language desired destination (eg, C ++, Java..) From saved .proto files .
  3. Server implementation in the desired language.
  4. Creation of the stub of the client that calls the service; the queries are then processed between the server and the client.
image
Basic steps of the gRPC workflow

In the case of a database query to determine the stock of an item in a warehouse ( inventory ), the first steps to follow in the current Protocol Buffer syntax (version: proto3) would be the following:

  syntax = "proto3"; package grpc_service; import "google/protobuf/wrappers.proto"; service InventoryService { rpc getItemByName(google.protobuf.StringValue) returns (Items); rpc getItemByID(google.protobuf.StringValue) returns (Item);  rpc addItem(Item) returns (google.protobuf.BoolValue); }   message Items {   string itemDesc = 1;   repeated Item items = 2; }   message Item { string id = 1; string name = 2; string description = 3; }  

In the example, the database query uses? Wrapper libraries? Specials of the gRPC framework that already make relevant translation processes available by default and are imported at startup. In multi-lingual and unequal client-server structures, these libraries allow interfaces in principle incompatible to communicate. This is how they are generated, p. eg, the classes needed to read and write messages.

The following graphic shows how the service definition ( inventory.proto ) regulates the query of a database in a client-server structure:

image
Managing a database query with Protocol Buffers (.proto file)

HTTP / 2: optimized streaming

HTTP / 2 plays a critical role for gRPC, enabling the transfer of compact binary data to bring greater efficiency to the exchange of messages and data. The protocol provides four variants for remote procedure calls:

1. Unary RPCs represent the simplest gRPC pattern, whereby the client sends a single request to the server and, like a normal function call, only gets one response.

Example: rpc SayHello (HelloRequest) responds (HelloResponse)

2. Server streaming RPCs allow a more complex exchange of messages within a single RPC call. First, the client sends a request to the server. Then it receives in response a thread ( stream ) with a long sequence of messages (efficient request for messages within a single RPC call). The client then reads the thread until there are no messages left.

Example: rpc LotsOfReplies (HelloRequest) responds (stream HelloResponse)

3. Client streaming RPCs reverse the process: the client writes a sequence of messages and sends it to the server via stream . Once the client has prepared the messages, it waits for the server to read them and respond to it. In this case, the message request is also done within a single RPC call.

Example: rpc LotsOfGreetings (stream HelloRequest) responds (HelloResponse)

4. Two-way streaming RPCs represent the most complex form of communication, in which both sides send a sequence of messages. Both data streams work independently of each other, so that the client and the server can read and write regardless of the order. In this way, the server can wait for all messages from the client to come in before composing its responses, but it can also alternately write and read messages. Any other combination of read and write processes is also allowed.

For example: rpc BidiHello (stream HelloRequest) responds (stream HelloResponse)

Thanks to nested requests, variants 2 to 4 allow for particularly efficient multiplexing in which several signals are grouped into a single TCP connection and can be transmitted simultaneously over the network. Data traffic blocks are bypassed by the powerful HTTP / 2 protocol.

image
HTTP / 2 streaming variants

Advantages and disadvantages of gRPC

gRPC has many advantages: the technology is easy to apply, as it uses a simple, easy-to-learn IDL when creating .proto files . With it, even outdated programs can be extended with a powerful gRPC interface, so that they can transmit large files significantly faster.

gRPC has been tested multiple times and is easily scalable, so it can also be applied in more complex and extensive communications, eg. For example, in client-server structures interconnected at a global level. Here, HTTP / 2 not only increases efficiency thanks to multiplexing and bidirectional streaming , but also allows headers to be compressed to reduce the data volume of requests and responses on the network significantly.

The highly standardized layered structure of the gRPC framework allows for reduced programming effort. In this way, developers can focus their full attention on applying the methods. If adaptations are required, programmers and system developers can take advantage of free access to the source code.

In addition, Protocol Buffers and Protobuf compilers allow communication that transcends borders: different operating systems, disparate components of client-server frameworks, and different programming languages ​​are no longer a problem. Thus, software written in C can communicate with Java software, for example. Today, Protobuf compilers are available for a large number of languages, eg. Eg for C #, C ++, Go, Objective-C, Java, Python, Node.js, Android Java, Swift, Ruby and PHP.

The flexibility of gRPC represents another advantage. It can also be used for the exchange of data from microservices or mobile applications that share their data with servers. More advantages: the protocol allows the application of modern security technologies. gRPC has built-in authentication and encourages the use of SSL / TLS for authentication and encryption of the exchange.

On the other side of the coin, testing of the gRPC interfaces currently still has a lot of room for improvement. Protobuf encoded gRPC messages are not readable without technical assistance. Therefore, when analyzing data traffic and, above all, in troubleshooting errors ( debugging ), additional instances must be used to transmit the code in a readable way to detect errors. The interconnection of distributed client-server structures implies further disadvantages. When connecting computers over long distances, gRPC is much more exposed than a local system . It depends on a stable and powerful network and that the network, the data traffic, the transmission protocols and the client and server do not become easy prey for hackers . Another downside is that gRPC doesn't support multicasting.

Comparison between gRPC and REST

Thanks to its advantages, gRPC represents a competent alternative to  REST  (Representational State Transfer). REST is especially suitable for simpler services, while gRPC develops its full potential in complex interfaces (APIs). As a general rule, for the exchange of data between applications, REST uses the JSON data format, a text-based format, which places a great burden on network resources.

The gRPC system, on the other hand, is capable of organizing a much more compact data flow thanks to Protocol Buffers and HTTP / 2. In this way, the memory space required for serialization and binarization is reduced by almost 70 percent compared to, for example, the JSON system. In addition, bi-directional streaming , which works without blocking the exchange of data, provides enormous power and speed advantages compared to REST.

Currently, their cooperation with web applications still leaves much to be desired, as they are usually not optimized for the use of log buffers, the? Contract first? Paradigm. characteristic of gRPC for which the Contract First APIs of the gRPC framework are optimized. One of the big problems with working together with web applications is that it is not yet possible to access a gRPC service directly from a browser . REST does not have these disadvantages, since, in contrast to the more modern HTTP / 2 protocol, HTTP is compatible with all browsers, although it is true that this setback can be compensated with an acceptable effort, so that it can be used the same service for a web application and for a mobile application. A viable option in this area is gRPC-Web. GRPC developers continue to work to find more solutions that facilitate gRPC collaboration with web-based tools.

In which cases is gRPC used?

The gRPC system is especially suitable for efficient communication between processes in distributed client-server structures that seek low latency and a high flow of data and messages. gRPC is frequently used in and between remote data centers to connect services or microservices to each other. As gRPC tools are compatible with the most common development languages, they are very often used in multilingual environments .

Speed, efficiency and its multilingual nature favor the use of gRPC in the mobile field and communication with applications. The gRPC regulates, above all, the last leg of distributed data processing, since it connects devices, mobile applications and browsers with backend services .

In addition, powerful streaming over HTTP / 2 predestines it for real-time point-to-point communication . The Internet of things and home automation benefit from this resource-friendly procedure as it increasingly deals with low-income customer communication. Due to its power and since it is easy to develop, this communication technology could play a prominent role in the cloud in the future, thus counteracting the current hegemony of REST. Today gRPC is also used as an alternative to XML (Extensible Markup Language).

Note

XML is a frequently used format for exchanging data or storing information in a structured way.


...