top of page

Brushing Up: Protocol Buffers

  • Writer: kendrickumstattd
    kendrickumstattd
  • Nov 20, 2017
  • 2 min read

Updated: Feb 29, 2020


Imagine that you want to send a letter to a friend. Yes, a real letter, not an email. You write your message on a nice card, put the card in the envelope, and seal the envelope. Before you place the letter in the mailbox, you write your friend's name and address, in addition to your name and address in the top left corner.

What happens when your friend receives the envelope? Well, he is probably interested in the name of the sender and the message inside the card. In computer science terms, he wants to know the value of card.sender and card.message. The envelope contains a packet of information transported by a mail truck. In the same way, a protocol buffer is Google's way of packaging information to be transported online.

Step 1: Defining the Protocol Buffer Structure

Using the card-sending example, suppose that Ada Lovelace wants to wish her friend Charles Babbage a happy birthday. When she goes to write the card, she is taking advantage of the predefined structure for cards sent by mail. Simplifying the full structure, a portion of the protocol buffer structure for this example would look like the following:

message Card{

required string sender = 1;

required string message = 2;

}

The fields of "sender" and "message" are said to be required, because, without this information, Charles Babbage will not receive all of the important information.

Step 2: Writing the Card

When Ada writes her card, she is defining the values of the protocol buffer. For this example, Ada would assign the field values as follows:

card.sender = "Ada Lovelace"

card.message = "Happy Birthday!"

Step 3: Reading the Message

Finally, when Charles Babbage receives the envelope, he can understand the information it contains by accessing the fields "sender" and "message" to obtain the values "Ada Lovelace" and "Happy Birthday!" He is easily able to conclude that his friend Ada Lovelace is wishing him well on his birthday.

There is much more to learn about protocol buffers. Whether your expertise lies in C++, C#, Go, Java, or Python, Google has great resources to get you started.

 
 
bottom of page