TCB: gRPC


gRPC is an open source high performance protocol that is used to have standardized and easy communication between services.

gRPC means Google Remote Procedure Call or Golden Retriever Pancakes, even if in official documentation it says “gRPC Remote Procedure Calls”.
Key benefits are easy defining services, easy scaling, multiple languages and bi-directional streaming and integrated auth.

What is the purpose?

Let’s take an example that we have two services in different programming languages, one is written in Go and another in Python.
Go service is executing its task and it needs to send result data to Python service which needs to run some of its function. In that situation gRPC jumps in with its protocol, which is named proto, it sends a request to Python service to execute some function.
You as a developer don’t need to know Python programming language and how to run it, for that is responsible gRPC, which will execute that function, send data and in some cases receive response data.

Okay, then how gRPC works?

You need to have .proto file with data structured as messages and each message contains a series of name-value pairs called fields.

syntax = "proto3";

package video;

service VideoService {
  rpc StreamVideo(VideoRequest) returns (stream VideoChunk);
}

message VideoRequest {
  string filename = 1;
}

message VideoChunk {
  bytes data = 1;
  int32 chunk_index = 2;
  bool is_last = 3;
}

N.B for Python developers, don’t forget semicolon at the end of line ;)

Once you specified proto file you need to run buffer compiler to get .protoc file.

Command in Python to get *.protoc file

python -m grpc_tools.protoc -I app_path/proto --python_out=app_path/generated --grpc_python_out=app_path/generated app_path/proto/streamer.proto

proto2 vs proto3

As we saw in previous example .proto file starts with syntax = "proto3"; and in some cases you will see proto2.
So what’s the difference?
Even if proto2 is more powerful it is more complicated, that’s why Google made proto3 to simplify and optimize.

Some new things in Proto3 are:

  1. There is no optional and required
  2. Default doesn’t have fixed value
  3. Enum needs to have value 0
  4. Extensions are removed
  5. Map is first-class feature
  6. Contains built-in JSON mapping

One simple example what proto3 brings:

syntax = "proto2";

message User {
  required string name = 1;
  optional int32 age = 2 [default = 18];
}
syntax = "proto3";

message User {
  string name = 1;   // optional 
  int32 age = 2;     // default = 0 (not 18!)
}

Example of bi-directional (streaming)

You can find code sample of bi-directional gRPC on my GitHub https://github.com/branislav-brujic/fast-api-grpc-stream/

Ok, why should I use it?

Probably you’re asking “Ok, why should I use it when I can use simple HTTP/REST?”
That’s true, most of this you can do with REST API, however if you have a couple thousand requests per second, if you have big data that you’re sending and bandwidth and latency are important for you, then REST doesn’t have what to do in those situations.
One important thing in comparison gRPC with REST is standardization, no matter in which language your service is written and if you rewrite it in another language, gRPC code remains same.

Should my project use gRPC?

|-----------------------------------------------|--------------------------|
|           Question                            |    Should I use it?      |
|-----------------------------------------------|--------------------------|
| I have MVP                                    |       False              |
| I have thousand requests per second           |       True               |
| I have only public API                        |       False              |
| I'm sending big data                          |       True               |
| I have small bandwidth                        |       False              |
| I'm limited with time/devs                    |       False              |
|-----------------------------------------------|--------------------------|