Lesson 07: Google Protocol Buffers vs. Text Serialization (JSON / XML)
Industrial edge devices continuously capture process variables from plant-floor machinery and transmit high-frequency telemetry across factory subnets, wide-area cellular links, and enterprise message brokers. In distributed Supervisory Control and Data Acquisition (SCADA) and Industrial Internet of Things (IIoT) platforms, the serialization format dictates system scalability, CPU utilization, garbage collection overhead, and cellular bandwidth expenses. This lesson examines the low-level computer science foundations of serialization, analyzes IEEE 754 floating-point wire encoding and endianness hazards, details the bit-packing and Base-128 Varint mechanics of Google Protocol Buffers (Protobuf), and establishes the architectural governance required to enforce schema contracts in industrial systems.
1. Physical Edge Reality: The Serialization Bottleneck & Endianness
Plant-floor edge gateways and programmable logic controllers (PLCs) operate under stringent computational and physical bandwidth constraints. Transmitting high-density sensor telemetry requires transforming in-memory processor structures into serialized byte streams suitable for industrial communication media (Jaloudi, 2019; Šenk et al., 2026):
Core Computer Science Foundations
- Serialization & Deserialization:
- Serialization (Marshalling): The deterministic translation of in-memory data structures (objects, structs, arrays) into a contiguous stream of binary or textual bytes for network transit or persistent disk storage.
- Deserialization (Unmarshalling): The inverse operation—reconstructing in-memory data structures from the received byte stream. Textual serializations (JSON, XML) convert numerical quantities into human-readable ASCII/UTF-8 character sequences (e.g., the number
4.25is encoded as four distinct character bytes:0x34,0x2E,0x32,0x35), forcing processors to perform expensive lexical analysis and floating-point reconstruction upon receipt. Binary formats encode raw bit representations directly into machine bytes.
- Endianness (Byte Order):
- When a numeric primitive exceeds a single byte (such as a 16-bit integer, 32-bit float, or 64-bit timestamp), communicating endpoints must agree on the memory layout order of those bytes:
- Little-Endian (LE): The least significant byte (LSB) is stored at the lowest memory address or transmitted first. Standard across x86, x86-64, modern ARM processors, and Protobuf fixed-length numerical fields.
- Big-Endian (BE): The most significant byte (MSB) is stored at the lowest memory address or transmitted first. Historically designated as "Network Byte Order" in standard TCP/IP networking and universally enforced in industrial serial/Ethernet fieldbuses such as Modbus RTU/TCP.
The Physical Edge Infrastructure
In brownfield and greenfield automation environments, edge computation is executed by ruggedized DIN-rail microcontrollers and industrial gateway computers interfacing with sensors and legacy fieldbuses:
The physical edge hardware executing these serialization routines is shown in Figure 07.1, contrasting embedded microcontroller gateways (Figure 07.1(a)) with cellular Sparkplug B edge computers (Figure 07.1(b)):
(a) Low-Power Embedded Edge Gateway / Microcontroller (Kunbus Revolution Pi Core / ESP32)
(b) Industrial Cellular / Ethernet Sparkplug B Edge Computer (Moxa UC-8100A)
IEEE 754 Floating-Point Anatomy and Endianness Hazards
Industrial pressure transmitters, flowmeters, and power meters represent continuous analog measurements using the IEEE 754 Single-Precision (32-bit) floating-point standard. A 32-bit float partitions its bits into three distinct fields:
Where:
- Sign ($s$, Bit 31, 1 bit):
0denotes positive,1denotes negative. - Biased Exponent ($e$, Bits 30..23, 8 bits): Stored with a constant offset (bias) of $127$.
- Mantissa / Fraction ($m$, Bits 22..0, 23 bits): The fractional part of the significand. An implicit leading
1.is assumed and never transmitted over the wire, providing 24 bits of effective precision ($\approx 7.22$ decimal digits).
Consider an analog pressure reading of $4.25\text{ bar}$:
- $4.25_{10} = 4 + \frac{1}{4} = 100.01_2 = 1.0001_2 \times 2^2$
- Sign $s = 0$
- Exponent $e = 2 + 127 = 129_{10} = 10000001_2$
- Mantissa $m = .00010000000000000000000_2$
- Assembled 32-bit binary:
0100 0000 1000 1000 0000 0000 0000 0000$\to$0x40880000
The Silent Endianness Inversion Hazard
When transmitting 0x40880000:
- Protobuf wire transmission (Little-Endian) emits the least significant byte first:
00 00 88 40. - Modbus TCP fieldbus transmission (Big-Endian; see Lesson 02) emits the most significant register first:
40 88 00 00.
If a supervisory edge driver reads the four bytes of a Little-Endian Protobuf payload (00 00 88 40) assuming Big-Endian Modbus ordering, the receiver extracts:
- Sign $s = 0$
- Exponent $e = 00000000_2 = 0$ (subnormal number representation)
- Mantissa $m = 00000000100010000100000_2$
- Decoded Result: $\approx 4.89 \times 10^{-41}\text{ bar}$
Operational Failure Mode: The receiving system does not throw a parser crash, a syntax error, or an invalid frame exception. The deserializer outputs a mathematically valid, yet catastrophic near-zero reading. If bound to a boiler safety interlock or chemical feed pump, the control logic triggers emergency runaway heating under the false assumption that line pressure has collapsed.
This 32-bit floating-point bit layout, along with the Little-Endian byte sequence and Modbus register alignment risks, is diagrammed in Figure 07.2:
Figure 07.2: Bit-Level Architecture of IEEE 754 Single-Precision Floating-Point Values (Sign, Biased Exponent, Fraction), Little-Endian Wire Ordering, and Modbus Register Inversion Risks.
2. Textual Serialization Bottlenecks: XML, JSON & Hex Wire Profiles
Industrial telemetry environments aggregate tens of thousands of sensor readings per second across distributed plant networks. Three primary serialization paradigms dominate industrial computing:
- XML (Extensible Markup Language): Heavily utilized in legacy enterprise buses, SOAP endpoints, OPC XML-DA, and substation configuration standards (IEC 61850 SCL). Highly verbose, requiring explicit opening and closing character tags for every attribute.
- JSON (JavaScript Object Notation): The ubiquitous standard for web REST APIs and web browser applications. Highly flexible and human-readable, but carries significant string and punctuation overhead.
- Google Protocol Buffers (Protobuf): A compact, strongly typed binary format utilizing compiled schemas. Mandated by modern IIoT standards including Eclipse Sparkplug B v3.0 (Šenk et al., 2026; analyzed in Lesson 06).
Anatomy of Packet Analyzer Hex Dumps
Consider transmitting an industrial telemetry sample containing an integer timestamp, a metric alias, and an analog pressure value:
JSON Over-the-Wire Transmission (UTF-8 Text)
{"timestamp":1774523400,"alias":101,"value":4.25}
Inspecting the raw packet bytes in Wireshark reveals 49 bytes:
- Field Names (19 Bytes / 38.8%):
"timestamp","alias","value"are repeated character-for-for-character in every single emitted packet. - Structural Syntax (13 Bytes / 26.5%): Braces
{,}, quotation marks", colons:, and commas,. - Measurement Values (17 Bytes / 34.7%): The actual payload data (
1774523400,101,4.25).
Non-Data Overhead: $65.3\%$ of the transmitted packet consists of structural scaffolding and field names, meaning nearly two-thirds of available network capacity is consumed by metadata.
The Modbus-to-JSON Hex Encoding Trap
In many ad-hoc IoT integrations, developers convert raw Modbus binary frames directly into hexadecimal ASCII strings inside JSON payloads to achieve interoperability (Jaloudi, 2019). Each raw byte (e.g., 0x00 0x01) is expanded into two UTF-8 characters ("0001"), requiring two bytes on the wire (0x30 0x30 0x30 0x31) per physical byte. This naive mapping doubles the payload size before accounting for JSON framing overhead.
Protocol Buffers Over-the-Wire Transmission (Binary)
The identical measurement encoded via Protobuf occupies 13 bytes:
08 88 A8 94 CE 06 10 65 1D 00 00 88 40
- Tag
0x08+ 5-byte Varint timestamp (88 A8 94 CE 06): 6 Bytes - Tag
0x10+ 1-byte Varint alias (65): 2 Bytes - Tag
0x1D+ 4-byte IEEE 754 float (00 00 88 40): 5 Bytes
Protobuf achieves an immediate $73.5\%$ byte reduction relative to JSON and an $85.1\%$ reduction relative to XML ($87\text{ bytes}$) without executing runtime compression algorithms.
This dramatic difference in wire efficiency—comparing 49 bytes of repetitive JSON metadata against 13 bytes of compact binary Protobuf—is illustrated in Figure 07.3:
Figure 07.3: Packet Analyzer Wire Comparison — 49 Bytes of Repetitive UTF-8 JSON Syntax vs. 13 Bytes of Compact Protocol Buffers Binary Payload Encoding the Same Industrial Metric.
Bandwidth Compounding at Scale
Consider a medium-scale chemical plant publishing 10,000 process metrics per second to an enterprise broker over a metered cellular backhaul:
- JSON ($49\text{ B/metric}$):
- Protobuf ($13\text{ B/metric}$):
Over a metered industrial SIM tier costing $\$5.00$ per gigabyte over quota, JSON introduces an operational expenditure penalty of exceeding **$\$4{,}500$ per month per site** solely in uncompressed string syntax overhead.
3. Binary Mechanics: Varint Encoding, TLV Packing & ZigZag Math
Protobuf achieves compact payloads not through general-purpose compression algorithms (such as DEFLATE or Zstandard, which consume significant CPU cycles on microcontrollers), but through Tag-Length-Value (TLV) packing and Base-128 Varint variable-length integer encoding.
Field Tag Bit Encoding
In JSON and XML, every transmission includes the field key string. In Protobuf, field names exist exclusively in the shared .proto schema. The wire protocol transmits only a numeric Tag Byte combining the compiled Field Number and the Wire Type:
Because the wire type occupies the 3 least significant bits ($2^3 = 8$), Protobuf supports eight fundamental wire types, four of which remain active in modern proto3:
| Wire Type ID | Semantic Format | Target Data Types | Description |
|---|---|---|---|
0 | Varint | int32, int64, uint32, uint64, sint32, sint64, bool, enum | Variable-length Base-128 integer encoding (1 to 10 bytes). |
1 | 64-bit | fixed64, sfixed64, double | Fixed 8-byte little-endian storage (used for high-precision double floats). |
2 | Length-delimited | string, bytes, embedded submessages, packed repeated fields | Prefixed with a Varint length byte, followed by $N$ raw bytes. |
5 | 32-bit | fixed32, sfixed32, float | Fixed 4-byte little-endian storage (used for standard IEEE 754 single floats). |
Tag Byte Evaluation
- Field 1 (Timestamp,
uint64, Wire Type 0):
- Field 2 (Alias,
uint32, Wire Type 0):
- Field 3 (Value,
float, Wire Type 5):
Field numbers $1$ through $15$ encode their entire Tag into a single byte. Field numbers $16$ through $2047$ require two bytes. In industrial SCADA profiles, high-frequency metrics are strictly assigned field numbers $1..15$ to enforce single-byte header efficiency.
Varint Base-128 Integer Mechanics
Standard integers in C, Rust, and Java occupy fixed memory allocations ($32\text{ bits} = 4\text{ bytes}$, $64\text{ bits} = 8\text{ bytes}$), even when storing small quantities like $1$ or $42$. Protobuf utilizes Varints (Variable-Length Quantities) to serialize integers using only as many bytes as mathematically necessary:
- Each byte dedicates its 7 least significant bits (Bits 6..0) to payload data.
- The Most Significant Bit (Bit 7, MSB) serves as a continuation flag:
- $\text{MSB} = 1$: Additional bytes follow in the stream.
- $\text{MSB} = 0$: Terminating byte of the current integer.
Step-by-Step Encoding Walkthrough: Timestamp $1{,}774{,}523{,}400$
- Convert to Raw Binary:
- Partition into 7-Bit Groups (Right-to-Left):
- Group 0 ($g_0$, bits 0..6):
0001000($8$) - Group 1 ($g_1$, bits 7..13):
0101000($40$) - Group 2 ($g_2$, bits 14..20):
0010100($20$) - Group 3 ($g_3$, bits 21..27):
1001110($78$) - Group 4 ($g_4$, bits 28..30):
0000110($6$)
- Reverse Order (Emit Least-Significant Group First):
- Prepend MSB Continuation Flags:
- $g_0$: Prepend
1$\implies \mathbf{1}0001000_2 = \text{0x88}$ - $g_1$: Prepend
1$\implies \mathbf{1}0101000_2 = \text{0xA8}$ - $g_2$: Prepend
1$\implies \mathbf{1}0010100_2 = \text{0x94}$ - $g_3$: Prepend
1$\implies \mathbf{1}1001110_2 = \text{0xCE}$ - $g_4$: Prepend
0(final byte) $\implies \mathbf{0}0000110_2 = \text{0x06}$
- Final Wire Stream:
Deserialization Loop & Reconstruction
The receiver reconstructs the native numerical integer via a non-allocating bitwise shift loop:
function decodeVarint(bytes: Uint8Array): bigint {
let result = 0n;
let shift = 0n;
for (const byte of bytes) {
result |= BigInt(byte & 0x7F) << shift;
if ((byte & 0x80) === 0) break; // MSB is 0: end of sequence
shift += 7n;
}
return result;
}
Reconstruction arithmetic:
The complete step-by-step transformation from 31-bit binary representation down to five Base-128 Varint wire bytes is detailed in Figure 07.4:
Figure 07.4: Step-by-Step Bit Walkthrough of Protobuf Varint Base-128 Encoding for Timestamp 1,774,523,400 across 5 Bytes, MSB Continuation Flags, and Receiver Reconstruction Loop.
ZigZag Encoding for Signed Quantities
Standard two's complement integer representation stores negative numbers with their sign bit (Bit 31 or Bit 63) set to 1. If $-1$ is encoded directly into a standard 64-bit Varint (int64), all 64 bits are high (0xFFFFFFFFFFFFFFFF), forcing the Varint encoder to emit the theoretical maximum of 10 bytes on the wire:
In industrial telemetry, outdoor ambient temperatures and differential pressures routinely oscillate around zero (e.g., $-2^\circ\text{C}$, $-5\text{ mbar}$). Encoding small negative process variables into 10-byte payloads destroys edge bandwidth.
Protobuf resolves this through ZigZag Encoding on sint32 and sint64 data types. ZigZag maps signed numbers to positive unsigned integers by interleaving positive and negative quantities across the number line:
| Signed Original Value | ZigZag Encoded Equivalent | Wire Bytes via Varint |
|---|---|---|
0 | 0 | 1 Byte (0x00) |
-1 | 1 | 1 Byte (0x01) |
1 | 2 | 1 Byte (0x02) |
-2 | 3 | 1 Byte (0x03) |
2 | 4 | 1 Byte (0x04) |
-5 | 9 | 1 Byte (0x09) |
By interleaving signs, small negative process values (e.g., $-5^\circ\text{C} \to 9$) consume only a single byte on the physical wire instead of ten.
4. Architectural Decoupling: The Schema as the Authoritative Codebook
Protobuf payloads achieve density because field identity, data types, and semantic names never travel across the network. The compiled .proto contract functions as a shared cryptographic codebook held independently by both the edge publisher and enterprise subscriber.
The Schema Contract Specification
Both endpoints compile an identical contract definition using protoc:
syntax = "proto3";
package meridian.telemetry.v1;
message Metric {
uint64 timestamp = 1; // milliseconds since epoch
uint32 alias = 2; // UNS tag index
float value = 3; // scaled process reading
}
Forward & Backward Schema Evolution Mechanics
Industrial facilities operate on 20-year lifecycles. It is operationally impossible to shut down an entire manufacturing plant to simultaneously upgrade edge firmware and central SCADA servers. Protobuf enforces deterministic evolution rules:
Forward Compatibility: v2 Producer $\to$ v1 Consumer
Suppose a newly commissioned smart transmitter publishes a v2 message that appends engineering unit strings:
message Metric {
uint64 timestamp = 1;
uint32 alias = 2;
float value = 3;
string unit = 4; // Added in v2
}
When a legacy v1 receiver (which only knows fields 1..3) intercepts this wire stream:
- The v1 decoder processes tags
0x08,0x10, and0x1D, extracting fields 1, 2, and 3 without error. - The decoder reads tag
0x22:
- Because Field 4 is absent from its compiled v1 schema, the decoder checks the Wire Type.
- Seeing Wire Type
2, it reads the subsequent length byte (0x03) and skips exactly 3 bytes ahead in the stream. - Result: The decoder resumes parsing without raising a fatal parser exception, preserving uninterrupted operation. In modern protobuf runtimes, unknown fields are preserved in raw byte arrays for lossless pass-through routing.
This schema decoupling and forward-compatible tag-skipping mechanism are diagrammed in Figure 07.5:
Figure 07.5: The .proto Contract as the Shared Codebook — Zero Field Name Wire Transmission, Decoder Forward Compatibility via Tag Skipping, and Bounded-Context Language Governance.
Enterprise Governance Rules for Protobuf Schemas
To prevent silent operational failures, engineering teams must enforce three inviolable invariants:
- Never Reorder or Renumber Fields: Field numbers are permanent wire identifiers. Altering a field number instantly causes older decoders to map data into wrong internal variables.
- Never Alter Wire Types: Changing a field from
int32(Type 0) tofixed32(Type 5) corrupts the wire parsing stream, causing deserializers to desynchronize bit boundaries. - Enforce
reservedon Deprecated Identifiers: When a metric field is permanently retired, its field number and name must be explicitly reserved in the.protofile to prevent future developers from reusing the ID:
message Metric {
reserved 4, 7 to 9;
reserved "unit", "raw_adc";
// ...
}
- The Bounded-Context Published Language (ISO/IEC 29110): In Domain-Driven Design (DDD), a versioned
.protoschema operates as an immutable Published Language contract bridging the Operational Technology (OT) edge domain and the Information Technology (IT) enterprise domain (see Lesson 20).
5. Performance Benchmarks, Memory Allocation & Platform Tradeoffs
Choosing a serialization technology requires balancing network wire footprint, CPU parsing overhead, memory allocation churn, and developer ergonomics:
| Engineering Parameter | XML (SOAP / SCL) | JSON (REST / WebSockets) | Google Protocol Buffers (Protobuf) |
|---|---|---|---|
| Data Representation | Human-readable ASCII / UTF-8 text | Human-readable UTF-8 text | Compact, strongly typed binary stream |
| Schema Enforcement | XSD (heavy, optional at runtime) | JSON Schema (external, ad-hoc) | .proto strictly compiled into code |
| Wire Footprint Baseline | Massive ($100\%$ baseline) | Medium ($30\%\text{--}50\%$ of XML) | Minimal ($10\%\text{--}25\%$ of XML) |
| CPU Parsing Overhead | Extremely High ($10\times\text{--}20\times$) | Moderate ($3\times\text{--}5\times$) | Minimal ($1\times$ baseline) |
| Zero-Copy Deserialization | Impossible (strings must be allocated) | Impossible (strings must be tokenized) | Native support in C, C++, Rust, and Go |
| Garbage Collector Churn | Severe heap allocation per tag | High allocation for strings & objects | Zero-allocation buffer slicing |
| Browser Native Parsing | Supported (DOMParser) | Native C++ implementation (JSON.parse) | Requires WebAssembly or JS runtime library |
| Human Debuggability | Trivial (readable in text editors) | Trivial (readable in terminal / browser) | Requires .proto schema or protoc --decode_raw |
Zero-Copy Ingestion in SCADA Historians
In high-throughput SCADA ingestion engines (handling $>1{,}000{,}000\text{ events/sec}$; see Lesson 09), deserialization bottlenecks are dominated by dynamic heap allocation and Garbage Collection (GC) pauses:
- The JSON Penalty: Parsing a JSON payload in Node.js or Java requires the runtime to scan character tokens, allocate heap memory for property strings (
"timestamp"), allocate dictionary objects, and parse float strings. At high event rates, this triggers frequent GC sweep cycles, causing ingestion latency spikes and packet buffer drops. - The Protobuf Zero-Copy Advantage: In systems programming languages (Go, Rust, C++), Protobuf deserializers construct structs by pointing directly to slices of the existing network receive buffer. String fields and binary blobs do not require memory copies; the struct merely holds a memory pointer and a byte length offset. This eliminates dynamic memory allocations entirely on the ingestion hot path.
Architectural Boundary Pattern
The current requirements describe a dual-serialization boundary as an architectural direction, not an implemented contract:
- Southbound Telemetry (Edge $\to$ Broker $\to$ Ingestion Services): The candidate stack includes Protobuf over MQTT Sparkplug B v3.0 (see Lesson 05), subject to Phase 2 contract and ADR work. Its trade-offs include reduced field bandwidth and stronger schema typing.
- Northbound Visualization (Core Services $\to$ Browser Web Clients): Standard JSON telemetry events over secure WebSocket delivery are one candidate integration contract (see Lesson 11 and Lesson 12). GraphQL and zero-copy ingestion are lesson options rather than universal requirements.
6. Active-Recall Assessments
Why does Protobuf achieve significantly faster deserialization than JSON even on powerful multi-core server processors?
What is the operational failure mode if Little-Endian Protobuf float bytes are read by a legacy Big-Endian Modbus parser?
0x40880000. Protobuf Little-Endian wire transmission emits the least significant byte first: 00 00 88 40. If a receiver interprets these bytes in Big-Endian order, the sign bit remains 0, but the exponent drops to 0 and the mantissa shifts, silently decoding the value to $\approx 4.89 \times 10^{-41}\text{ bar}$. Because this represents a mathematically valid floating-point number, no parser exception or network error is raised; the control system silently operates on corrupted near-zero data (Jaloudi, 2019).What is ZigZag encoding and why is it essential for signed industrial telemetry variables like temperature?
1 for all negative numbers. When serialized using standard Varint Base-128 encoding, a 64-bit negative integer (like $-1$) has all 64 bits high, requiring a full 10 bytes on the wire. ZigZag encoding maps signed integers to unsigned values by interleaving signs ($n \mapsto (n \ll 1) \oplus (n \gg 31)$), mapping $0 \to 0, -1 \to 1, 1 \to 2, -2 \to 3, -5 \to 9$. This ensures small negative process readings (e.g., $-5^\circ\text{C}$) occupy only a single byte on the physical wire instead of ten.How does Protobuf wire type 2 enable forward-compatible schema evolution when edge gateways deploy updated .proto versions before central SCADA hosts are upgraded?
2 denotes Length-delimited data (strings, bytes, submessages). If an edge gateway transmits a new v2 field unknown to an older v1 SCADA receiver, the v1 decoder reads the Tag Byte, determines the field is unknown, reads the subsequent Varint length byte ($L$), and skips exactly $L$ bytes forward in the stream. The decoder continues processing subsequent known fields without crashing or corrupting data alignment.Why might an industrial architecture maintain dual serialization paradigms (Protobuf southbound, JSON northbound) rather than forcing Protobuf directly into browser JavaScript clients?
JSON.parse()) parse JSON faster than non-native JavaScript Protobuf libraries, while enabling transparent network inspection in web developer tools without requiring client-side compiled .proto bundle bloat.