Introduction to gRPC

04 december 2020 om 10:00 by ParTech Media - Post a comment

The world is rapidly moving towards an API dominated world. Inside this world, you will find microservice architecture as the current hot seat. Microservices are a collection of loosely coupled services, where each service is responsible for a feature.

To understand microservices better, consider an e-commerce website. Any user can create an account, add a product to cart, do the payment, and start tracking the order. If microservices are implemented for this, then, account-related activities will be carried out by a separate service, product billing and shipping will be carried out by another service, and so on.

Now, there is a need for the shipping service to communicate with the account service to get the list of stored addresses and other details. Similarly, there will be multiple scenarios where every service has to interact internally with other services to make UI respond to the user's request.

The need of the hour is a procedure to exchange data that aligns with the data format, error patterns, interoperability with multiple languages, etc. Also, it should be easy to implement and should facilitate effective communication between different services to produce better results.

This is where gRPC helps. In this post, let's understand what is gRPC and how does it work.

Table of contents

  1. Basic understanding of RPC
  2. Introduction to gRPC
  3. How does gRPC work?
  4. Practical implementation of gRPC in C#
  5. Conclusion

Basic understanding of RPC

Before we jump into gRPC, let's understand what RPC is.

To build distributed systems, Remote Procedure Call (RPC) can be used. RPC enables the program on one machine to call a different program on another machine without exposing that it is remote. RPC is a way of using existing transport protocols transparently.

What is gRPC

gRPC is a modern, light-weighted, open-source and high-performing Remote Procedure Call system. It works across any environment and was developed by Google in 2015. The first letter of gRPC denotes Google.

It can effectively connect services and supports modern methods such as load balancing, tracing, health checking, and authentication.

gRPC makes use of HTTP/2 for transporting data streams between remote computers. And for processing the data, Google has developed protocol buffers, which are simple text files with extension ‘.proto’.

gRPC is a framework that comes in with the standard structure for development which helps developers to save time and effort.

How does gRPC work?

Protocol buffers play an important role in the gRPC system. It serves as an Interface Definition Language (IDL) that describes the declaration of the interface in a language-independent way. Protocol buffer ensures that services to be consumed and the features provided are language independent and can be consumed by any programming language. Also, you can specify the required input parameters and the response type of the method for the functions that are present.

gRPC has special interfaces and translation mechanisms that allow client and server applications written in different programming languages to interact smoothly.

Protocol buffers also ensure that the client and the server understand each other and operate at optimal efficiency. It serves as the underlying message exchange format that contains the message, structures, types, and objects.

gRPC uses data streams for transporting data back and forth between the client and the server machines. The stream transports the serialized compact binary data over the network. The transmitted data is then deserialized on the other side so that it helps in achieving complete abstraction which can then be processed at the client and the server end.

gRPC workflows

There are four steps involved in the gRPC workflows. Here are they -

  1. Definition of the service contract: In the first step, the communicating service has to be set up with the basic input parameters and return types which can be utilized to call remotely.
  2. Generation of gRPC code: Using the .proto file, gRPC special compilers generate the working code with the required classes and methods for the appropriate target language.
  3. Implementation of server: Taking the preferred programming language of the user, the server-side code will be implemented.
  4. Creation of client stub that is used to call the services.

Practical implementation of gRPC in C#

Here is a practical implementation of gRPC in C# language. To do this, we need to create a gRPC server, gRPC client, and test the gRPC client by calling the server.

Step 1 - Creating gRPC Server

Create new project in Visual Studio.

Choose ‘gRPC service. This will be the gRPC server project in our case.

There will be a greet.proto file pre-defined with few methods.

Step 2 - Creating gRPC Client

Open another instance of Visual Studio, and create a Console application (.Net core based), and this will be our client application.

Once the project is created, create a folder named protos.

Copy the greet.proto from the server (Step 1) solution to this solution.

After copying the file, install the below NuGet packages

  • Grpc.Tools
  • Grpc.Net.Client
  • Google.Protobuf

After installing the above-mentioned packages, go to the program.cs file and modify the existing main function below so that it can be used to call the gRPC server.

using var channel = GrpcChannel.ForAddress("https://localhost:5001");

​      var client = new Greeter.GreeterClient(channel);

​      var reply = await client.SayHelloAsync(

​               new HelloRequest { Name = "PARTECH_GRPC_Client" });

​      Console.WriteLine("Greeting: " + reply.Message);

​      Console.WriteLine("Press any key to exit...");

​      Console.ReadKey();

Once modified, open the .csproj file of the project and add the below line to ItemGroup

<Protobuf Include="Protos\greet.proto" GrpcServices="Client" />

Step 3 - Testing the gRPC client by calling the server

Open the gRPC server solution and run the solution (or) press the F5 button. This will open up a console window saying the server has been hosted and the address is - https://localhost:5001.

Next, open the gRPC client solution, and run the solution (or) press the F5 button. This will open up a console window displaying *Greeting: PARTECH_GRPC_CLIENT*.

If this message is getting displayed, then the client can successfully establish a connection with the server.


gRPC is an effective way of communication in a distributed system that provides low latency and high efficiency in data transfer. It has been used in multilingual microservice architecture where there is a need to connect between the services or to the remote data centers. Its special abilities to stream it via HTTP/2, speed, efficiency makes it suitable for back-end services and mobile applications.