Protocol buffers are a method of serializing data that can be transmitted over wire or be stored in files. The other formats like JSON and XML are also used for serializing data. Although these platforms have proved themselves to be extremely flexible and effective, one place where they aren't fully optimized is scenarios where the data is to be transmitted between multiple microservices.

This was the challenge that made Google create the ProtoBuf format in 2008. Since then, it's widely been used internally at Google and has been the default data format for the gRPC framework.

Like JSON and XML, the Protobufs are language and platform-neutral. The Protobuf is optimized to be faster than JSON and XML by removing many responsibilities usually done by data formats and making it focus only on the ability to serialize and deserialize data as fast as possible. Another important optimization is regarding how much network bandwidth is being utilized by making the transmitted data as small as possible.

The definition of the data to be serialized is written in configuration files called proto files (.proto). These files will contain the configurations known as messages. The proto files can be compiled to generate the code in the user's programming language.

Key Features

Binary transfer format

The Protobuf is a binary transfer format, meaning the data is transmitted as a binary. This improves the speed of transmission more than the raw string because it takes less space and bandwidth. Since the data is compressed, the CPU usage will also be less.

The only disadvantage is the Protobuf files or data isn't as human-readable as JSON or XML.

Separation of context and data

In JSON and XML, the data and context aren't separate—whereas in Protobuf, it is separate. Consider a JSON example.

{
	"first_name": "Alex",
	"last_name": "Won"
}