How do you handle backward compatibility in Protobuf?
Answer
Protobuf backward compatibility is maintained through these rules: (1) Never change field numbers — field numbers identify fields in binary encoding; changing them breaks wire compatibility; (2) Never remove fields in use — mark deprecated fields as reserved or leave them in place; (3) New fields are optional by default in proto3 — old clients ignore new fields; (4) Field type changes — some are compatible (int32 → int64), most are not (string → bytes breaks things); (5) Enum values — adding new values is safe; removing values or changing numbers breaks compatibility; (6) Reserve deprecated field numbers — reserved 5, 10 to 15; reserved "old_field"; prevents future reuse of those numbers; (7) Use the Buf CLI — buf breaking --against origin/main automatically detects breaking changes in CI. Following Protobuf style guide and semantic versioning for .proto files minimizes compatibility issues in evolving APIs.
Previous
What is the role of `google.protobuf.Any` type?
Next
What is gRPC gateway and what problem does it solve?