.proto — Leveling up your server communication

A few weeks ago in a little chit-chat with our technical lead, I heard about a new form of serializing structured data for use in data storage or communications protocols from Google. You may have heard about it — it’s called protocol buffers.

I won’t go into the past to dig up the equivalents of protocol buffers since there seems to be only XML. I don’t even add JSON as an equivalent since it’s only a data format. It’s safe to say you have probably heard of these two.

Protocol buffers have many advantages over XML for serializing structured data.

Protocol buffers: are simpler, 3 to 10 times smaller, 20 to 100 times faster, less ambiguous, generate data access classes that are easier to use programmatically

I have a reason to not like XML, and it is basically my history with SOAP, and love for REST — even if they do not share the same purpose at the end.

But, to make you a little bit more interested, let’s have a look at the difference:

# This is *not* the binary format used on the wire.
# The text format below is just a human-readable representation for debugging and editing
employee {
name: "Jane Doe"
email: "jdoe@example.com"
position: "Mobile Application Developer"
}

In a manner of parsing these two; the binary format of protocol buffer message would take around 100–200 nanoseconds, whereas the XML version would take around 5,000–10,000 nanoseconds to parse.

The example I’m going to use will be in Go and is a “employee info” application which can write and read the basic information of an employee. Each of them has a name, ID, position, e-mail, address, and a phone number.

required: value for the field must be provided, otherwise the message will be considered “uninitialized”, and trying to build an uninitialized message will throw a RuntimeException.

optional: the field may or may not be set. If an optional field value isn't set, a default value is used. For simple types, you can specify your own default value, as we've done for the phone number type in the example.

repeated: the field may be repeated any number of times (including zero). It may be simply called dynamically sized array.

The modifier required is a little tricky to use, see the big red warning in documentation and dive in for more language specs.

That’s it for now, what we have here is Google’s lingua franca for data as they mention.

I’m extremely HYPED for this, and in my next post, I’ll show you how to compile this example as well as show you how I feel about the process!