gRPC - ghdrako/doc_snipets GitHub Wiki

gRPC

RPC google protocol using:

  • PROTO BUFFER - serialisation format to marshal/unmarshall
  • HTTTP/2 - transport protocol

HTTP/2

  • binary frames
  • header compression
  • multiplexing
  • request prioritization
  • server push
  • secure

Ecosystem

Protocol Buffers

You define a protocol buffer message types in .proto file to specify your data structure. Let’s look at the simple car description example below:

syntax = "proto3";

message Car {
    string brand = 1;
    string model = 2;
    Type type = 3;
    float engine_capacity = 4;
    int32 doors = 5;
}

enum Type {
    HATCHBACK = 0;
    SEDAN =1;
    SUV = 2;
    COUPE = 3;
    CONVERTIBLE =4;
}

The current proto3 standard allows us to define scalar value types (int32, double, bool, string…), enumerations and many others.

The unique number at the end of the field definition is used to identify the field in the message binary format. Enumerations start from 0 value. This number should not be changed once your message is in use!

Based on such a file, the protocol buffer compiler will generate the necessary code in the chosen language.

gRPC takes advantage of released in 2015 HTTP/2 Web protocol. Thanks to that, supports multiplexing (client and server can push messages over the same TCP connection in parallel). This significantly reduces latency.

HTTP/2 also supports client, server and bi-directional streaming.

Text-based headers can be compressed decreasing package size.

As a binary protocol is more efficient over the network. Finally, HTTP/2 can be used over TLS encrypted channels (h2 standard) or without any encryption (h2c standard).

gRPC API types

Thanks to HTTP/2 gRPC have 4 types of API:

  • unary – a traditional API (like HTTP REST)
  • server streaming
  • client streaming
  • bi-directional streaming

gRPC has also its limitations. By using ProtocolBuffers the RPC messages are not human-readable. We also cannot directly call a gRPC service from a browser. It’s harder to debug.

Doc: