Lesson 1 of 9 · 26m
Protobuf Contracts That Age Well
Model RPCs and messages around stable business meaning, reserve compatibility room, and avoid schema habits that become migration debt.
The worst protobuf schemas are written like database rows with RPC decoration. They look efficient in week one and become untouchable in year two because nobody knows which field means what, which client relies on it, or whether changing a type will silently corrupt old traffic. A protobuf contract is not serialization paperwork; it is the public memory of your system.
Start with the operation, not the object
Design services around operations with clear ownership and consequences:
service Billing {
rpc CreateInvoice(CreateInvoiceRequest) returns (CreateInvoiceResponse);
rpc GetInvoice(GetInvoiceRequest) returns (Invoice);
rpc VoidInvoice(VoidInvoiceRequest) returns (VoidInvoiceResponse);
}
message GetInvoiceRequest { string invoice_id = 1; }UpdateInvoice with a bag of optional fields is convenient until nobody can tell which transition is legal. Explicit operations make authorization, audit, idempotency, and validation easier to reason about.
Field numbers are forever
Never reuse field numbers. If a field is removed, reserve it:
message Customer {
string customer_id = 1;
string display_name = 2;
reserved 3, 4;
reserved "legacy_credit_score", "internal_notes";
}The number is the wire contract. Reusing it can make an old client interpret a new value as an old meaning. Protobuf will not save you from semantic corruption; it will happily decode bytes into the wrong business meaning.
Prefer explicit domain types
Repeatedly passing raw strings works until one service thinks account_id is a UUID, another thinks it is a tenant slug, and a third accepts either. Shared types help only when they are truly stable:
message Money {
string currency = 1; // ISO 4217, for example USD
int64 minor_units = 2; // cents, not floating-point dollars
}Do not create a shared Common.proto junk drawer. Shared contracts create coupling; copy small structures when ownership differs.
Presence matters more than people expect
In proto3, scalar fields default to zero values. If 0 is meaningful and "unset" is meaningful, use optional or a wrapper-like design deliberately:
optional int32 retry_after_seconds = 5;This distinction is crucial for patches, feature flags, quotas, and configuration. "The client omitted it" and "the client explicitly set it to zero" are different instructions.
Make output smaller than truth
Responses should serve the RPC's use case. Do not expose every internal field because protobuf makes it easy. Internal replication state, cost centers, deletion tombstones, and security metadata are not automatically part of the public contract.
Field notes
- Use
linttooling early; buf'sbuf lintcatches style and compatibility mistakes while they are cheap. - Keep oneof for real alternatives, not for vague polymorphism that clients cannot interpret.
- Document units in field names or comments:
timeout_ms,size_bytes,minor_units. - Do not encode workflow state as a magic integer without an enum whose zero value is explicitly unspecified.
- Compatibility is both wire compatibility and meaning compatibility. The second one hurts more.
Was this lesson helpful?