Protocol Buffers in depth
The proto3 language in full: scalars, messages, enums, oneof, map, packages, options, field presence, and the tag-number math that turns a message into bytes.
Learning objectives
By the end of this chapter you can:
- Map every proto3 scalar type to its wire type (VARINT, I64, I32, LEN) and explain why
sint32andsint64exist alongsideint32andint64. - Choose between
repeated,map<K, V>andoneoffor a field shape, and state the contract each enforces. - Explain why a fieldâs tag number, not its name, is Protobufâs wire-level contract, and why reusing a shipped tag number corrupts old data silently.
- Hand-decode a small messageâs wire bytes field by field with the key-byte formula
(field_number << 3) | wire_type. - State why proto3 has no
requiredand howoptionalrestores the field-presence information a bare scalar canât carry.
Why this matters
Every type in the shipping domain (Booking, Cargo, Money, Route) is a .proto message in contracts/proto/, read once by buf and generated into both the Quarkus and Python stacks. The sibling REST course models the same nouns as JSON Schema components: two contract languages for the same domain, because the nouns donât change when the transport does. A JSON Schema can describe a numberâs range. It canât tell a parser which bits on the wire belong to that number. Protobufâs type system and its wire format are one document, and you need both.
Scalars, messages and wire types
A message is a named set of fields, each with a type and a field number:
message Money {
string currency = 1; // ISO 4217, e.g. "USD"
string amount = 2; // decimal string, e.g. "1250.00"
}
The field number (= 1, = 2) is neither a default value nor an array index. It is the fieldâs permanent identifier on the wire. Every scalar maps to one of four wire types:
| Wire type | Scalars | Encoding |
|---|---|---|
| VARINT (0) | int32, int64, uint32, uint64, sint32, sint64, bool, enum |
Variable length: small values take one byte, growing a byte at a time |
| I64 (1) | fixed64, sfixed64, double |
Always eight bytes; cheaper than VARINT for usually large values |
| LEN (2) | string, bytes, embedded messages, packed repeated fields |
A length prefix, then exactly that many bytes, so a parser needs no delimiter scan |
| I32 (5) | fixed32, sfixed32, float |
Always four bytes |
sint32 and sint64 arenât redundant. Plain int32 VARINT-encodes a negative number as its unsigned 64-bit twoâs-complement value, always ten bytes. sint32 and sint64 apply ZigZag encoding first, mapping small-magnitude negatives to small unsigned values, so -1 costs one byte instead of ten. Choose them when a field is often negative. Theyâre not a safer default than int32.
Nested messages and the enum zero value
A fieldâs type can be another message. Booking embeds Location twice, a repeated Cargo and a Money, each defined once and reused:
message Booking {
string id = 1; // "bkg_" prefix, ULID-shaped
Location origin = 2;
Location destination = 3;
repeated Cargo cargo = 4;
BookingState state = 5;
Money quote = 6;
google.protobuf.Timestamp created_at = 7;
map<string, string> labels = 8;
}
An enum is a closed set of named integer constants, and proto3 requires the value mapped to 0 to exist. By convention it is named <ENUM_NAME>_UNSPECIFIED. BookingStateâs zero value is BOOKING_STATE_UNSPECIFIED, not BOOKING_STATE_PENDING, even though every real booking starts pending. Proto3 canât distinguish ânever assignedâ from âassigned zeroâ, so whatever sits at 0 is what every unset field reads back as, including in messages built before the field existed. Naming it UNSPECIFIED keeps a missing value from being misread as meaningful. Enum constants carry the enumâs name as a prefix (BOOKING_STATE_CONFIRMED, not CONFIRMED) because they share a namespace with their enclosing scope, and two enums in one file canât both declare a bare CONFIRMED.
repeated, map and oneof
| Shape | Contract | On the wire |
|---|---|---|
repeated |
Zero or more, in order (Booking.cargo, Route.legs) |
One LEN-prefixed entry per element, or one packed run for repeated scalars |
map<K, V> |
A dictionary in every generated language (Booking.labels), with no iteration order |
A repeated message of implicit {key, value} entries, which is why a map canât itself be repeated |
oneof |
Of a named group, at most one is ever set; setting another clears the first | Nothing for an unset branch; the exclusivity is generated-code behavior (WhichOneof() in Python, a case enum in Java) over fields sharing one storage slot |
None of the shipping types needs a oneof, but the shape recurs: a PaymentMethod that is a credit card, a purchase order or an escrow reference, never two. A oneof enforces that in the type of every generated language instead of in a comment a caller will eventually violate.
Packages, imports and options
package shipping.type.v1; namespaces every message and enum in the file, so shipping.type.v1.Location never collides with another serviceâs Location. The trailing v1 is a convention, since Protobuf has no built-in package version. An incompatible Location gets a new package, shipping.type.v2, beside the old one, and callers compiled against v1 keep working. buf breaking enforces that convention.
import "shipping/type/v1/types.proto"; pulls another fileâs declarations into scope, resolved against the include path the compiler is given. buf.yamlâs modules: - path: proto lets types.proto and routing.proto resolve against each other without knowing each otherâs location. routing.proto imports types.proto for Location, Cargo, Money and Route, and google/protobuf/field_mask.proto, a well-known type shipped with every compiler.
option statements configure code generation without changing the wire format. A file-level option java_package or option go_package tells a generator where to emit. The shipping.type.v1 files set none, because the generators derive a default from the proto package. A contract shared across teams with stricter layouts should pin them.
proto3 defaults, presence and no required
An unset scalar reads back as its typeâs zero value (0, "", false), never null. Thatâs the biggest semantic gap from a JSON Schema or ORM model, where âabsentâ and âpresent but zeroâ differ. In bare proto3 a Cargo.weight_kg of 0.0 is indistinguishable from one nobody set. Message-typed fields donât have the problem: an unset Booking.quote is a null pointer, not an empty Money{}.
The optional keyword, added in protoc 3.15, restores presence. optional int32 progress_pct = 4; generates a HasProgressPct() accessor that tells âset to 0â from ânever touchedâ. It is implemented as a synthetic single-field oneof, so it behaves like a declared oneof on the wire. It costs a Has*() check at every call site, and most fields donât need it: Cargo.hazardous unset and Cargo.hazardous false are the same fact to every consumer. Use optional when âwas this ever setâ is something the caller must act on.
Proto3 removed required. A required field made the whole message fail to parse when absent, so a server could never safely stop sending it and a client could never safely add one without breaking a fleet that hadnât upgraded. To make sure a field is populated, validate in your own code, not in a parser constraint that can never relax.
Tag numbers are the contract
A key byte is (field_number << 3) | wire_type. A VARINT byte carries seven payload bits and the wire type takes three of them, so numbers 1 through 15 fit in one key byte and 16 needs two. Fields serialized on every call, such as the hot fields of Booking and Cargo, should take 1 through 15, a one-byte tax instead of two at RoutingService volume.
A shipped field number is permanent in a way a field name is not. Renaming Booking.id to Booking.bookingId changes the generated accessor and nothing on the wire, since the string "id" never appears there. Reusing tag 1 for a different field after Booking.id has shipped is the failure case. Every message serialized under the old definition still carries a 1-tagged LEN value, and a parser built against the new definition decodes it as whatever 1 means now, silently, with no error. The number is the contract and the name is documentation.
Hand-decoding a message
Serializing Money{currency: "USD", amount: "1250.00"} produces exactly these 14 bytes:
0A 03 55 53 44 12 07 31 32 35 30 2E 30 30
0x0A is binary 0000 1010. The low three bits, 010, are wire type 2 (LEN), and the rest is field number 1, currency. 0x03 is a VARINT length: three bytes follow, 55 53 44, the UTF-8 for "USD". 0x12 is 0001 0010: LEN again, field number 2, amount. 0x07 announces seven bytes, "1250.00". Two key-length-value runs, no bytes spent on field names.
The JSON form, {"currency":"USD","amount":"1250.00"}, is 37 bytes, more than twice as many, nearly all of it syntax and field-name text both ends already agree on.
Why binary beats JSON
The gap compounds in three ways minifying JSON canât close:
- Field identity is a one- or two-byte integer instead of a quoted key, so a long descriptive name like
weight_kgcosts the same asw. - Numbers use VARINT and fixed-width encodings instead of decimal ASCII, so small integers and booleans cost one byte.
- Parsing never tokenizes. A LEN prefix says how many bytes to consume and a VARINTâs continuation bit says where an integer ends, so decoding is fixed-size reads, not a text scan with escape handling.
None of this is specific to gRPC; plain HTTP/1.1 keeps every advantage. HTTP/2 adds multiplexed, HPACK-compressed framing on top, and the two wins stack.
Choosing oneof over separate fields
Run the choice through the decision axes. Shape and sync-or-stream are neutral, since a oneof serializes the same on a unary or streamed call. Failure class matters when the alternatives are outcomes a caller must branch on. A RouteConstraint that is a max transit time or a max cost is one choice with two shapes, and a oneof turns a missed case into a gap in a generated switch instead of a runtime bug when both fields arrive set. Concurrency matters when independent writers could otherwise race to populate what should be exclusive state, because a oneof makes âexactly oneâ a property of the data. Evolution cuts the other way. Adding a branch to an existing oneof is additive like any new field. Converting an existing plain field into a oneof member after it shipped is not safe, because an already-serialized message with that field set still parses as âfield N presentâ and older code must agree on what that now means.
Decision
Use a oneof when fields are alternative shapes of one value, such as a payment method that is a card, a purchase order or an escrow reference, and the exclusivity is a domain fact. Keep plain fields when values are independent and can coexist, even if usually absent: Booking.labels is an open-ended set of key/value pairs, which is what a map is for. Default to plain fields when unsure. Converting independent fields into a oneof later is the safe direction. Converting a oneof back to independent fields after callers depend on its exclusivity is not.
đ€ Explore with your agent
âRead contracts/proto/shipping/type/v1/types.proto. For every message, list any group of fields that are mutually exclusive and should become a oneof, every enum whose zero value is not *_UNSPECIFIED, and any frequently serialized field numbered above 15. Do not renumber or reuse any shipped tag; propose changes as new fields only, and add a Notification-style oneof example to examples/03-protobuf/oneof_demo.proto if you find a case.â
Check: run
buf build contractsandexamples/03-protobuf/verify.sh; both must still pass, and no existing field number may change in the diff.
Build, run, observe
cd examples/03-protobuf
./verify.sh
There is no server, port or container. verify.sh runs buf build against contracts/, generates Python stubs for shipping.type.v1 and a small standalone oneof demo proto (Notification) in a throwaway virtualenv, then runs roundtrip.py. That script hand-decodes Moneyâs 14 bytes with a 20-line VARINT reader using the key-byte arithmetic above, and round-trips a Money, a Booking (nested message, enum, map, repeated field, Timestamp) and the demo through SerializeToString() and ParseFromString(). A run ends with a PASS: line. buf comes from PATH if installed; otherwise verify.sh downloads a pinned release into .bin/ on first run.
For deeper coverage
- Clément Jean, gRPC Go for Professionals (Packt): Protobuf language, schema design, and the evolution concerns tag numbers exist to protect.
- Protocol Buffers: Language Guide (proto3): the canonical scalar types, wire types and field-presence reference.
- buf: breaking change detection: the check that enforces the tag-number contract.
- Cloud Native Design Patterns, the Versioning & Evolution appendix, for what counts as a breaking change in Protobuf alongside REST and GraphQLâs own versioning stories.