Lesson 07: Google Protocol Buffers vs. Text Serialization (JSON / XML)
←→

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

  1. Serialization & Deserialization:
  1. Endianness (Byte Order):

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)):

Low-Power Embedded Edge Gateway / Microcontroller (Kunbus Revolution Pi Core / ESP32)

Photo Unavailable

Search: "Low-Power Embedded Edge Gateway / Microcontroller"


(a) Low-Power Embedded Edge Gateway / Microcontroller (Kunbus Revolution Pi Core / ESP32)
Industrial Cellular / Ethernet Sparkplug B Edge Computer (Moxa UC-8100A)

Photo Unavailable

Search: "Industrial Cellular / Ethernet Sparkplug B Edge Computer"


(b) Industrial Cellular / Ethernet Sparkplug B Edge Computer (Moxa UC-8100A)
Figure 07.1: Physical Edge Telemetry Infrastructure: (a) Embedded DIN-rail gateway converting local Modbus/CAN bus registers to lightweight binary wire payloads; (b) Industrial Sparkplug B edge computer packaging plant telemetry over bandwidth-constrained cellular links.

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:

$$\text{Value} = (-1)^s \times (1 + m) \times 2^{e - 127}$$

Where:

Consider an analog pressure reading of $4.25\text{ bar}$:

The Silent Endianness Inversion Hazard

When transmitting 0x40880000:

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:

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:

07-ieee754-float-anatomy.svg

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:

  1. 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.
  2. 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.
  3. 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:

$$\text{Sample} = \{\text{timestamp}: 1774523400, \text{alias}: 101, \text{value}: 4.25\}$$

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:

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

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:

07-wire-hex-dump-comparison.svg

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:

$$10{,}000 \times 49\text{ B} = 490{,}000\text{ B/s} \approx 478.5\text{ KB/s} \implies 1.24\text{ TB / month}$$
$$10{,}000 \times 13\text{ B} = 130{,}000\text{ B/s} \approx 126.9\text{ KB/s} \implies 329\text{ GB / month}$$

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:

