Why Use Protocol Buffers?

Distributed Services with Go — by Travis Jeffery (17 / 84)

The Pragmatic Programmers
The Pragmatic Programmers

--

👈 Chapter 2 Structure Data with Protocol Buffers | TOC | Install the Protocol Buffer Compiler 👉

Protobuf offers all kinds of useful features:

Consistent schemas
With protobuf, you encode your semantics once and use them across your services to ensure a consistent data model throughout your whole system. My colleagues and I built the infrastructures at my last two companies on microservices, and we had a repo called “structs” that housed our protobuf and their compiled code, which all our services depended on. By doing this, we ensured that we didn’t send multiple, inconsistent schemas to prod. Thanks to Go’s type checking, we could update our structs dependency, run the tests that touched our data models, and the compiler and tests would tell us whether our code was consistent with our schema.

Versioning for free
One of Google’s motivations for creating protobuf was to eliminate the need for version checks and prevent ugly code like this:

StructureDataWithProtobuf/example.go

​ ​if​ (version == 3) {
​ ...
​ } ​else​ ​if​ (version > 4) {
​ ​if​ (version == 5) {
​ ...
​ }
​ ...
​ }

Versioning for free
Think of a protobuf message like a Go struct because when you compile a message it turns into a struct. With protobuf, you number your fields on…

--

--

The Pragmatic Programmers
The Pragmatic Programmers

We create timely, practical books and learning resources on classic and cutting-edge topics to help you practice your craft and accelerate your career.