DEV Community

Omar Belghaouti
Omar Belghaouti

Posted on

Hands on gRPC

gRPC is becoming the standard to communicate between microservices. It uses HTTP 2.0 (released in 2015) as its underlying transfer protocol which is more faster and efficient compared to HTTP 1.1 that was released at 1997 and still used in the modern days.

And with all that it secures the connections by default with SSL/TLS, so not only performance but also security are included.

Unlike REST APIs which uses json communication which takes a lot of size and latency, it uses what it is called by ProtoBuf (Protocol Buffers) that are way minimal and faster with communications.

For now, gRPC is used a lot in micro services and mobiles, especially for mobiles because it is faster and the transferred data is minimal and can be delivered in minimal time compared to HTTP 1.1. For web it’s still not there yet but there are some workarounds like making some gateways for that matter.

Also one of the cool features about gRPC framework is that it is language agnostic, meaning that it works for any language out there, you just need to write a proto file which has a simple syntax and then let the protoc (proto compiler) handle generating the code for the language you want like C++, Go, Java, … etc. All what you need to care about is the implementation for the services.

There are 4 types of APIs in gRPC, unary, server streaming, client streaming and bi directional streaming. Which holds a new way for communication, especially the streaming ones compared to REST.

In this example, I have created a blogs service that handles all the CRUD operations with MongoDB, it has unary rpc for all the operations and a server streaming to list all the blogs from the database. You can see in action how simple it is, because as I said above you just need to care about the implementation, all other things are generated for you!

To test the example yourself, check this repo

Top comments (0)