Introduction to gRPC

Nuwanga Herath
4 min readNov 24, 2020

What is gRPC?

gRPC (gRPC Remote Procedure Calls) is an open-source high-performance remote procedure call (RPC) framework initially developed at Google in 2015. It can efficiently connect services in and across data centres with pluggable support for load balancing, tracing, health checking and authentication.

Overview of gRPC

Using gRPC, a client application can directly call methods on a remote server application as if it were a local object. Simple idea is that we can define services and methods which can be remotely called with their parameters and their return types. On the server-side, the server implements those interfaces and runs to handle client calls. On the client-side, the clients have stubs that provide the same methods as the server. Let’s have a look at the diagrams below to get a better idea.

Ability to run and talk in a variety of environments

The most effective and unique feature is that the gRPC client and server can run and talk to each other in a variety of environments. The idea is we can write the server and the client in any of gRPC’s supported languages. For example, you can create a gRPC server in Java and clients in Java, Go, Python or Ruby.

Now you have got a basic idea about the gRPC and its key features. Let’s move to the service definition part which is the very first step of the implementation.

Service Definition

In gRPC, you define services in a .proto file which can be compiled using Protocol Buffer Compiler to generate data access classes in any preferred languages. When defining services you have to define the method parameters and return types as messages. You can see a sample definition of services and messages in a .proto file.

Don’t be confused with the syntax of the proto file at this moment. If you want to get familiar with the fundamentals of the protocol buffer, please follow the this https://developers.google.com/protocol-buffers/docs/overview.

I am willing to discuss more on the implementation in the next article. so here I try to give basics of the gRPC which will help beginners to have fundamental conceptual knowledge to initialize a project.

Service Methods

let's go through the overview of the service method types of gRPC. There are 4 types of RPC in gRPC,

  1. Simple RPC

In Simple RPC, a client sends a simple request to a remote server and the server returns a simple response message to the client.

Defining Simple RPC

rpc orderPizza(Order) returns(Delivery);

2. Server streaming RPC

In Server streaming RPC, the server returns a stream of messages in the response for one client request.

Defining Server streaming RPC

rpc orderPizza(Order) returns(Stream Delivery);

3. Client streaming RPC

In Client streaming RPC, the client sends a stream of messages on request and the server returns a single response message. Typically, the server returns the response after receiving all the client messages.

Defining Client streaming RPC

rpc orderPizza(Stream Order) returns(Delivery);

4. Bi-directional streaming RPC

As you already know about the server and client streaming, this is the combination of both them. The server and client send streams of messages as request and response asynchronously. As the two streams are independent, Read and write of messages can be executed in any order. As an example, the server can return streams of messages on the response after all the streams of messages on client request received, or server can return stream of response messages at the same time of receiving the stream of request messages.

Defining Bidirectional RPC

rpc orderPizza(Stream Order) returns(Stream Delivery);

Now you have got basics of the gRPC, knowledge on defining services and messages and knowledge on service method types of gRPC. In the next article, I will be going through a simple demo implementation of gRPC using all 4 service method types.

Thank you.

--

--

Nuwanga Herath

Former Software Engineering Intern @ WSO2, Computer Science and Engineering Undergraduate @ University of Moratuwa, Sri Lanka