$$\text{Tag Byte} = (\text{Field\_Number} \ll 3) \mid \text{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 IDSemantic FormatTarget Data TypesDescription
0Varintint32, int64, uint32, uint64, sint32, sint64, bool, enumVariable-length Base-128 integer encoding (1 to 10 bytes).
164-bitfixed64, sfixed64, doubleFixed 8-byte little-endian storage (used for high-precision double floats).
2Length-delimitedstring, bytes, embedded submessages, packed repeated fieldsPrefixed with a Varint length byte, followed by $N$ raw bytes.
532-bitfixed32, sfixed32, floatFixed 4-byte little-endian storage (used for standard IEEE 754 single floats).

Tag Byte Evaluation

$$\text{Tag} = (1 \ll 3) \mid 0 = 8 \mid 0 = 8 = \text{0x08}$$
$$\text{Tag} = (2 \ll 3) \mid 0 = 16 \mid 0 = 16 = \text{0x10}$$
$$\text{Tag} = (3 \ll 3) \mid 5 = 24 \mid 5 = 29 = \text{0x1D}$$

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:

Step-by-Step Encoding Walkthrough: Timestamp $1{,}774{,}523{,}400$

  1. Convert to Raw Binary:
$$1{,}774{,}523{,}400_{10} = \text{0x69C51408} = 110\;1001110\;0010100\;0101000\;0001000_2\quad\text{(31 bits)}$$
  1. Partition into 7-Bit Groups (Right-to-Left):
  1. Reverse Order (Emit Least-Significant Group First):
$$\{g_0, g_1, g_2, g_3, g_4\} \implies \{0001000, 0101000, 0010100, 1001110, 0000110\}$$
  1. Prepend MSB Continuation Flags:
  1. Final Wire Stream:
$$\text{0x88 0xA8 0x94 0xCE 0x06}\quad\text{(5 bytes)}$$

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:

$$\Sigma = (8 \times 128^0) + (40 \times 128^1) + (20 \times 128^2) + (78 \times 128^3) + (6 \times 128^4) = 1{,}774{,}523{,}400$$

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:

07-varint-encoding-walkthrough.svg

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:

$$-1 \implies \text{FF FF FF FF FF FF FF FF FF 01}\quad\text{(10 Bytes!)}$$

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:

$$\text{ZigZag32}(n) = (n \ll 1) \oplus (n \gg 31)$$
$$\text{ZigZag64}(n) = (n \ll 1) \oplus (n \gg 63)$$
Signed Original ValueZigZag Encoded EquivalentWire Bytes via Varint
001 Byte (0x00)
-111 Byte (0x01)
121 Byte (0x02)
-231 Byte (0x03)
241 Byte (0x04)
-591 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:

  1. The v1 decoder processes tags 0x08, 0x10, and 0x1D, extracting fields 1, 2, and 3 without error.
  2. The decoder reads tag 0x22:
$$\text{Field} = 0x22 \gg 3 = 4,\quad \text{Wire Type} = 0x22 \ \& \ 7 = 2\ (\text{Length-delimited})$$
  1. Because Field 4 is absent from its compiled v1 schema, the decoder checks the Wire Type.
  2. Seeing Wire Type 2, it reads the subsequent length byte (0x03) and skips exactly 3 bytes ahead in the stream.
  3. 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:

07-shared-schema-codebook.svg

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:

  1. 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.
  2. Never Alter Wire Types: Changing a field from int32 (Type 0) to fixed32 (Type 5) corrupts the wire parsing stream, causing deserializers to desynchronize bit boundaries.
  3. Enforce reserved on Deprecated Identifiers: When a metric field is permanently retired, its field number and name must be explicitly reserved in the .proto file to prevent future developers from reusing the ID:
   message Metric {
     reserved 4, 7 to 9;
     reserved "unit", "raw_adc";
     // ...
   }
  1. The Bounded-Context Published Language (ISO/IEC 29110): In Domain-Driven Design (DDD), a versioned .proto schema 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 ParameterXML (SOAP / SCL)JSON (REST / WebSockets)Google Protocol Buffers (Protobuf)
Data RepresentationHuman-readable ASCII / UTF-8 textHuman-readable UTF-8 textCompact, strongly typed binary stream
Schema EnforcementXSD (heavy, optional at runtime)JSON Schema (external, ad-hoc).proto strictly compiled into code
Wire Footprint BaselineMassive ($100\%$ baseline)Medium ($30\%\text{--}50\%$ of XML)Minimal ($10\%\text{--}25\%$ of XML)
CPU Parsing OverheadExtremely High ($10\times\text{--}20\times$)Moderate ($3\times\text{--}5\times$)Minimal ($1\times$ baseline)
Zero-Copy DeserializationImpossible (strings must be allocated)Impossible (strings must be tokenized)Native support in C, C++, Rust, and Go
Garbage Collector ChurnSevere heap allocation per tagHigh allocation for strings & objectsZero-allocation buffer slicing
Browser Native ParsingSupported (DOMParser)Native C++ implementation (JSON.parse)Requires WebAssembly or JS runtime library
Human DebuggabilityTrivial (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:

Architectural Boundary Pattern

The current requirements describe a dual-serialization boundary as an architectural direction, not an implemented contract:


6. Active-Recall Assessments

Why does Protobuf achieve significantly faster deserialization than JSON even on powerful multi-core server processors?
JSON deserialization requires continuous text scanning, character-by-character lexical analysis, quote verification, string unescaping, dynamic heap allocation for object keys, and ASCII-to-binary floating-point parsing. In contrast, Protobuf deserialization is a direct memory unpacking process: field identifiers, data lengths, and data types are encoded as bit flags inside tags, allowing parsers to copy raw memory slices directly into native struct fields with zero string allocations and zero lexical scanning (Šenk et al., 2026).
What is the operational failure mode if Little-Endian Protobuf float bytes are read by a legacy Big-Endian Modbus parser?
An IEEE 754 float representing $4.25\text{ bar}$ has the 32-bit hex value 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?
Standard two's complement integer representation sets the most significant bit to 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?
Wire type 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?
Southbound industrial networks operate over constrained cellular/satellite links and embedded microcontrollers (ESP32, ARM) where binary compaction (74% savings) and zero-copy server ingestion are paramount (Freitas et al., 2025). Northbound browser clients execute JavaScript, where native browser C++ engines (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.