gRPC and Protocol Buffers

gRPC and Protocol Buffers#

gRPC is a high-performance, open-source framework for remote procedure calls (RPCs), enabling client applications to invoke methods on server applications as if they were local, even when these applications are deployed on different machines. It is widely adopted for building distributed systems and microservices due to its efficiency, flexibility, and ease of use.

In gRPC, services are divided into a server and a client. The server hosts and implements the service methods defined in the .proto file and manages incoming requests from clients. The client interacts with these remote procedure calls using stubs, which abstract the complexities of network communication. This abstraction allows the client to invoke methods on the remote server as if they were local function calls. The service definitions in the .proto file serve as a contract between the client and server, ensuring both parties agree on the data structures and available methods.

The diagram below illustrates the communication flow between the client and server:

grpc communication graph

As depicted, the server-side functions are accessible through the stub on the client side. The client can invoke these functions as if they were locally implemented, thanks to the stub, which handles the complexities of making requests to the server and receiving responses. The stub simplifies the interaction, allowing the client to focus on the function calls without worrying about the underlying network operations.

Code Generation#

Once the .proto file is defined, it can be processed using the Protocol Buffers compiler, protoc, to generate code for both the client and the server, as well as for the defined data structures. The generated code typically falls into three categories: the service interface, client code, and server code. The service interface is an abstract class or interface that defines the methods corresponding to the RPCs specified in the .proto file. The server code implements this service interface, while the client code, which includes the stub, provides methods for making requests to the server and handling responses. The stub abstracts network communication, enabling the client to use server-implemented methods as if they were local.

Implementing gRPC#

Graph illustrating gRPC implementation

The diagram above provides an overview of the steps involved in implementing gRPC. The process begins with defining the service where all server functions will be implemented. For instance, if you consider the example from the previous chapter, services for the components, such as the clean_data function within the data service, have already been created.

Once the service is defined, the next step is to write the .proto file. This file should outline the functions to be made available as RPC methods, along with their respective inputs and outputs. You will define the Proto messages corresponding to these inputs and outputs, as well as the service with all its functions.

The .proto file is then used to generate code for both the client and server, which will be available in the pb2 and pb2_grpc files. This generated code serves as the foundation for creating the client and server.

With this foundational knowledge of gRPC and Protocol Buffers, you should now be well-prepared to proceed with the example implementation.