Logo

dev-resources.site

for different kinds of informations.

What is gRPC

Published at
3/13/2023
Categories
grpc
microservices
protobuf
Author
koich1
Categories
3 categories in total
grpc
open
microservices
open
protobuf
open
Author
6 person written this
koich1
open
What is gRPC

Introduction

gRPC is an open-source framework for high-performance remote procedure calls created by Google. It is commonly used for Server-To-Server communication in Microservice architecture because it allows a server to connect with another server even written in a different programming language.

What is RPC

RPC stands for Remote Procedure Call. It is a protocol that allows a client to call a function on a server and get a response back. In this context, A client can be a server that wants to send a request to another server.

A client takes a gRPC Stub, a kind of instance that provides functions/methods of a server, and calls a function from the instance.

Protobuf

Advantages of gRPC

One advantage is data transportation. gRPC is built on an HTTP/2 protocol that enhances data transfer performance and efficiency.
gRPC also uses Protocol Buffers, a data serialization format, to encode data. Protocol Buffers are smaller in size than JSON, which means that the data transfer is faster.

HTTP/2

HTTP/2 is a new version of the HTTP protocol that allows multiple requests over a single connection. It also enables to send of binary data, which is a more efficient way to send than text data.

Protocol Buffers

Protocol Buffers are also called Protobuf. You can define a data schema with a .proto file:

syntax = "proto3";

message Example {
    int32 id = 1;
    string name = 2;
}
Enter fullscreen mode Exit fullscreen mode

The numbers 1 and 2 is the field number, different from their actual values.

This example corresponds with this JSON structure:

{
    "id": ,
    "name":
}
Enter fullscreen mode Exit fullscreen mode

Protobuf supports multiple language platforms and this feature enables gRPC to let servers written in several languages connect.

I also wrote a post about Protocol Buffers, so if you want to know more about it, please check it out.
What are Protocol Buffers

And this is an example of .proto file for gRPC:

message Example {
  int32 id = 1;
  string name = 2;
  string header = 3;
  string body = 4;
}

message ExampleRequest {
  Example example = 1;
}

message ExampleResponse {
  string result = 1;
}
service ExampleService {
  rpc callExample(ExampleRequest) returns (ExampleResponse) {};
}
Enter fullscreen mode Exit fullscreen mode
// function ExampleService
@Override
public void callExample(ExampleRequest request, StreamObserver<ExampleResponse> responseObserver) {
  Example example = request.getExample();
// do Something
}
Enter fullscreen mode Exit fullscreen mode

const client = getClient();

const exampleRequest:ExampleRequest = {/** some props **/}
const response = this.client.callExample(exampleRequest);
Enter fullscreen mode Exit fullscreen mode

This diagram shows how gRPC works:

example

4 Types of API calls

gRPC has realized several types of API calls:

  • Unary RPC
  • Server streaming RPC
  • Client streaming RPC
  • Bidirectional streaming RPC

Unary API

Unary API is a basic API call that consists of 1 request and 1 response. It is the most common type of API call.
The definition of the API call is as follows:

Unary

message Example {
    int32 id = 1;
    string name = 2;
    string header = 3;
    string body = 4;
}

message ExampleRequest {
  Example example = 1;
}

message ExampleResponse {
  string result = 1;
}
service ExampleService {
  rpc Example(ExampleRequest) returns (ExampleResponse) {};
}
Enter fullscreen mode Exit fullscreen mode

The message keyword is used to define a new message type called Example. The service keyword defines a type of service that is supposed to be used by a client, and the rpc keyword defines a remote procedure call method.

Server streaming RPC

Streaming Server API is an API call that consists of 1 request and multiple responses. The server sends multiple responses to the client. The proto file uses the stream keyword to define a streaming data flow that is multiple data sendings.

Server-Streaming

message ExampleServerStreamingRequest {
  Example example = 1;
}

message ExampleServerStreamingResponse {
  string result = 1;
}
service ExampleService {
  rpc ExampleServerStreaming(ExampleServerStreamingRequest) returns (stream ExampleServerStreamingResponse) {};
}

Enter fullscreen mode Exit fullscreen mode

Client streaming RPC

Streaming Client API is opposite to the Streaming Server API, which allows clients to send multiple requests. The stream keyword is attached to the request in the rpc declaration.

Client-Streamin

message ClientStreamingExampleRequest {
    Example example = 1;
}

message ClientStreamingExampleResponse {
    string result = 1;
}

service ExampleService {
    rpc ClientStreamingExample(stream ClientStreamingExampleRequest) returns (ClientStreamingExampleResponse) {};
}
Enter fullscreen mode Exit fullscreen mode

Bidirectional streaming RPC

Bi-directional Streaming API is an API call that allows both the client and the server to send multiple requests and responses.

Bidirectional-Streaming

message ExampleBiDirectionRequest {
    Example example = 1;
}

message ExampleBiDirectionResponse {
    string result = 1;
}
service ExampleService {
    rpc ExampleBiDirection(stream ExampleBiDirectionRequest) returns (stream ExampleBiDirectionResponse) {};
}
Enter fullscreen mode Exit fullscreen mode

gRPC-web

gRPC is mainly used in server-to-server communication, but it can also be used in client-to-server communication. gRPC-web is a gRPC implementation for web browsers. It is a JavaScript library that allows you to call gRPC services from a web browser. It supports Unary and Streaming Server API calls.

Conclusion

gRPC gives advantages to data transfer performance and efficiency and also enables several types of communication styles.
gRPC is a good choice if your servers require to communicate with other servers frequently.

Reference:

protobuf Article's
30 articles in total
Favicon
Protocol Buffers as a Serialization Format
Favicon
Part 2: Defining the Authentication gRPC Interface
Favicon
Compile Protocol Buffers & gRPC to Typescript with Yarn
Favicon
Use RBAC to protect your gRPC service right on proto definition
Favicon
Gamechanger Protobuf
Favicon
Gamechanger Protobuf
Favicon
RPC Action EP2: Using Protobuf and Creating a Custom Plugin
Favicon
FauxRPC
Favicon
Why should we use Protobuf in Web API as data transfer protocol.
Favicon
JSON vs FlatBuffers vs Protocol Buffers
Favicon
gRPC - Unimplemented Error 12
Favicon
A protoc compiler plugin that generates useful extension code for Kotlin/JVM
Favicon
Reducing flyxc data usage
Favicon
Koinos, Smart Contracts, WASM & Protobuf
Favicon
This Week I Learnt: gRPC & Protobuf
Favicon
Building a gRPC Server with NestJS and Buf: A Comprehensive Showcase
Favicon
Exploring Alternatives: Are There Better Options Than JSON?
Favicon
Creating the Local First Stack
Favicon
Roll your own auth with Rust and Protobuf
Favicon
OCaml, Python and protobuf
Favicon
Introduction to Protocol Buffers
Favicon
Using Protobuf with TypeScript
Favicon
[Typia] I made Protocol Buffer library of TypeScript, easiest in the world
Favicon
Protoc Plugins with Go
Favicon
Using Azure Web PubSub with Protobuf subprotocol in .NET
Favicon
A secret weapon to improve the efficiency of golang development, a community backend service was developed in one day
Favicon
Linting Proto Files With Buf
Favicon
What is gRPC
Favicon
fast framework for binary serialization and deserialization in Java, and has the fewest serialization bytes
Favicon
Protobuf vs Avro for Kafka, what to choose?

Featured ones: