How a binary message is put together

How a stream of raw bytes represents numbers, fields, missing values and message boundaries, why binary formats work this way, and what silently breaks when a reader gets a rule wrong.

View as Markdown

An exchange market-data feed can push tens of millions of updates a day. Binary messages are the most efficient way to carry them, for three reasons:

  • Fewer bytes: no field names, quotes or separators travel with the values, and each number takes only the bytes its type needs.
  • No parsing: numbers are written in the form a computer already holds them in memory, so a reader copies them instead of converting text.
  • Fields at known places: in formats like SBE every field sits at a fixed offset, so a reader goes straight to the one it needs.

For contrast, imagine sending the same updates as text like JSON. The wire carries quotes, braces, commas, repeated field names like "bidPrice", and decimal text like "62900.00" that every receiver has to scan character by character and convert back into numbers.

The message below (BestBidAskStreamEvent from Binance's market-data streams) takes 164 bytes as compact JSON with its field names, and 66 bytes as a binary message (70 with the 4-byte length the sample file puts before it). Here is the same update both ways, one field to a row: the JSON's text over the binary's bytes, each with a bar as long as the bytes it takes.

  1. length prefixJSON—0Binary420000004
  2. messageHeader.blockLengthJSON—0Binary32002
  3. messageHeader.templateIdJSON—0Binary11272
  4. messageHeader.schemaIdJSON—0Binary01002
  5. messageHeader.versionJSON—0Binary00002
  6. eventTimeJSON"eventTime":180512640000187928Binary57c7d8bdc06906008
  7. bookUpdateIdJSON"bookUpdateId":7030001250326Binaryd70f355e100000008
  8. priceExponentJSON—0Binaryfe1
  9. qtyExponentJSON—0Binaryf81
  10. bidPriceJSON"bidPrice":"62900.00"21Binary50fa5f00000000008
  11. bidQtyJSON"bidQty":"3.89678000"21Binaryb0033a17000000008
  12. askPriceJSON"askPrice":"62900.01"21Binary51fa5f00000000008
  13. askQtyJSON"askQty":"5.35938000"21Binaryd0c3f11f000000008
  14. symbol.lengthJSON—0Binary071
  15. symbol.varDataJSON"symbol":"BTCUSDT"18Binary425443555344547
  16. braces and commasJSON{},,,,,,8Binary—0
  17. Whole messageJSON164Binary70
The BestBidAskStreamEvent as JSON with the schema's field names, 164 bytes, and as the binary message, 66 bytes after a 4-byte length: one row per field, each bar as long as its bytes

The field names, the quotes and the decimal text take most of the JSON's bytes. The binary side spends its bytes on numbers: 4 on the length the file puts before the message, 8 on the SBE message header, one each on the two exponents that turn its integers back into prices, and one on the symbol's length. The trade-off is that raw bytes carry no explanation of their own: if the sender and the reader disagree on even one rule, the bytes rarely cause an error. They silently decode into a wrong price or a message type that does not exist.

Four topics decide how any binary message is read, one section each:

  1. Endianness: the order of a number's bytes.
  2. Fields and padding: how a reader tells the fields of a message apart, and whether unused bytes sit between them.
  3. Framing: where one message ends and the next begins.
  4. Null values: how a field with no value is written.

An example message

Let's look at a real message from Binance. Below is the BestBidAskStreamEvent message from this schema:

binance_stream_1_0_sbe.xml

Here are the message's 70 bytes, sixteen to a line, with the offset of each line on the left and, on the right, every byte that happens to be a printable ASCII character:

 0  42 00 00 00 32 00 11 27 01 00 00 00 57 c7 d8 bd  B...2..'....W...
16  c0 69 06 00 d7 0f 35 5e 10 00 00 00 fe f8 50 fa  .i....5^......P.
32  5f 00 00 00 00 00 b0 03 3a 17 00 00 00 00 51 fa  _.......:.....Q.
48  5f 00 00 00 00 00 d0 c3 f1 1f 00 00 00 00 07 42  _..............B
64  54 43 55 53 44 54                                TCUSDT
The 70 bytes of the frame at byte 686 of the synthetic BTCUSDT stream (btcusdt-stream.sbe), sixteen to a line

The text column ends in BTCUSDT, the one piece of text in the message. The other printable characters there are bytes of numbers that happen to fall in the ASCII range. Everything else is binary numbers packed end to end. With the message's schema (binance_stream_1_0_sbe.xml, bundled in the Market Data Visualizer), those 70 bytes slice cleanly into fields. The colors in both views mark the four parts: the file's length prefix, the SBE message header, the fixed fields of the message (its root block), and the variable-length symbol at the end.

OffsetBytesFieldTypeValueMeaning
0–342 00 00 00length prefixuint326666 message bytes follow
4–532 00messageHeader.blockLengthuint1650root block: 50 bytes
6–711 27messageHeader.templateIduint1610001BestBidAskStreamEvent
8–901 00messageHeader.schemaIduint161
10–1100 00messageHeader.versionuint160
12–1957 c7 d8 bd c0 69 06 00eventTimeutcTimestampUs18051264000018792027-03-15 16:00:00.001879 UTC
20–27d7 0f 35 5e 10 00 00 00bookUpdateIdupdateId70300012503
28fepriceExponentexponent8-2× 10^-2
29f8qtyExponentexponent8-8× 10^-8
30–3750 fa 5f 00 00 00 00 00bidPricemantissa64629000062900.00
38–45b0 03 3a 17 00 00 00 00bidQtymantissa643896780003.89678000
46–5351 fa 5f 00 00 00 00 00askPricemantissa64629000162900.01
54–61d0 c3 f1 1f 00 00 00 00askQtymantissa645359380005.35938000
6207symbol.lengthuint877 bytes follow
63–6942 54 43 55 53 44 54symbol.varDatauint8"BTCUSDT"
The BestBidAskStreamEvent at byte 686 of the synthetic BTCUSDT stream (btcusdt-stream.sbe), field by field; offsets count from the frame's first byte

The app's layout counts offsets from the first byte of the root block, which is byte 12 of the frame: bidPrice sits at offset 18 of the root block and at byte 30 of the frame.

Every section below takes one piece of those 70 bytes and explains the idea behind it from scratch.

Endianness

Endianness, or byte order, is the order in which a number that takes more than one byte keeps those bytes: the most significant byte first or the least significant byte first. A reader that assumes the other order than the writer used reads every such number wrong.

To see why the question exists, start from the byte. To a computer, memory and network packets are arrays of bytes. Each byte is 8 bits and holds one whole number from 0 to 255 (0x00 to 0xff in hexadecimal). That is enough for a small count or a single ASCII character, but not for a message id like 10001 or a price like 6290000.

To store a larger integer, a computer splits it into base-256 digits across 2, 4 or 8 adjacent bytes. Take 10001. Divide by 256: you get 39 (0x27) with remainder 17 (0x11), because 39 × 256 + 17 = 10001. Those two bytes, 27 (the high byte, worth 256 each) and 11 (the low byte, worth 1 each), have to sit next to each other in memory. Which one goes first?

There is no mathematical reason to prefer one direction, so computing ended up with both. Putting the most significant (high) byte first is big-endian, also called network byte order because Internet headers (IP, TCP) standardized on it; it reads left to right like decimal numbers. Putting the least significant (low) byte first is little-endian. Modern x86 and almost all ARM servers keep integers little-endian in memory, so sending little-endian bytes lets a receiver load a price straight into a CPU register without swapping bytes.

The catch is that raw bytes never say which order they use. Here are two numbers from the message above, read both ways:

FieldBytes on the wireRead low byte firstRead high byte firstThe number written high byte first
messageHeader.templateId11 2710001439127 11
bidPrice50 fa 5f 00 00 00 00 006290000583508072081653760000 00 00 00 00 5f fa 50
messageHeader.templateId and bidPrice of the BestBidAskStreamEvent at byte 686 of the synthetic BTCUSDT stream (btcusdt-stream.sbe), read low byte first and high byte first

A decoder that forgets this message is little-endian and reads high byte first gets:

  • 11 27 as 17 × 256 + 39 = 4391 instead of 10001, so it looks up the wrong message type and either drops the quote or decodes it with the wrong layout.
  • 50 fa 5f 00 00 00 00 00 as 5,835,080,720,816,537,600 instead of 6,290,000. There is no syntax error and no exception: just a valid 64-bit integer that is about twelve orders of magnitude wrong.

Single-byte values like priceExponent (fe = −2) and ASCII text (BTCUSDT) do not depend on byte order, because each value or character is one byte. Every wider integer or floating-point number does.

Fields and padding

A binary message has no names, quotes or commas between its fields, so a reader cannot tell them apart by looking at the bytes. It tells them apart by position: the schema gives every field a fixed place and width, and a reader counts its way to it. Some layouts also leave unused bytes, padding, between fields; whether they do decides every offset after the gap.

Look at how a receiver reads bidPrice out of the message. It does not search for the text "bidPrice". Every field before it has a known, fixed width (eventTime 8 bytes, bookUpdateId 8, priceExponent 1, qtyExponent 1), so bidPrice always starts at byte 8 + 8 + 1 + 1 = 18 of the root block. One addition, start + 18, reaches it without touching any byte before it. The position of a field from the start of its block is its offset.

Now notice something about offset 18: bidPrice is an 8-byte integer, yet 18 is not a multiple of 8.

In memory, compilers place 4-byte integers at addresses divisible by 4 and 8-byte integers at addresses divisible by 8 (natural alignment), because processors fetch memory in aligned chunks: an unaligned read used to cost extra cycles, and some older processors refused it outright. To reach the next multiple of 8 after byte 17, a C compiler (or Rust, for a struct marked #[repr(C)]) inserts 6 unused bytes of padding between qtyExponent and bidPrice. Here are the root block's fields as they travel and as such a struct holds them, each field's offset beside a bar as long as its bytes:

The block of BestBidAskStreamEvent as it travels and as a compiler aligns the same fields in memory: one row per field, each bar as long as its bytes
FieldPacked, as it travels: 50 bytesAligned, as a C struct holds it: 56 bytes
eventTime0808
bookUpdateId8888
priceExponent161161
qtyExponent171171
padding186
bidPrice188248
bidQty268328
askPrice348408
askQty428488

Formats that put padding in do it for speed: a field at an aligned offset can be loaded directly, and a message laid out exactly like a struct can be read without copying. Formats that leave it out do it for size: padding is bytes on every message that carry no data. Wire formats for market data mostly choose size and pack fields back to back. On this message packing saves 6 bytes of 56 on every update, which adds up to gigabytes over a busy trading day. Some formats add padding only where a schema asks for it, by leaving a gap before a field or at the end of a block, to align fields for direct memory access or to reserve space; they then specify that writers fill those bytes with zeros and readers skip them.

Here is the classic bug in a first binary decoder. The developer copies the fields from the specification into a C struct, points the struct at the received bytes, and reads msg->bidPrice. Because the compiler padded the struct, that reads bytes 24..31 of the root block instead of 18..25. In the message above those are 00 00 b0 03 3a 17 00 00: the last two bytes of bidPrice followed by the first six of bidQty. They decode to 25,537,937,408,000 instead of 6,290,000, and every field after it is off by 6 bytes as well.

Framing

Framing marks where one message ends and the next begins, so a reader can cut a run of bytes into whole messages before it decodes any of them.

Imagine two BestBidAskStreamEvent updates arriving over a TCP connection, or stored one after another in a file. TCP does not deliver messages: it delivers a stream of bytes, like water through a pipe. One read might hand your program 140 bytes (two messages run together), 50 bytes (less than one message), or 95 bytes (one message and the first 25 bytes of the next).

If the stream holds only the messages themselves, back to back, how do you tell where the first stops and the second starts? You could decode the first field by field until you reach its end, but that breaks the moment you meet a message type your code does not know, or a message cut short, or when you want to skip messages you do not care about. One unknown message loses your place for the rest of the stream.

The answer is framing: an outer boundary around each message, so a receiver can cut the stream into whole messages before decoding any of them. The simplest and most common frame is a length prefix: a small integer just before each message that says how many bytes belong to it.

That is how btcusdt-stream.sbe keeps 35,668 messages in one flat file. Each message comes after a 4-byte little-endian length that counts the message's bytes, not its own four. Here are the message above and the one after it, as they sit in the file:

  1. byte 686 42 00 00 00 32 00 11 27 01 00 00 00 57 c7 d8 bd c0 69 06 00 d7 0f 35 5e 10 00 00 00 fe f8 50 fa 5f 00 00 00 00 00 b0 03 3a 17 00 00 00 00 51 fa 5f 00 00 00 00 00 d0 c3 f1 1f 00 00 00 00 07 42 54 43 55 53 44 54 length 66: a BestBidAskStreamEvent; the next frame starts at 686 + 4 + 66 = 756
  2. byte 756 42 00 00 00 32 00 11 27 01 00 00 00 ab d3 d8 bd c0 69 06 00 db 0f 35 5e 10 00 00 00 fe f8 50 fa 5f 00 00 00 00 00 c8 c5 19 17 00 00 00 00 51 fa 5f 00 00 00 00 00 d0 c3 f1 1f 00 00 00 00 07 42 54 43 55 53 44 54 length 66: a BestBidAskStreamEvent; the next frame starts at 756 + 4 + 66 = 826
2 frames of the synthetic BTCUSDT stream (btcusdt-stream.sbe) from byte 686: each message follows its length, 4 bytes, low byte first

A reader reads 4 bytes (42 00 00 00 = 66), waits until 66 more bytes have arrived, hands those 66 bytes to the message decoder, and knows the very next byte, 756, starts the next length. If it does not know a message type, it skips the 66 bytes and stays in step.

Other transports build on the same idea:

  • Packet headers (UDP multicast): a UDP packet already has a boundary, and exchanges pack several small messages into one packet. They put a packet header at the front (usually a sequence number, to notice lost or reordered packets, and a sending time) and a short length before each message inside the packet.
  • Transport frames (WebSocket): WebSocket frames each binary message in its own protocol header, so the message inside needs no length of its own. Binance sends each of these messages in its own WebSocket frame; the length prefix exists only in the sample file.
  • File footers (storage files): a file on disk is read whole rather than as it arrives, so a format can put an index at the end that lists the offset and length of every block inside it.

Three framing bugs happen constantly:

  • Forgetting to strip the frame: handing all 70 bytes to the message decoder makes it read 42 00 00 00 as the start of the message header, and every field shifts by 4 bytes.
  • Counting the length differently: this file's 66 leaves out the 4 length bytes, whereas many exchange framing headers count themselves too (a 4-byte header with a 66-byte message would say 70). Being off by 4 bytes misaligns the next message and every one after it.
  • Reading the frame in the wrong byte order: framing headers often come from a different standard than the message inside and can use the other byte order. Reading 42 00 00 00 high byte first tells the reader to wait for 1,107,296,256 bytes before handing over a 66-byte quote.

Null values

A null value is a field that has no value in a given message: an order that belongs to no order list has no orderListId. In JSON you leave "orderListId" out or write "orderListId": null. In a binary message with fixed offsets you cannot leave a field out: dropping an 8-byte orderListId would pull every field after it 8 bytes to the left and break their offsets. Those 8 bytes have to be on the wire, even when the field is empty.

Every combination of 64 bits is already a valid 64-bit integer, so a binary format has three ways to say "no value":

  1. Reserve one value as a sentinel: keep the fixed slot and agree that one extreme number, typically the smallest signed integer or the largest unsigned one, means "empty". It costs no extra bytes and keeps fixed offsets, at the price of one number the field can no longer hold.
  2. Tag fields and leave missing ones out: a tag is a small number written in front of a field's value that says which field follows. A reader no longer finds fields by their offset: it reads a tag, looks up which field that is, reads the value, then reads the next tag. A field with no value simply gets no tag and no bytes.
  3. Store presence separately: put a bitmask in front of the values: a number read bit by bit, one bit per field, where 1 means "present" and 0 means "empty". A reader checks a field's bit first and reads its value only when the bit is set, so an empty field takes one bit instead of its full size.

Here are all three on the same order acknowledgement: its orderId is 9001 and its optional orderListId is empty.

A sentinel (SBE): both 8-byte slots are written, low byte first 16 bytes

  1. 0orderId29230000000000009001
  2. 8orderListId0000000000000080−9,223,372,036,854,775,808, the smallest int64: the sentinel for empty

Tags (Protobuf): each field says which one it is, the empty one is left out 3 bytes

  1. 0tag08field 1 (orderId), a varint follows
  2. 1orderIda9469001 as a varint, Protobuf's variable-length integer; field 2 (orderListId) has no tag and no bytes

Presence kept apart: one bit per field ahead of the values 9 bytes

  1. 0presence01bit 0 (orderId) is set, bit 1 (orderListId) is clear
  2. 1orderId29230000000000009001; orderListId's 8 bytes are not written
One message, three ways to say that orderListId is empty; each row's offset counts from the start of its group

How a tag and a varint are built, byte by byte, is in how Protobuf writes a message.

Column files such as Parquet use the third way for a whole column at once: one present/absent mark per row, packed into bits, and only the present values stored.

Sentinels are fast and cost nothing, which is why low-latency feeds use them, and also why they catch readers out. If code forgets to test for the sentinel before doing math or logging, an empty signed 64-bit field quietly becomes −9,223,372,036,854,775,808, an empty unsigned byte becomes 255, and an empty floating-point field becomes NaN. Not every format picks the same sentinel either: some reserve 0 as empty so that 1..255 stay valid. A decoder that assumes 255 means empty then reads every missing value as zero and treats real 255 values as missing.

One more distinction: an empty sentinel value is on the wire, taking its full bytes. A field that did not exist in an older sender's schema version is not on the wire at all; schema versioning covers how decoders keep those two apart.

How common formats handle it

Each binary format picks a point in this design space by what it optimizes for: the lowest decoding latency, small messages that can change shape, or fast scans over stored data. Each has its own article with the details.

SBE

SBE (Simple Binary Encoding) optimizes for decoding at fixed offsets on live trading feeds.

  • Byte order: set once for the whole schema in its root element, littleEndian unless the schema says bigEndian (the XML message schema).
  • Padding: none unless the schema asks for it, so odd offsets like freeQtyDelta at 17 in Binance Spot API's BalanceUpdateEvent are normal. A schema adds padding with an explicit field offset or a larger blockLength (how an SBE message is laid out).
  • Framing: outside SBE. The 8-byte message header carries blockLength, the size of the root block only, not of the whole message. Venues add their own frame, from a WebSocket frame on Binance to a packet header with a MsgSize before each message on CME MDP 3.0 (the message header).
  • Null values: fields are required, optional (a sentinel, by default the extreme value of the type, unless the schema sets its own nullValue) or constant (no bytes on the wire) (presence and null values).

Open the optional orderListId in the Market Data Visualizer

Protobuf

Protobuf (Protocol Buffers) gives up fixed offsets for small, flexible messages between services, where fields can be left out or added freely (how Protobuf writes a message).

  • Byte order: fixed-width numbers (fixed32, fixed64, float, double) are always little-endian. Ordinary integers (int32, int64, uint64, sint64) are varints: 7 bits of the number per byte, lowest group first, with the top bit of each byte saying whether another follows. 6290000 takes 4 bytes that way (d0 f4 ff 02) instead of 8.
  • Padding: none. Every present field is a tag (field number and wire type) followed by its value, back to back; there are no fixed offsets to align.
  • Framing: a Protobuf message does not mark its own end, and two messages written back to back read as one merged message. Streams add a length before each message, usually a varint; gRPC puts a 5-byte header (a 1-byte compression flag and a 4-byte big-endian length) before each.
  • Null values: unset fields are left out. With implicit presence (proto3's default for plain scalar fields), a field set to 0 or "" is left out too, so a reader cannot tell "unset" from "zero". With explicit presence (optional fields, message fields, oneof, and the default in Protobuf Editions 2023), a field set to 0 is written, so the reader can tell them apart.

Parquet

Parquet stores tables on disk, column by column, for compression and for reading only the columns a query needs.

  • Byte order: fixed by where a number is. The 4-byte footer length and PLAIN-encoded INT32, INT64, FLOAT and DOUBLE values are little-endian, whereas a DECIMAL stored in FIXED_LEN_BYTE_ARRAY (such as the 9-byte notional in the bundled trades sample) is a big-endian two's-complement integer.
  • Padding: no per-row padding; a column's values sit together in encoded, compressed pages (Parquet encodings and compression).
  • Framing: the whole file is framed. It opens and closes with the 4 ASCII bytes PAR1, with the 4-byte little-endian footer length just before the closing PAR1; a reader checks both ends and goes to the footer for the offset and size of every column chunk (how a Parquet file is laid out).
  • Null values: columns are required, optional or repeated. An optional column stores a small definition level for each value beside the data (compressed with run-length and bit-packing) and leaves nulls out of the values, so a column keeps every value its type can hold.

Sources

Reviewed by Structure on 2026-11-24. Last changed 2026-11-24.