gRPC
January 5, 2024

References

What is gRPC?

gRPC is a modern open-source high-performance Remote Procedure Call (RPC) framework developed by Google. It can efficiently connect services in and across data centers with pluggable support for load balancing, tracing, health checking and authentication.

Why gRPC over REST?

Protobuf instead of JSON/XML

REST uses JSON for sending and receiving messages. JSON is flexible, text-based and human-readable but it is not fast enough or light-weight enough for transmitting data between systems. gRPC uses Protobuf(protocol buffers) messaging format which is faster and more efficient for data transmission

First-class support for code generation

REST needs third-party tools to generate the code for API calls. gRPC comes with an in-built protoc compiler which provides the code generation features by default.

Built on HTTP 2

REST is built on HTTP 1.1 which uses a request-response model of communication. gRPC uses HTTP2 so that it supports client-response communication and bidirectional streaming.

Works across languages and platforms

Since the code can be generated for any language, it is easy to create micro-services in any language to interact with each other

Communication across languages

4 Types of API in gRPC

  1. Unary
  2. Server Streaming
  3. Client Streaming
  4. Bidirectional

4 Types of API in gRPC

Following is an example of the four types of APIs. You can understand it further when we are implementing the sample.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
service GreetService {
    // Unary
    rpc Greet(GreetRequest) returns (GreetResponse) {};

    // Streaming server
    rpc GreetManyTimes(GreetManyTimesRequest) returns (stream GreetManyTimesResponse) {};

    // Streaming client
    rpc LongGreet(stream LongGreetRequest) returns (LongGreetResponse) {};

    // Bidirectional streaming
    rpc GreetAll(stream GreetAllRequest) returns (stream GreetAllResponse) {};
}