Protocol Buffer

Vrushali Raut
3 min readMar 12, 2019

--

You’ve probably heard of JSON and XML as ways to serialize data. JSON and XML are great for frontend Like them for Back-end server to server communications Protocol buffers match better.

Protocol buffers aka Protobufs mainly used if you want to exchange messages between services. Protocol Buffers is widely used at Google for storing and interchanging all kinds of structured information. it was designed to be smaller and faster than XML.

What are the protocol buffers?

“Protocol Buffers are a way of encoding structured data in an efficient yet extensible format.”

  • Protobufs is Google’s creation in 2008
  • Mostly used for internal protocols
  • Benchmarked with XML
  • Language neutral

How do they work?

Protocol buffer transmission
  • Binary Serialization — they encode or compress data into binary streams which is easy to transfer.
  • Uses a determined schema to encode and decode
    Compiled into many languages.

Sample Schema:-

Each protocol buffer message is a small logical record of information, containing a series of name-value pairs

Let’s define .proto file containing information about a person:

You can specify optional fields, required fields, and repeated fields. You can find more information about writing .proto files in the Protocol Buffer Language Guide.

Advantages of protobufs over XML?

  • Lightweight
  • Take up less space, 3–10 times smaller
  • Faster transmission, 20 to 100 times faster
  • Validation of data structure
  • less ambiguous
  • Easy to modify a schema

For example, let’s say you want to model a person with a name and an email. In XML, you need to do:

XML format

while the corresponding protocol buffer message

protocol buffer format

When this message is encoded to the protocol buffer binary format it would probably be 28 bytes long and take around 100–200 nanoseconds to parse. The XML version is at least 69 bytes if you remove whitespace, and would take around 5,000–10,000 nanoseconds to parse.

Also, manipulating a protocol buffer is much easier.

Sounds like the solution for me!

In the next article, we will step you through creating a simple application that uses protocol buffers. :)

If you have found this useful Click the 💚 below to show your support and share it with other fellow Medium users. stay tuned.

--

--

Vrushali Raut
Vrushali Raut

Written by Vrushali Raut

I’m a Engineer. Ex Spenmo, Gojek, Leftshift . I love to share my experiments, learnings via Blogs. https://twitter.com/vrushaliSRaut

No responses yet