NashTech Blog

Table of Contents
three people sitting beside table

Google Remote Procedure Call (gRPC) is an open-source remote procedure call framework initially developed by Google. It facilitates communication between different services or systems, allowing them to interact and exchange data regardless of the languages and platforms they are built upon.

What are Remote Procedure Calls?

Remote Procedure Calls (RPCs) mimic local function calls but differ in their execution, as remote function calls usually occur on a separate machine. RPCs enable programs to execute procedures or functions on different address spaces or machines, concealing the complexity of network communication.

Why do we need remote procedure calls?

Remote procedure calls offer a means to run code on a separate machine, proving particularly crucial in large-scale products where a single machine cannot accommodate all necessary code for the complete functioning of the product.

When to use GRPC?

Here are some scenarios where gRPC is particularly useful:

  1. Microservices Architecture:
    • gRPC is well-suited for building microservices-based architectures where services need to communicate with each other. Its efficient communication and small payload size make it suitable for the high-volume and low-latency communication that microservices often require.
  2. Polyglot Environments:
    • gRPC supports multiple programming languages, allowing services written in different languages to communicate seamlessly. This is especially useful in heterogeneous environments where various languages are used to develop different components.
  3. Efficient Serialization:
    • gRPC uses Protocol Buffers for serialization, which is more compact and faster than other serialization formats like JSON. This efficiency is crucial in scenarios where bandwidth is a concern, such as mobile networks or low-latency environments.
  4. Bi-directional Streaming:
    • gRPC supports bi-directional streaming, allowing both the client and server to send a stream of messages to each other. This is useful for scenarios like real-time updates, interactive applications, or when handling large datasets.
  5. Code Generation:
    • gRPC generates client and server code based on the service definition specified in Protocol Buffers. This can help reduce boilerplate code and make it easier to maintain a consistent API across services.
  6. Official HTTP/2 Support:
    • gRPC uses HTTP/2 as its transport protocol, which provides features like multiplexing, header compression, and flow control. This helps in reducing latency and improving performance compared to traditional HTTP/1.x.
  7. Service Contracts and Versioning:
    • The Protocol Buffers schema used by gRPC provides a clear contract for services, making it easier to define and maintain APIs. Additionally, gRPC supports versioning, allowing for the evolution of services without breaking existing clients.
  8. Security Features:
    • gRPC supports features like Transport Layer Security (TLS) for secure communication. This is crucial in scenarios where secure communication between services is a requirement.

gRPC Basics

  • gRPC based on HTTP/2 which allows streaming from both sides in addition to Request/Response.
  • REST works with entities, not methods but gRPC calls the actual methods.
  • gRPC supports four communication styles

Unary – Basically a request – response model.

Client Streaming – The client opens a connection to the server and sends continuous messages to the server.

Server Streaming – The client opens a connection to the server and the server sends continuous messages using the connection.

Bi-directional – Both the client and the server send continuous messages using the connection.

  • It uses ProtoBuf as data format.

Protocol Buffer

Introduction

  • Protobuf Serialization format used by gRPC to serialize the messages between client and server.
  • It is a binary format, so it is not human readable and is extremely efficient and fast.
  • Generates client classes and it provides cross language compatibility.

Protobuf Work flow

The code generated by protocol buffers provides utility methods to retrieve data from files and streams, extract individual values from the data, check if data exists, serialize data back to a file or stream, and other useful functions.

Compiling this .proto file creates a Builder class that you can use to create new instances. You can then deserialize data using the methods protocol buffers creates in other languages, like C++.

Protobuf Usage Flow

There are three steps in using Protobuf:

Define messages and services:
  • Messages and services are defined in a .proto file.
  • It is a simple text file and can have more than one file.
  • Can define multiple services and messages in the same file.
Generate Code:
  • The protoc compiler generates client code in appropriate language.
  • The client code can create new message, populates messages, serialize and deserialize messages.
  • Code in one language can use message serialize in another language.
Use the generated code:
  • The generated code can be used as any another code in the language.
  • Allow access to the message’s fields and to the services.
  • Handles serialization, deserialization and more.

ProtoBuf Message Syntax

Message defines the structure of the message sent between Grpc components.

ProtoBuf Practical Example

Step 1 : Create ProtoBuf file with name employee.proto which contains message format.

Step 2: In program.cs call Employee class which is created automatically using the message format of proto file and assign values to the message fields defined in proto file.

After executing the project a file is created with emp.dat which contains data in binary format.

To fetch the details from binary format file we can deserialize the same as shown below:

Features of gRPC

  • Language agnostic – gRPC employs Google Protocol Buffer internally, enabling interoperability among various programming languages like Java, Python, Go, Dart, C# and more. This allows a Java client to invoke a procedure call, receiving a response from a Python-based server, ensuring seamless language independence.
  • Streamlined Data Compression – Within a microservices architecture, optimizing data transmission across networks is crucial. gRPC, leveraging Google Protocol Buffer, excels in compacting data, eliminating unnecessary information to facilitate swift data transfer.
  • Efficient Serialization and Deserialization – In a microservices environment, rapid serialization and deserialization of data are paramount during numerous network communications. gRPC’s utilization of Google Protocol Buffer ensures expedited data serialization and deserialization processes.
  • User-Friendly – gRPC provides libraries and plugins that automatically generate procedural code, simplifying its usage. For straightforward scenarios, it can mimic local function calls, enhancing ease of implementation.

gRPC Example:

A simple registration of Chat Room:

groom.Proto file:

GRoom Client: Request for chat room from the server.

Groom Server: Provide room number to the requested client.

Output:

Conclusion:

In conclusion, gRPC stands out as a powerful communication framework due to its language-agnostic nature, efficient data compaction, rapid serialization/deserialization capabilities, and user-friendly approach. Its reliance on Google Protocol Buffer facilitates seamless communication among diverse programming languages, optimizing data transfer efficiency in microservices environments. With its ability to generate code and simplify usage, gRPC emerges as a robust solution for building scalable, interoperable, and high-performance distributed systems.

 

Picture of anshurawate48e9c921e

anshurawate48e9c921e

Leave a Comment

Your email address will not be published. Required fields are marked *

Suggested Article

Scroll to Top