What is gRPC (Google Remote Procedure Call)

Gizem Cifgüvercin
4 min readJun 5, 2021

Overview

In gRPC, a client application can directly call a method on a server application on a different machine as if it were a local object, making it easier for you to create distributed applications and services.

As in many RPC systems, gRPC is based around the idea of defining a service, specifying the methods that can be called remotely with their parameters and return types.

On the server side, the server implements this interface and runs a gRPC server to handle client calls.

On the client side, the client has a stub that provides the same methods as the server.

How It Works ?

Behind The Scenes

This technology follows an RPC API’ s implementation that uses HTTP 2.0 protocol

gRPC aims to make data transmissions between microservices faster.

Rest versus gRPC

Protocol Buffers

gRPC uses Protocol Buffers, Google’s mature open source mechanism for serializing structured data (to serialize payload data)

The first step when working with protocol buffers is to define the structure for the data you want to serialize in a proto file: this is an ordinary text file with a .proto extension.

QUICK TIP

Protocol buffer data is structured as messages, where each message is a small logical record of information containing a series of name-value pairs called fields.

This solution is lighter since it enables a highly compressed format and reduces the messages’ size

gRPC services

You define gRPC services in ordinary proto files, with RPC method parameters and return types specified as protocol buffer messages

Unary interactions and different types of streaming

gRPC is able to handle unary interactions and different types of streaming:

  1. Unary: when the client sends a single request and receives a single response.
  2. Server-streaming: when the server responds with a stream of messages to a client’s request. Once all the data is sent, the server additionally delivers a status message to complete the process.
  3. Client-streaming: when the client sends a stream of messages and in turn receives a single response message from the server.
  4. Bidirectional-streaming: the two streams (client and server) are independent, meaning that they both can transmit messages in any order. The client is the one who initiates and ends the bidirectional streaming.

Supported Languages

# Let’s create a client and server for implementing RPC architecture

As you can see above we have 2 base projects, I created Client as Console App And Service as .Net 5 App

Creating gRPC Service

1- Add Package

dotnet add package Grpc.AspNetCore

2- Protos

Create proto file (Request, Response, Rpc Service)

3- Build Project

dotnet build

In this step, your definitions in your proto file will be generated and you will be able to use services and objects from C# class.

4- Services

Create Service and Implement from proto base

5- Enable Http 2 in appsettings

6- Add gRPC service and Map

Creating gRPC Client

First, you should add your proto file that you use in server app.

Add packages

Grpc.Net.Client, Google.Protobuf, Grpc.Tools

With this proto file, you have a contract about server functions and objects on client side

After running apps you will see result as below ;

Also we can call service with BloomRPC

You can check my gRPC project whose has client/server examples and proto file on Github.

References

https://grpc.io/

https://imaginarycloud.com/blog/grpc-vs-rest/

--

--