Site icon Vinsguru

gRPC – An Introduction Guide

Overview:

In this tutorial, I would like to introduce you to the modern RPC framework from google called gRPC. It is one of the hottest technologies right now!

Need For gRPC:

Microservices are the popular way to design distributed systems. A big monolith application is broken down into multiple independent Microservices. These Microservices have few advantages compared to traditional monolith applications – like easy to deploy, scale, reuse etc. Microservices do not come alone. There will be always more than 1 service. These Microservices communicate with each other mostly with REST using HTTP/1.1 protocol by exchanging JSON.

For example: If an user tries to send a request to order-service to order a product, order-service might internally send multiple requests to other services to fulfill the request. One request from user might trigger multiple internal requests among microservices in an application design.

REST is simple and very easy to use. REST is great for browser. Easy to test our APIs. Developers love this. So we always use REST style for our inter-microservices communication. However it has following issues.

This all can affect the overall performance of our Microservices. REST is good between browser and back-end. But we need something better than REST for inter microservices communication to avoid above mentioned issues.

RPC:

RPC, Remote Procedure Call, is an old mechanism in distributed computing, to execute certain procedure in a remote machine without having to understand the network details. Processes in the same system/different systems which are not sharing the same address space can use RPC for their communication. The call will be made as we would normally invoke a local method call. It follows the client-server model. A client sends a request to the server by invoking a method on the remote server and exchanges messages. RPC provides a well defined interface and type safety.

gRPC:

gRPC is a RPC implementation / framework from Google for inter-microservices communication. Google has been using this for more than 15 years (internal name is Stubby). It is battle tested for more than a decade. Google site shows they have been processing 10 BILLIONS requests / second using gRPC.

gRPC is faster than REST (Checkout this gRPC vs REST Performance Comparison). We achieve this performance gain by switching to gRPC because of these 2 important reasons along with other in-built tools.

gRPC by default uses HTTP/2 for transport and Protocol Buffers for message exchange instead of JSON whereas most of the current microservices architectural style is REST with JSON on top of HTTP/1.1

HTTP/1.1 HTTP/2
Textual format Binary format
Headers are plain text. Not compressed Compressed headers
Single request and response with 1 single TCP connection. Same TCP connection can be reused for multiplexing.
Server streaming
Client streaming
Bi-directional streaming
JSON Protocol Buffers
No strict schema definition / No strict type Strict schema definition & Type safety
Textual Binary
Test format makes serialization / deserialization slower & CPU/Memory intensive Binary format makes serialization/deserialization faster
Manual work to create the classes to serialize & deserialize Auto generated for most of the programming languages

By combining the above HTTP/2 and Protocol Buffers,  gRPC becomes a better choice for inter-microservices communication.

REST gRPC
JSON Protocol Buffers
HTTP/1.1 HTTP/2
Single Request & Response Single Request / Response
Client Streaming
Server Streaming
Bi-Directional Streaming

gRPC Course:

I learnt gRPC + Protobuf in a hard way. But you can learn them quickly on Udemy. Yes, I have created a separate step by step course on Protobuf + gRPC along with Spring Boot integration for the next generation Microservice development. Click here for the special link.



gRPC API Types:

REST by default is unary. We send a request and get the response. But gRPC supports streaming requests and responses along with unary APIs.

Service Definition:

Lets assume that we need to a develop a Calculator application which provides the following functionalities.

We know the functionalities to be implemented on the server side. We also know what to expect from the client and what to respond back. Lets come up with a service contract using Protocol buffers as shown below.

syntax = "proto3";

package calculator;

option java_package = "com.vinsguru.calculator";
option java_multiple_files = true;

message Input {
  int32 number = 1;
}

message Output {
  int64 result = 1;
}

service CalculatorService {
  // unary
  rpc findFactorial(Input) returns (Output) {};
  // server stream
  rpc getAllFactors(Input) returns (stream Output) {};
  // client stream
  rpc sumAll(stream Input) returns (Output) {};
  // bi-directional stream
  rpc findPrime(stream Input) returns (stream Output) {};
}

The above service definition is platform neutral and language neutral.

gRPC – Project Set up:

<dependency>
    <groupId>io.grpc</groupId>
    <artifactId>grpc-netty-shaded</artifactId>
    <version>1.30.2</version>
</dependency>
<dependency>
    <groupId>io.grpc</groupId>
    <artifactId>grpc-protobuf</artifactId>
    <version>1.30.2</version>
</dependency>
<dependency>
    <groupId>io.grpc</groupId>
    <artifactId>grpc-stub</artifactId>
    <version>1.30.2</version>
</dependency>

mvn clean compile

Summary:

In this tutorial, we discussed the need for gRPC, API types, project set up etc. Lets further explore how to implement the Microservice/Client-Server application using gRPC and make use of its various RPC types as part of below articles.

  1. gRPC Unary API In Java – Easy Steps
  2. gRPC Server Streaming API In Java
  3. gRPC Client Streaming API In Java
  4. gRPC Bidirectional Streaming API In Java

The source code is available here.

Happy learning 🙂

Share This:

Exit mobile version