Protobuf: how a message is written on the wire
How Protocol Buffers turns a message into bytes, one field at a time, worked through byte by byte, from varints and tags to strings, negative numbers and missing fields, and how a reader skips fields it does not know.
Protocol Buffers (Protobuf) is Google's binary format for structured messages. A schema file (.proto) declares each message's fields, and gives every field a field number that never changes. The bytes on the wire carry those numbers, not the field names, and not the offsets a format like SBE uses (fields and padding).
This article writes one small message by hand, byte by byte: an order acknowledgement whose .proto file declares three fields.
| Field number | Name | Type | Value |
|---|---|---|---|
| 1 | order_id | int64 | 9001 |
| 2 | order_list_id | optional int64 | empty |
| 3 | symbol | string | BTCUSDT |
The whole message is 12 bytes:
- 0tag08field 1 (order_id), a varint follows
- 1order_ida9469001 as a varint
- 3tag1afield 3 (symbol), a length follows
- 4length077 bytes follow
- 5symbol42544355534454BTCUSDT
order_list_id is not there at all: an empty field writes nothing (null values). The sections below explain each row.
Records: a tag, then a value
A Protobuf message is a run of records, one per field that has a value. Each record is a tag, which says which field this is and how its value is written, followed by the value. There is no header with the message's length and no end marker: the message ends where its bytes end (framing covers how a stream separates messages).
Because every record names its own field, the order of records does not matter, a field the reader has never heard of can be skipped, and a field nobody set costs no bytes.
Varints: integers in as few bytes as they need
Most integers, the tags themselves included, are written as varints (variable-length integers). A small number takes one byte; a large one takes more. The rule:
- Write the number in binary and cut it into groups of 7 bits, starting from the lowest bits.
- Write the groups lowest first, one per byte, in the low 7 bits of the byte.
- Set the byte's top bit (the eighth) to
1if another byte follows, and to0on the last byte.
Take 9001. In binary it is 10001100101001, 14 bits, so it splits into two groups of 7:
| Group | Its 7 bits | Its value | Top bit | Byte in binary | Byte in hex |
|---|---|---|---|---|---|
| lowest | 0101001 | 41 | 1, another byte follows | 1 0101001 | a9 |
| next | 1000110 | 70 | 0, the last byte | 0 1000110 | 46 |
A reader does the reverse. It reads a9, sees the top bit set, keeps the low 7 bits (0101001, 41) and reads on. It reads 46, sees the top bit clear, keeps its 7 bits (1000110, 70) and stops. Then it puts the groups back in order, highest first: 1000110 followed by 0101001 is 10001100101001, which is 9001.
Each group is worth 128 times the one before it, because 7 bits hold the values 0 to 127. So the second group counts 128s: 70 × 128 + 41 = 8960 + 41 = 9001. A third group would count 128 × 128 = 16384s, and so on.
The cost of this compactness is that a reader cannot jump to a field: it has to read each varint to find where the next record starts.
| Values | Bytes |
|---|---|
| 0 to 127 | 1 |
| 128 to 16,383 | 2 |
| 16,384 to 2,097,151 | 3 |
| up to 2^64 − 1 | up to 10 |
Tags: field number and wire type
The tag is a varint too. It packs two things into one number: the field number, shifted left by 3 bits, and a 3-bit wire type in the lowest bits:
tag = field number × 8 + wire type
The first tag of the message, 08, is 1 × 8 + 0: field 1, wire type 0. The second, 1a (26), is 3 × 8 + 2: field 3, wire type 2. Field numbers 1 to 15 fit in a one-byte tag, which is why schemas give their most frequent fields the smallest numbers.
Wire types: how long a value is
The wire type does not say what the value means (that is in the schema); it says how the value is laid out, so that any reader, with or without the schema, knows where the value ends:
| Wire type | Name | Used for | Where the value ends |
|---|---|---|---|
| 0 | VARINT | int32, int64, uint32, uint64, sint32, sint64, bool, enum | at the first byte whose top bit is 0 |
| 1 | I64 | fixed64, sfixed64, double | after 8 bytes |
| 2 | LEN | string, bytes, nested messages, packed repeated fields | a varint length comes first, then that many bytes |
| 5 | I32 | fixed32, sfixed32, float | after 4 bytes |
(Wire types 3 and 4 marked the start and end of "groups", an old feature that is deprecated.)
This table is what makes Protobuf easy to evolve: a reader built before field 3 existed still reads its tag, sees wire type 2, reads the length 07, and skips 7 bytes to the next record (schema versioning). Fixed-width numbers (I64, I32) are always little-endian (endianness).
Strings, bytes and nested messages
A LEN value is a varint length followed by that many bytes. For symbol, that is 07 and the seven ASCII bytes of BTCUSDT. A nested message is written the same way: its own records, prefixed by their total length, so a reader can skip the whole nested message in one step (variable-sized data and lists).
Negative numbers
A varint stores an unsigned number. An int64 of −5 is stored as the 64-bit two's complement pattern, which has its top bit set, so it always takes the full 10 bytes:
- 0valuefbffffffffffffffff01−5 as int64
The sint32 and sint64 types avoid this with ZigZag encoding, which interleaves negative and positive numbers before writing the varint: 0 → 0, −1 → 1, 1 → 2, −2 → 3, 2 → 4, and so on. −5 becomes 9, one byte (09). A schema uses sint64 for fields that are often negative, such as price changes, and int64 for ones that rarely are, such as ids.
Missing fields and default values
An empty field writes no record, so a reader that finds no record for a field has to decide what that means:
- A field declared
optional, and any field whose type is a message, has explicit presence: the reader can tell "not set" apart from a value, asorder_list_idabove. - A plain scalar field in proto3 has implicit presence: the writer also leaves it out when it holds its default,
0or"", so a reader cannot tell "set to 0" from "not set".
This is the second of the three ways to write a missing value in null values: tag the fields and leave the empty ones out.
Sources
Reviewed by Structure on 2026-11-24. Last changed 2026-11-24.