What is gRPC
Tags: grpc, microservices, protobuf
Published: 2023-03-13 00:34:14

## 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/iz8cl4itnklyk28214o6.png)


## 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:

```proto
syntax = "proto3";

message Example {
	int32 id = 1;
	string name = 2;
}
```

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

This example corresponds with this JSON structure:
```json
{
	"id": ,
	"name":
}
```

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](https://dev.to/koich1/what-are-protocol-buffers-ec9)

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


```proto
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) {};
}
```

```java
// function ExampleService
@Override
public void callExample(ExampleRequest request, StreamObserver<ExampleResponse> responseObserver) {
  Example example = request.getExample();
// do Something
}
```

```typescript

const client = getClient();

const exampleRequest:ExampleRequest = {/** some props **/}
const response = this.client.callExample(exampleRequest);
```

This diagram shows how gRPC works:

![example](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/weqqd87a296r0vhgfji5.png)


## 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3y0t5i9ldhuqzn3on805.png)

```proto
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) {};
}
```
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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3bedoxuc62fwwgu25z0r.png)


```proto
message ExampleServerStreamingRequest {
  Example example = 1;
}

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

```

### 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/i69llqaw3i57v5svcwc1.png)

```proto
message ClientStreamingExampleRequest {
    Example example = 1;
}

message ClientStreamingExampleResponse {
    string result = 1;
}

service ExampleService {
    rpc ClientStreamingExample(stream ClientStreamingExampleRequest) returns (ClientStreamingExampleResponse) {};
}
```

### 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rxgas7nr61qtmehh1nbh.png)

```proto
message ExampleBiDirectionRequest {
    Example example = 1;
}

message ExampleBiDirectionResponse {
    string result = 1;
}
service ExampleService {
	rpc ExampleBiDirection(stream ExampleBiDirectionRequest) returns (stream ExampleBiDirectionResponse) {};
}
```

## gRPC-web
gRPC is mainly used in server-to-server communication, but it can also be used in client-to-server communication. [gRPC-web](https://github.com/grpc/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:
- https://grpc.io/docs/what-is-grpc/introduction/