Explainer
Shrinking a Sparkplug JSON payload on a narrow link
Short answer
Casting the JSON to bytes changes the Sparkplug datatype tag, not the size: String and Bytes metrics both ride one protobuf length-delimited field with the same length prefix. The example's ~700-byte object minifies to ~400 bytes and shrinks further as typed metrics with aliases; with a 1460-byte IPv4 segment limit, the gain is bytes on the link, not packets.
On this page
- Is a Sparkplug Bytes metric smaller than a String metric?
- What sits on top of my JSON on the wire?
- Does a smaller payload reduce the packet count?
- Which bytes can come out of the JSON itself?
- Should the object become typed Sparkplug metrics instead?
- When does compressing the JSON pay off?
- How do I verify the savings end to end?
- So what should I ship on a narrow link?
- Questions asked
Casting the object to bytes and switching the Sparkplug metric from String to Bytes changes nothing on the wire: both datatypes carry their value in the same protobuf length-delimited field, so the example object below stays the same size either way. What shrinks it is the content. Minified, the roughly 700-byte object drops to roughly 400 bytes; mapped onto typed metrics with aliases it drops to a small fraction of that, and compression becomes a measured threshold decision rather than a default. None of this changes the packet count on a normal Ethernet path — everything here fits in one TCP segment — so the gain lands where it matters: bytes per second on a narrow or metered link.
Is a Sparkplug Bytes metric smaller than a String metric? #
No. The same JSON occupies the same protobuf length-delimited field either way — same field tag, same length prefix, same content bytes — so an object of about 700 bytes stays about 700 bytes whether the metric datatype is String or Bytes.
Sparkplug B encodes every payload as a protobuf message, and both datatypes store their value identically: one field tag, one varint length prefix, then the raw bytes. The metric's datatype field takes a different value for String than for Bytes, but it is a varint both times, so even that does not change size. Casting your JSON to bytes re-labels the container; it does not remove a byte of content.
Confirm it in one capture: publish the identical JSON once as a String metric and once as a Bytes metric on the same topic and compare the MQTT message lengths. They match.
Decision: keep plain JSON in the String metric. The one case where Bytes is the correct datatype is a compressed payload, because compressed output is binary and belongs in a binary field.
What sits on top of my JSON on the wire? #
About 40 bytes of IP and TCP headers, plus MQTT and protobuf framing that you measure once with a one-character publish — and the ceiling all of it must fit under is 1500 − 20 − 20 = 1460 bytes.
| Layer | What it adds | Notes |
|---|---|---|
| JSON text | about 700 B pretty-printed, about 400 B minified | the example object |
| Sparkplug payload | per metric: name or alias, datatype, timestamp, value length prefix, all as varints; payload-level timestamp and sequence number | small and fixed per metric |
| MQTT PUBLISH | 1 B fixed header, 1–4 B remaining length, 2 B topic length, topic string | QoS 1 or 2 adds a 2 B packet identifier; the topic runs spBv1.0/group/message_type/edge_node/device |
| TCP | 20 B header minimum | options add more |
| IPv4 | 20 B header minimum | — |
| Ethernet | frame header and FCS | outside the MTU; the 1500 B MTU covers the IP packet |
The remaining-length field is a variable-byte count of everything after it in the control packet, so it grows slightly as the payload grows. The protobuf framing around one metric is a handful of varints; the topic string is the biggest mover among the fixed parts, since a Sparkplug topic carries the group, message type, edge node and device. Measure your exact total with the one-character publish described under verification.
Does a smaller payload reduce the packet count? #
No — the example publish already travels as one segment, because its content plus framing sits far below the 1460-byte ceiling a 1500-byte MTU leaves for a TCP segment.
Shrinking the object reduces bytes per packet, not the number of packets. What the link carries is bytes per second = (content + framing + headers) × messages per second, and both factors are yours to cut.
Worked example at an illustrative 10 messages per second: minifying saves 700 − 400 = 300 B per message; 300 B × 10/s = 3000 B/s; 3000 B/s × 8 bit/B = 24 kbit/s. On an unmetered plant LAN that is noise; on a cellular link metered by volume it is the budget. The cut also compounds through recovery, because MQTT store and forward backfill after a connection outage empties its queue at full message size once the link returns — a smaller publish shrinks the backlog too.
Check it once in Wireshark: mqtt.msgtype == 3 shows each PUBLISH; confirm one arrives per publish and tcp.len sits far below the segment limit.
Which bytes can come out of the JSON itself? #
About 300 of the 700 bytes: minifying alone takes the object from about 700 bytes to about 400, and most of the rest is duplicated keys, a floating-point artifact, derived data and routing fields no subscriber reads.
| Field in the example | Action |
|---|---|
| Whitespace and indentation | Minify: about 700 B to about 400 B, a 300/700 ≈ 43 percent cut |
battery = 3.2940600000000004 |
Round to the resolution the sensor actually reports; the trailing digits are a floating-point artifact, 18 characters of it |
type, present twice in the payload |
Keep one; a parser keeps the last (receive_packet), so the first is already lost |
addr and original.mac |
The same 17-character address twice; keep one |
data, 13 decimal values |
Drop it if sensor_data is derived from it and no consumer needs the raw frame |
receive_options (ack, broadcast, empty type) |
Drop it if no subscriber reads it |
topic and _msgid |
The MQTT topic already carries routing; the message ID is a runtime artifact |
Do this pass before touching the encoding, and diff the trimmed object against the original so every field a subscriber reads survives.
Should the object become typed Sparkplug metrics instead? #
Yes, when the schema is fixed — and this is the biggest cut. Each value rides its own typed protobuf field, where a float costs 4 bytes, names give way to small integer aliases, and quotes, braces and decimal text disappear entirely.
Map the fixed fields directly: humidity, temperature and battery become Float metrics; counter, firmware, sensor_type and nodeId become small integer metrics. A float value occupies 4 bytes and each of those integers fits one varint byte, so the example's seven values need 3 × 4 + 4 × 1 = 16 bytes of value storage — against roughly 80 bytes of text for the battery key with its 18-character value and the sensor_data object alone.
Publish each metric's full name once, in the birth payload; afterwards data messages carry only the small integer alias. Publish only what changed — report by exception — so a steady process sends almost nothing. Any Sparkplug host decodes typed metrics without custom parsing, which a JSON string in one metric never gives you. The alias mapping lives in the birth, and a host that missed it cannot resolve what follows — the failure that why the Rebirth metric arrives without its Sparkplug alias walks through; a rebirth re-sends the full mapping.
The trade is flexibility: a new field means editing the metric list and re-issuing the birth, where JSON accepts a new key silently. Keep a String metric for the part of the object whose structure genuinely varies, and type the rest.
When does compressing the JSON pay off? #
Only above a threshold you measure, and only in a Bytes metric: DEFLATE-style compression removes repeated key names and structure, but the gzip container alone adds a fixed 18 bytes, so a few hundred bytes of JSON gains little and can even grow.
Set the threshold from your own messages, not from a rule of thumb:
- Compress representative messages, including the largest you send, and log raw length against compressed length.
- Set the threshold where compressed length is reliably smaller; send raw below it, compressed above it.
- Mark compressed messages so the subscriber knows to decompress before parsing. The Sparkplug specification defines a compressed-payload convention; take its exact form from the spec revision you implement so other hosts stay interoperable.
- Measure compression CPU time on the publishing node; a small fanless gateway can pay more in cycles than it saves on the link.
Compressed bytes are binary: they ride a Bytes metric, never a String metric. Decision for the example: at about 400 bytes minified, typed metrics beat compression outright — hold conditional compression for payloads that stay large after trimming, such as arrays of many samples in one message.
How do I verify the savings end to end? #
Capture on the publisher side, measure your fixed framing with a one-character publish, apply each change one at a time and record the length after each, and confirm the subscriber decodes every variant before you switch.
- Capture on the publisher's interface with
tcp.port == 1883; where TLS terminates at the broker, capture the plaintext side so the MQTT fields stay readable. - Publish the baseline object as a String metric and record the MQTT message length and the TCP payload length.
- Publish a one-character string on the same topic with the same metric framing: that message is your fixed overhead — protobuf framing, MQTT header and topic, TCP and IP headers. The gap between it and the baseline is your content.
- Apply minify, then the field trim, then typed metrics, then compression, recording the length after each step so each gain is measured on its own.
- Replay the worst-case message and confirm the TCP payload stays below 1460 B, so it still moves as one segment.
- Have the subscriber decode every variant, including the compressed one, and compare the result byte-for-byte with the source object.
Switch the production encoding only after step 6 passes: a message that got smaller and cannot be decoded is data loss, not a saving.
So what should I ship on a narrow link? #
Minified, trimmed JSON as the stopgap — about 400 bytes — and typed metrics with aliases plus report by exception as the target, with conditional compression in a Bytes metric held back for payloads that stay large after trimming.
Work them in order of payoff: minify for about 43 percent off this object, trim the duplicated and unread fields, then type the fixed schema — 16 bytes of value storage where the text needed about 80, and whole messages suppressed whenever nothing changes — and only then a compression threshold for what remains. String versus Bytes decides nothing about size; it decides only whether the payload is text or compressed binary. The packet count stays at one segment on a standard 1500-byte-MTU path either way, so the saving shows up as bytes per second, which is exactly what a narrow link is short of.
Questions asked
Why does my compressed JSON fail to decode when it is sent as a Sparkplug String metric?
Because compression output is binary: it holds byte sequences that are not valid UTF-8 text, and a String metric is a text field. Sent that way, the subscriber decodes damaged characters or rejects the message. Put compressed payloads in a Bytes metric and decompress on the subscriber, and remember the gzip container adds a fixed 18 bytes, so short messages can come out larger than they went in.
How do I measure the fixed framing overhead of my own setup?
Publish a one-character string on the same topic, with the same metric framing, and capture it. That message carries no content, so its MQTT length is your fixed overhead for this setup: protobuf framing, the MQTT fixed header and remaining length, the topic string, and the TCP and IP headers. The gap between it and your real publish is your JSON.
Does report by exception save more than minifying?
Usually yes. Minifying is a fixed cut, about 43 percent on this example, while report by exception scales with how quiet the process is: a data message carrying one changed metric by alias can be tens of bytes, and an interval with no changes sends nothing at all. Minify first anyway; the two stack.
Does a jumbo-frame path change the 1460-byte figure?
Yes. The 1460-byte figure assumes a 1500-byte MTU, IPv4 and no TCP options. A 9000-byte jumbo frame raises the segment payload to 9000 − 20 − 20 = 8960 bytes, while a tunnel or VPN lowers the effective MTU below 1500. Discover the path MTU on the actual route instead of assuming it.
What breaks at the subscriber if it misses the birth payload?
Alias resolution. The birth payload is where each metric appears with its full name and is assigned its alias; data messages then carry the alias alone. A host that joined after the birth sees integers it cannot map, so request a rebirth: it re-sends the full mapping and restores decoding.