FAUN — Developer Community 🐾

We help developers learn and grow by keeping them up with what matters. 👉 www.faun.dev

Follow publication

Protocol Buffer -New Data Exchange Format and Explore gRPC -An Implementation of Protocol Buffer

Anshu Mishra
FAUN — Developer Community 🐾
6 min readNov 3, 2019

--

What is protocol buffer and why to use?

Protocol buffers are a flexible, efficient, automated mechanism for serializing structured data. Think about JSON or XML and also think about HTTP,RPC and other client-server protocols. In traditional client server model , client or server will exchange data in text ,JSON or XML format. Protocol buffer is technique to serialize you data in smaller or compressed format and then this smaller and compressed part will travel on internet in faster speed. You can deserialize your data on other end. All serialization will be in binary format.

Now question is why not language specific serialization and they are built in languages like java serialization.Only problem with language specific serialization is you need specific environment on other side to deserialize data like if you want to deserialize java object then you need JRE on other end. Protocol buffer solved this issue. You can serialize your message using Java and then on other end you can use Python to deserialize data.

How to use protocol buffer ?

First of all you need to define your message format in .proto file.

syntax = "proto3";

message Employee {
string name = 1;
int32 age = 2;
string dateOfBirth = 3;
string department = 4;
}

So let say you need to transfer employee data and you need to transfer 4 fields of employee object. You need to create employee message and save the same in .proto file (emp.proto).

First line of proto will be syntax and it will tell complier to generate specific protocol version source.

Second line is your actual message. In message you can use specific data type like string , integer, Enum etc. One thing to notice that each and every field in message has assigned unique sequence number and these numbers will in same sequence in serialize message and then once they deserialize you will get same sequence.

Field numbers in the range 16 through 2047 take two bytes. So you should reserve the numbers 1 through 15 for very frequently occurring message elements. Remember to leave some room for frequently occurring elements that might be added in the future. You also cannot use the numbers 19000 through 19999, as they are reserved for the Protocol Buffers implementation .Protocol buffer compiler will complain if you use one of these reserved numbers in your .proto file.

Message fields can be one of the following:

singular: a well-formed message can have zero or one of this field (but not more than one). And this is the default field rule for proto3 syntax.

repeated: this field can be repeated any number of times (including zero) in a well-formed message. The order of the repeated values will be preserved.

message EmployeeResponse {
repeated Employee employee = 1;
}

message Employee {
string name = 1;
int32 age = 2;
string dateOfBirth = 3;
string department = 4;
}

Importing Definitions

In above example Employee object is defined in same proto file and what if it is defined in different proto file. You can also import proto file inside proto file like following.

import "myproject/other_protos.proto";

What is gRPC?

gRPC is a language-neutral, platform-neutral remote procedure call (RPC) framework and toolset developed at Google. It lets you define a service using Protocol Buffers, a particularly powerful binary serialization toolset and language. It then lets you generate idiomatic client and server stubs from your service definition in a variety of languages.

grpc.io

By default, gRPC uses protocol buffers as the Interface Definition Language (IDL) for describing both the service interface and the structure of the payload messages. gRPC uses http2 specifications like header compression (HPack) and multiplexing.

For more information about http2 and http3 ,please refer below link.

Language supported by gRPC:

C++, Java (incl. support for Android), Objective-C (for iOS), Python, Ruby, Go, C#, Node.js are in GA and follow semantic versioning.

Can I use it in the browser?

Yes you can use with browsers as well but on client side you need deserializer which can deserialize your data. The gRPC-Web is node implementation and can be use on browser clients.

Use case where you can use gRPC:

gRPC is roughly 7 times faster than REST when receiving data & roughly 10 times faster than REST when sending data for this specific payload. This is mainly due to the tight packing of the Protocol Buffers and the use of HTTP/2 by gRPC.

Basically you can use gRPC services for internal micro service communication where you need maximum throughput and want to transfer large amount of data between deployed microservices.

Create first gRPC application using java:

Create a sample Employee gRPC service using protocol buffer.

First we need to create maven project -

Define proto file

Define pom.xml

Now execute mvn compile command , this command will download all dependency and generate java classes in defined package(see below image).

Copy all generated file in source folder and write 2 java classes. One SpringBoot main class and other Employee Service.

Now start your spring boot server and you will see that gRPC will be start on 6565 default port.

Things to notice :

If you notice in service class it is annotated with Grpcservice annotation.

You need to override getEmployee method present in EmployeeServiceImplBase generated class.Overridden method will take Employee pojo as request and then return EmployeeResponse object as data stream.

Now write client to test your application.

Now call client from test application. I have written another test application. You can see all logger

You can see on gRPC server console that employee service has been called from client stub and print log written in service class.

Conclusion: gRPC is faster than rest and other implementation but you need to understand your need before adopting gRPC. If you want to create a browser based application think twice because on gRPC is not enough mature for browser based application. For backend microservices gRPC is best to use as per my opinion.

UseFul resources :

Thanks :)

Follow us on Twitter 🐦 and Facebook 👥 and join our Facebook Group 💬.

To join our community Slack 🗣️ and read our weekly Faun topics 🗞️, click here⬇

If this post was helpful, please click the clap 👏 button below a few times to show your support for the author! ⬇

--

--

Published in FAUN — Developer Community 🐾

We help developers learn and grow by keeping them up with what matters. 👉 www.faun.dev

Written by Anshu Mishra

Solution Architect at TSYS. Technical Enthusiast, Technical blogger. Interest area is cloud native technology , ML and AI.

No responses yet

Write a response