Skip to content

Lesson 2 of 9 · 21m

Wire Types and Encoding Discipline

Define integer widths, endianness, canonical forms, string encodings, and numeric precision so implementations do not invent different protocols.

Most interoperability failures are not exotic. They are one implementation reading network-byte-order integers while another writes host-byte-order integers, one accepting NaN in JSON while another rejects it, or one treating timestamps as seconds while another uses milliseconds. A protocol does not become real when the first implementation works; it becomes real when independent implementations agree without reading each other's source code.

Fix the byte-level meaning of numbers

Write down width, signedness, endianness, and valid range for every numeric field:

sequence      uint64, big-endian, monotonically increasing per stream
length        uint32, big-endian, maximum 16777215
error_code    uint16, big-endian, registry-defined
sample_rate   uint32, big-endian, hertz, nonzero

Do not say "integer." In C that means something different by platform; in Python it means arbitrary precision; in JavaScript it means a floating-point number with a 53-bit exact integer ceiling.

Choose canonical representations when equality matters

If peers compare, hash, sign, cache, or deduplicate messages, representation matters. JSON is especially dangerous because these encode equivalent data differently:

{"a":1,"b":2}
{"b":2,"a":1}
{"a":1.0,"b":2}
{"a":1,"b":2,"a":3}

If you need cryptographic integrity over structured data, either define a canonical encoding or sign the exact byte representation. Never sign "the object" as if serialization were a mathematical function.

Strings are bytes plus rules

Specify Unicode encoding, normalization behavior, maximum byte length, maximum character length if different, and rejection of malformed input. UTF-8 is the sane default for new designs. Do not case-fold, trim, normalize, or parse structured values inside opaque identifiers unless those transformations are part of the protocol contract.

Time is a precision trap

A timestamp field needs units, epoch, clock source, and leap behavior. Prefer nanoseconds since the Unix epoch in an unsigned fixed-width integer for machine protocols, or RFC 3339 strings for human-facing control protocols. Avoid bare floats for time: precision degrades and implementations round differently.

Schemas help but do not replace rules

Protocol Buffers, CDDL, ASN.1, Avro, and JSON Schema can express shape. They rarely capture every invariant: sequence monotonicity, cross-field constraints, acceptable unknown fields, or resource limits. A protocol specification is the encoding plus the state and validation rules around it.

message Frame {
  uint32 type = 1;
  uint64 sequence = 2;
  bytes payload = 3;
}

That schema does not say whether sequence can restart, what unknown type means, or how large payload may be. Those omissions are where implementations diverge.

Field notes

  • JavaScript clients cannot safely represent every uint64; use strings for large identifiers crossing JSON boundaries.
  • Floating-point latency, price, and probability fields invite equality and rounding bugs; prefer scaled integers when exactness matters.
  • Endianness mistakes often pass local tests because both sides use the same wrong implementation.
  • Write encode/decode golden vectors into the repository and test every implementation against identical bytes.

Was this lesson helpful?

Sign in to rate