Protobuf: schema compatibility rules you must not break
In Protobuf a field is recognised by its number, not its name, so the number is part of the contract: it is never changed or reused. A new field gets a new number and a removed one is closed with reserved. Otherwise old and new clients read the same bytes differently, often with no error. Compatibility is checked automatically, for example with buf breaking in CI.
API · gRPC
A field was removed from the .proto, and a month later its number was given to a new one. What breaks?
A service release goes through, the tests are green, and in production some clients suddenly see a discount of 30 where there never was one. No errors in the logs. The reason is that two messages of different versions are compatible at the byte level but mean different things. The lesson explains why a field number is part of the contract in Protobuf, which changes are safe and how to check them automatically.
How it works and why it is designed so
In a binary message a field is recognised by its number and encoding type, not by its name: the name is absent from the bytes altogether, which is what keeps the format compact. So clients and servers of different versions that work at the same time rely on numbers alone. Compatibility has to hold both ways: backward (new servers read old messages) and forward (old ones read new). Modern implementations keep unknown fields and pass them on instead of dropping them.
Changes in a .proto: safe and not
Breaks compatibility
Change a field number or give a removed field's number to a new one. Change a type incompatibly. Remove a field without reserved.
Safe
A new field with a new number. A new enum value, a new method or service. Removed numbers and names closed with reserved.
Worked example: a number given away a second time
Before: field 3 meant a discount percentage. It was removed without closing the number:
// v1
message Cart {
string id = 1;
int32 discount_percent = 3;
}
// v2: the field is gone, but the number 3 is free
message Cart {
string id = 1;
}
A month later a new field was added to the same message, taking the "free" number:
// v3
message Cart {
string id = 1;
int32 max_items = 3;
}
An old client that has not updated yet sends discount_percent = 30 under number 3 with the same encoding type. The new server reads it as max_items = 30, and in the other direction the old client shows the max_items it gets from the new server as a 30% discount. There is no error: the type matches, the bytes are valid. Such a mismatch shows up only in behaviour. The fix has two steps: never reuse a number, and close the removed field with reserved so the compiler refuses to let anyone take it:
message Cart {
reserved 3;
reserved "discount_percent";
string id = 1;
int32 max_items = 4;
}
An automatic check makes the mistake impossible: in CI buf breaking --against '.git#branch=main' compares the schema of a PR with the main branch and goes red on a reused number or a type change.
What else is worth knowing
- Safe changes: a new field with a new number, a new enum value, a new method or service. Old clients ignore the new field, and new ones get the default from old servers.
- Renaming a field. Nothing changes in the binary format, but the JSON representation and the source code of clients break, as do enum value names in JSON. This is checked separately.
- Changing a type. Some integer types (
int32,uint32,int64,bool) are mutually compatible but with truncation of values;stringtoint32is not compatible. - Default values. In proto3 a scalar field with a zero value is not transmitted, and the receiver cannot tell "not set" from 0. For the difference between "discount 0%" and "no discount given" there is
optional(a presence flag). The documentation considers wrappers such asInt32Valueoutdated for new fields. - Enum. The first value has to be zero and is usually named
*_UNSPECIFIED: it becomes the default. - Numbers 1 to 15 are encoded with a one-byte tag, so they are kept for the most frequent fields.
Common mistakes
- Removing a field without
reserved: the number is "freed" and sooner or later gets taken again. - Changing a type to a "similar" one without checking: values are silently truncated.
- Testing compatibility only on a new client with a new server: the errors show on an old client with a new server and the other way round.
Check yourself
Is it safe to rename the field email to contact_email without changing the number?
Answer
For the binary format yes, because the name is absent from the bytes. But the JSON representation (the key changed) and the source code of clients that call the generated methods break, so a rename needs separate coordination.
The "new client, new server" test is green, the incident happened on an old client. Which two scenarios are missing?
Answer
An old client with a fresh server (backward) and a new client with an old server (forward): they involve unknown fields, default values and new enum values.
reserved and check compatibility automatically: buf breaking in CI and tests of an old client against a new server. Separately I remember the JSON representation, integer truncation and the difference between "not set" and zero via optional.