A brief introduction to Protocol Buffers

Int this tutorial I will give you a brief introduction to Protocol Buffers. Using Protocol Buffers to exchange data can bring great performance

Service-Oriented Architecture has well-deserved reputation developers as a solid approach to easing painful growth by extracting concerns from large applications.

Restful microservices use JSON to communicate over HTTP. Though JSON has many obvious advantages as a data interchange format – it is human-readable, well understood, and typically performs well – it also has its issues.

Where browsers and JavaScript are not consuming the data directly – particularly in the case of internal services – it’s my opinion that structured formats, such as Google’s Protocol Buffers, are a better choice than JSON for encoding data.

credits: codeclimate.com
credits: codeclimate.com
I’ll give you a brief introduction to Protocol Buffers and Protocol Buffers
. . .

What is Protocol Buffers?

First of all, what are Protocol Buffers? The docs say:
“Protocol Buffers are a way of encoding structured data in an efficient yet extensible format.”

Protocol buffers are Google's language-neutral, platform-neutral, extensible mechanism for serializing structured data – think XML, but smaller, faster, and simpler. 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.

A schema for a particular use of protocol buffers associates data types with field names, using integers to identify each field. (The protocol buffer data contains only the numbers, not the field names, providing some bandwidth/storage savings compared with systems that include the field names in the data.)
syntax = "proto2";

message Point {
required int32 x = 1;
required int32 y = 2;
optional string label = 3;

message Line {
required Point start = 1;
required Point end = 2;
optional string label = 3;

message Polyline {
repeated Point point = 1;
optional string label = 2;

The "Point" message defines two mandatory data items, x, and y. The data item label is optional. Each data item has a tag. The tag is defined after the equal sign. For example, x has the tag 1.

The "Line" and "Polyline" messages, which both use Point, demonstrate how composition works in Protocol Buffers. Polyline has a repeated field, which behaves like a vector.

This schema can subsequently be compiled for use by one or more programming languages. Google provides a compiler called protoc which can produce output for C++, Java or Python. Other schema compilers are available from other sources to create language-dependent output for over 20 other languages
. . .

Advantages of Protobuf:

  • Simpler, faster, smaller in size.
  • Schemas Are Awesome – by encoding the semantics of your business objects once in proto format, it helps “ensure that the signal doesn’t get lost between applications and that the boundaries you create enforce your business rules”;
  • Backward Compatibility for Free – JSON doesn’t use number fields whereas Protocol Buffers does, which obviate the need for version checks and avoids the need for “ugly code”, making backward compatibility less of a challenge;
  • Validations and Extensibility – The keywords in Protocol Buffers are powerful, allowing you to encode the shape of your data structure and how the classes will work in each language;
  • Easy Language Interoperability – The variety of languages that are used to implement Protocol Buffers makes “interoperability between polyglot applications in your architecture that much simpler”.
  • RPC support: Server RPC interfaces can be declared as part of protocol files.
. . .


Protocol Buffers offer several compelling advantages over JSON for sending data over the wire between internal services. While not a wholesale replacement for JSON, especially for services that are directly consumed by a web browser.

Protocol Buffers offers very real advantages not only in the ways outlined above, but also typically in terms of speed of encoding and decoding, size of the data on the wire, and more.

"Protobuf protocol to exchange data between services can bring great performance."

Nevertheless, considering that JSON is native to JavaScript engines, Protobuf still managed to be faster.

Also, one important thing that I noticed is that, even though there are not many resources around Protobuf, I was still able to use it in different environments without having a hard time. So I guess I will start using this technology with more frequency now.
. . .

In the next tutorial, we will deep dive into the Protocol Buffers Interface Definition Language and how to define Schema.

Never miss a post from Chris Gregori, when you sign up for Ednsquare.