Protocol Buffers
Protocol Buffers (Protobuf) is Google’s language-neutral binary serialization format. You describe messages in a .proto file, a compiler (protoc or buf) generates classes for C++, Java, Python, Go and many other languages, and those classes encode objects into compact bytes. Each field is identified on the wire by a field number, not by its name, which is what lets services upgrade one at a time. Protobuf is the payload format of gRPC and is common on Kafka topics alongside Avro.
The schema
syntax = "proto3";
package shop.v1;
message Order {
int64 id = 1;
string city = 2;
int32 amount_cents = 3;
optional string coupon = 4; // optional: lets you tell "unset" from ""
repeated Item items = 5;
}
message Item {
string sku = 1;
int32 qty = 2;
}The numbers (= 1, = 2, …) are the contract. Names can be renamed freely in code; numbers can never be changed or reused. Numbers 1–15 take one byte on the wire, so give them to the most frequent fields.
Since protobuf v27 (2024), Google has been moving from syntax = "proto2"/"proto3" to Editions (edition = "2023", then "2024"), which turn the old syntax differences into per-file and per-field feature flags. The wire format does not change.
How the bytes look, and why old readers survive
- A service writes orders with v2, which added coupon = 4. Another service still reads with v1.
- On the wire, every field is a tag (field number + wire type) followed by its value. Field names are never sent.
- The v1 reader decodes the field numbers it knows: 1, 2 and 3.
- Field 4 is unknown to v1. The wire type says how long it is, so the reader skips it and keeps it as an unknown field.
- The one fatal mistake: reusing a field number. Old readers decode the new field with the old meaning. Mark deleted numbers reserved.
There are only a handful of wire types (VARINT, I64, LEN, I32, plus two deprecated group types), and that is all a reader needs to skip a field it has never seen. Unlike Avro, a Protobuf reader does not need the writer’s schema to decode a message.
Rules for changing a .proto
| Change | Safe? | Why |
|---|---|---|
| Add a field with a new number | Yes | Old readers skip it; new readers see the default when it’s missing |
| Delete a field | Yes, if you reserve its number and name |
Prevents accidental reuse |
| Rename a field | Yes for binary, no for JSON | Binary uses numbers; the JSON mapping uses names |
| Change a field’s number | No | It’s a different field to every reader |
| Change a type | Mostly no | A few are compatible (int32 ↔ int64 ↔ uint32 ↔ bool share VARINT), but values can be truncated |
Move a field into or out of a oneof |
Mostly no | Moving a single field into a new oneof is safe; other moves can lose data |
Tools like buf breaking check these rules in CI, and a schema registry can enforce them for Kafka topics.
Common mistakes
- Relying on default values. In proto3, a missing
int32reads as0and a missingstringas"". If “not set” is meaningful, mark the fieldoptionalor use a wrapper type. - Using
required(proto2). It makes removing a field impossible without breaking readers; it was dropped in proto3 for that reason. - Treating Protobuf as a storage format for analytics. It’s row-oriented and not splittable. Land messages, then write Parquet.
- Serializing maps and expecting stable bytes. Map ordering is not guaranteed, so don’t hash or deduplicate on raw Protobuf bytes.
Learning Resources
- protobuf.dev: Language Guide (proto3)
- protobuf.dev: Encoding
- protobuf.dev: Proto Best Practices
- protobuf.dev: Editions overview
- Buf documentation
- Designing Data-Intensive Applications, Chapter 4: Martin Kleppmann
- See also: Avro, JSON