Skip to content

GRPC INTRODUCTION

Shivam edited this page Jan 17, 2022 · 1 revision

Introduction to 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’.

What is Grpc?

gRPC is developed by Google on top of RPC. gRPC uses HTTP/2 under the covers as its transfer protocol and makes use of the benefits of HTTP/2.

gRPC uses Protocol Buffers (Protobuf), Google’s mature open source mechanism for serializing structured data (although it can be used with other data formats such as JSON). Since HTTP/2 relies on the transferred data being binary encoded protobuf plays a very important role for gRPC.

gRPC supports code-generation for many of the most popular programming languages. This can be done as we can easily define services using Protobuf and generate client-side and server-side code based on that definition. In microservices, we may have one service written in Java (the server) and the other one in Python (the client), with gRPC’s code generation we can define our service in Protobuf and generate the stub for both Python and Java easily and we do not have to worry about the contract and the communication between the two application, we can just make use of the generated code and access the methods on server-side as if it was a method defined in client’s codebase. This feature makes gRPC a great option to consider for microservices architecture where polyglot architectures are usually chosen.

grpc

The main benefits of gRPC are:

  • Modern, high-performance, lightweight RPC framework.
  • Contract-first API development, using Protocol Buffers by default, allowing for language agnostic implementations.
  • Tooling available for many languages to generate strongly-typed servers and clients.
  • Supports client, server, and bi-directional streaming calls.
  • Reduced network usage with Protobuf binary serialization.

These benefits make gRPC ideal for:

  • Lightweight microservices where efficiency is critical.
  • Polyglot systems where multiple languages are required for development.
  • Point-to-point real-time services that need to handle streaming requests or responses.

REST API vs gRPC API

REST gRPC
1. REST is the most popular architectural style for building APIs, particularly for web-based applications and microservices-based infrastructures. 1. gRPC is an open-source RPC architecture designed by Google to achieve high-speed communication between microservices. Message transmission can be 7 to 10 times faster.
2. REST API can receive and return messages written in a variety of formats, the most common format used is JSON. 2. gRPC uses the Protobuf (protocol buffers) messaging format, which is a highly-packed, highly-efficient messaging format for serializing structured data.
3. It uses HTTP 1.1 protocol. 3. It uses HTTP 2 protocol.
4. It uses Native Protoc Compiler for code generation. 4. It uses Native Protoc Compiler for code generation.
5. It handles only Client-Requests. 5. It handles Unary Client-Request or Bidirectional/Streaming.
6. Implementation time could be approx. 20 minutes. 6. Implementation time could be approx. 1 hour.

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.

Protobuff

gRPC uses a contract-first approach to API development. Protocol buffers (protobuf) are used as the Interface Definition Language (IDL) by default. The *.proto file contains:

  • The definition of the gRPC service.
  • The messages sent between clients and servers.

Services

Create a concrete service implementation that derives from this base type and implements the logic for the gRPC calls.

Clone this wiki locally