Protocol buffers, an introduction

Tags
Programming
Tutorial
Published
2018-03-31
Author
Man Parvesh Singh Randhawa

Introduction

Protocol buffers are Google’s language-neutral, platform-neutral, extensible mechanism for serializing structured data. Some advantages are:
  • Cross platform and language independent
  • Backwards-compatible and future-proof
  • Focused. Proto schemas describe your data models.
Protobuf was invented by Google, and is a lot smaller and faster than other alternatives like XML.

Example

message Person { required string name = 1; required int32 id = 2; optional string email = 3; }
While using with Java, this can be used like this:
Person john = Person.newBuilder() .setId(1234) .setName("John Doe") .setEmail("jdoe@example.com") .build();output = new FileOutputStream(args[0]);john.writeTo(output);
Seems easy, right?

Advantages

  • The Builder notation can be used.
  • Less boilerplate code.
  • Easy to understand

Disadvantages

  • Unreadable data
  • When the data model is not suited for a schema.
  • When data from service is directly consumed by the browser, this can be a problem.

Conclusion

Well, this looks interesting and I would like to know more about it. For now, I won’t be diving deeper, but will definitely consider using protobufs for my next project.