June 3, 2023

Golang / Go Crash Course 11 | Getting started with Protocol Buffers (aka Protobuf) by Google

In this article we are going to take a look at Protocol Buffers. Some of its advantages how it fits into gRPC for a MicroService architecture and then we are going to create an example using Golang.

What is Protocol Buffers?

  • Protocol Buffers (aka protobuf) is a method created by Google for serializing data.
  • It’s an extensible mechanism that is language-neutral and platform-neutral.
  • It is useful in developing programs to communicate with each other over a wire or for storing data.
  • Protobuf allows to define the required data structures using its IDL (in .proto files)
  • Using that IDL as the source we can generate code for multiple languages (Go, Java, Python, C++, C#, etc).
  • Protobuf is used by default to define messages and services in gRPC.
  • gRPC is a mordern Remote Procedure Call (RPC) framework to connect applicationns using HTTP/2 and provides features such as authentication, bidirectional streaming and flow control.

Protocol Buffers and gRPC

This is an example of how protocol buffers fits into gRPC for microservices. In this example, we have a microservice architecture for a hotel booking system. In this case, the booking service (written in Golang) uses a gRPC stub as the client of two gRPC servers: payments and notifications. Both the request and response data structures along with the client and server code are generated by using Protocol Buffers. The Payment Service is written in Java, the Notification Service is written in Python… as we can see here gRPC and Protobuf support a polyglot microservices architecture.

Protocol Buffers | Advantages

Now let’s see some of the advantages of using protocol buffers:

  • Data is binary and efficiently serialized.
  • Data is fully typed.
  • Data is compressed automatically.
  • Faster serialization with less CPU usage than serialization into JSON or XML.
  • Data schema can evolve over time without breaking deployed programs compiled using “old” format.

Who is using Protobuf?

  • Google
  • Netflix
  • Square
  • CoreOS
  • CockroachDB

Are there any tools similar to Protobuf?

  • Apache Thrift
  • Apache Avro
  • MessagePack
  • Microsoft Bond

Install Proto Compiler (protoc) on Ubuntu

The Google protobuf compiler (which is a standalone binary named protoc) needs to be installed somewhere on your $PATH. You can get it by downloading the corresponding file for your system from https://github.com/google/protobuf/releases. (The corresponding file will be named something like protoc-*-.zip.)

In order to be able to use Protocol Buffers first we need to install protoc in our environment from this URL (https://github.com/protocolbuffers/protobuf/releases) and we can download a bundle that includes multiple languages or we can download protobuf and protoc just for the language that we are going to use (C++, C#, Java, …). When you install protoc in your computer (the bundle), then you can generate code for multiple langauges… but for Golang, in order to generate code in Golang we need install an additional library from this URL (https://github.com/golang/protobuf/protoc-gen-go)

Leave a Reply

Your email address will not be published. Required fields are marked *