loading...

A gRPC primer from a non-gRPC dev

perigk profile image Periklis Gkolias ・3 min read

I keep hearing about gRPC those days; not sure if the existing trend is soaring or it is a coincidence. In any case, as I have never dug to the concept, I thought it is a good time to do so.

High-level view of gRPC

gRPC is an RPC framework, created by Google. Interestingly enough, the initial "g" stands for "general (purpose)" and not for "google".

So gRPC is an RPC framework that can be used in any kind of situation where an RPC style communication is required.

Usually, we are talking about communications between services.

gRPC takes care of all the boring stuff that is required for communication between services, like defining service interfaces, communication formats, authentication, health checking, etc.

But in order to do so, we need to do some upfront work first.

Wait...What is RPC

I understand you might have encountered some unknown terminologies in the previous paragraph.

So, RPC is a technique (and used to be very popular before RESTful APIs take over) to make service-to-service calls.

In that case service, A calls service B but from a code reader perspective, it looks like this is on the same machine.

As with all high-quality abstractions, RPC does great work at hiding the low-level details that are required to perform network communication.

RPC also has different conventions and semantics from REST. From example, whereas in RPC you can see calls like

POST /addNewProduct

with body

{"company_id": 2}

in REST you would do something like

POST /products

with the same body.

Below is a nice diagram I borrowed from geekstogeeks which shows an RPC client-server communication

operating-system-remote-procedure-call-1.png

Protocol Buffers

Protocol buffers are the most popular entity of the gRPC framework. They are part of the "upfront work we need to do" in order to leverage gRPC.

What they do is to serialize and deserialize data at both ends of the communication. You can declare them in a struct-like format like:

message Point {
  int32 x = 1;
  int32 y = 2;
}

Easy, right?

The best part is that protocol buffers get serialized in binary, which means (amongst other benefits) a smaller size (comparing to text formats like JSON) and faster transmission.

Supported languages

gRPC supports officially most of the main programming languages. By the time of writing this article, those are:

C/C++
C#
Dart
Go
Java
Kotlin/JVM
Node.js
Objective-C
PHP
Python
Ruby

Where do I start

Apart from grpc.io, I struggled to find free resources. If I could pick two those would be:

Building a Basic API with gRPC and Protobuf

10000 Messages in 2.18 seconds with Python and gRPC

If you are willing to pay a few bucks, Stephane Maarek's course will not let you down.

Conclusion

Thank you for reading this short article. I have to say gRPC is a very interesting framework that I recommend you into if you are working with (micro)services.

Posted on by:

perigk profile

Periklis Gkolias

@perigk

In love with Python, but I admire all the stacks that offer solutions without testing my patience. Avid productivityist, great-food worshipper, always-smiling.

Discussion

markdown guide