$ protoc --python_out=. order.proto

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

$ protoc --decode_raw < order.bin
Field numbers on the wire: v2 writer, v1 reader
WRITER · order.proto v2 int64 id = 1;string city = 2;int32 amount_cents = 3;optional string coupon = 4; new READER · order.proto v1 int64 id = 1;string city = 2;int32 amount_cents = 3; BYTES · each field = tag (number + wire type) + value 142 2"NYC" 32500 4"NOV10" v1 knows 1, 2, 3 → id=42 city=NYC amount_cents=2500 unknown 4 → skipped, kept v3 deletes amount_cents and reuses 3 for "int32 discount_cents" v1 now reads the discount as amount_cents: wrong data. Write reserved 3; instead.
  1. A service writes orders with v2, which added coupon = 4. Another service still reads with v1.
  2. On the wire, every field is a tag (field number + wire type) followed by its value. Field names are never sent.
  3. The v1 reader decodes the field numbers it knows: 1, 2 and 3.
  4. 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.
  5. 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 int32 reads as 0 and a missing string as "". If “not set” is meaningful, mark the field optional or 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