Thursday, October 29, 2020

Protocol Buffers (a.k.a Protobuf)

Introduction:

  • Google's mature open source mechanism/protocol to enable serialization and deserialization of structured data between different services.  
  • Method of serializing structured data. It is useful in developing programs to communicate with each other over a wire or for storing data. 
    • gRPC By Default, uses Protocol Buffers, such as IDL (Interface Definition Language) to describe both the service interface and structure of payload messages.
    • gRPC uses HTTP2 as the Transport Protocol, a key reason for gRPC wide adaption.
  • Protocol buffers are Google's Language-Neutral, Platform-Neutral, extensible mechanism for serializing structured data – think XML, but smaller, faster, and simpler
    • I.e. “You define how you want your data to be structured once, then you can use special generated source code to easily write and read your structured data to and from a variety of data streams and using a variety of languages.”
  • Google’s design goal was to 
    • Create a better method than XML to make systems communicate with each other over a wire or for the storage of data. 3 Times Smaller & 10 Times Faster than XML
    • Better than Json in terms of size.
  • Protocol Buffers are 'Language Agnostic. I.e. Code can be generated in many languages
  • Data exchange is in 'Binary format', lowering payload size lower than anything (XML, JSON) making serialization efficient.
Note: 
  • Facebook uses an equivalent protocol called Apache Thrift and 
  • Microsoft uses Microsoft Bond Protocols in addition to a concrete RPC protocol stack used for defined services known as gRPC.
Few more:
  • Data is 'Fully Typed' 
  • Data is 'Compressed Automatically' (Leading to less CPU usage)
  • Data can be read across 'Any Language' (e.g. C#, Java, Go, Python, JS etc)
  • Huge amount of code Code is generated automatically in any language from a simple .proto file.
  • Payload is Binary, thus very efficient to send/receive on a network and serialize/de-serialize on a CPU.
  • Protocol Buffers define rules to make an API evolve without breaking existing clients, which is helpful.
Protocol Buffers is defined by a .proto text file. (HelloWorld.proto)













Protocol Buffers Over XML & JSON:
  • With XML - 3 Times Smaller & 10 Times Faster than XML
  • With JSON - High CPU Intensive - Parsing JSON is CPU Intensive (Bcoz JSON is human readable)











How do Protocol Buffers and JSON Compare?
Protocol Buffers and JSON messages can be used interchangeably; however, they were designed with different goals in mind.

JSON arose out of a subset of the JavaScript programming language. Its messages are exchanged in text (human-readable) format, are completely independent and supported by almost all programming languages.

Protobuf is not only a message format. It is simultaneously a set of rules and tools that define and exchange the messages. It is currently restricted to only some programming languages. In addition, it has more data types than JSON, such as enumerates and methods, and has other functions, including RPC.

gRPC: At the core of gRPC, messages and services are defined using Protocol Buffers.

Happy Blogging!!

Arun Manglick

No comments:

Post a Comment