Skip to content

The egg I didn't lay

· 10 min read

Why I built a 46-byte encrypted UDP protocol for factory tanks, and threw it away for boring MQTT.

When I started building PocketBulk, a tank-level telemetry system for factories, I gave myself a hard constraint: every unit had to run for 10 years on a single £12 1NCE SIM card.

That plan gives you 500MB of data to last a decade. Send a tank reading every 15 minutes, about 350,000 uploads over ten years, and the whole budget works out to roughly 1.4KB per reading. That has to cover every network header, handshake and failed retry.

On paper, 1.4KB looks generous. Inside a real factory, it evaporates.

Factories are brutal on cellular signals. Steel tanks, reinforced concrete and heavy machinery crush reception, which causes latency spikes and frequent packet loss. On a weak NB-IoT link, standard protocols don't just fail when packets drop. They get expensive. Every interrupted session triggers retransmits and reconnects that burn through megabytes. My rule was simple: smaller packets mean cheaper data and higher reliability. A tiny packet has less chance of being corrupted in poor signal, and costs almost nothing to resend when it is.

So I started stripping the stack down, one layer at a time.

Step 1: HTTPS, then MQTT

The obvious start is HTTPS. Every backend speaks it, every library supports it and debugging it is trivial.

The problem is what it puts on the wire. Every upload carries verbose text headers, repeated metadata, and the weight of a full TCP connection and TLS handshake. When weak signal drops a packet mid-flight, TCP retransmits and TLS renegotiates, burning the 500MB budget on overhead instead of readings.

Moving to MQTT was the easiest win. It strips out repetitive HTTP headers, keeps framing compact and is built for telemetry rather than web pages. For most IoT projects, that step alone cuts data usage dramatically.

Step 2: CoAP and binary

Then I went deeper. Even with MQTT, you still pay for a stateful TCP connection and TLS session. In a factory where reception fades in and out, stateful connections mean constantly paying to tear them down and rebuild them.

CoAP with compact binary payloads pushed the stack into constrained-IoT territory: stateless UDP messaging designed for lossy networks. Still, securing CoAP cleanly meant bringing in DTLS handshakes and extra protocol framing I didn't strictly need.

Step 3: raw UDP and AES-GCM

Why pay for CoAP's framing, topics or negotiation when I controlled both the gateway and the server? PocketBulk didn't need multi-vendor flexibility. It needed to move a tank reading from a sensor into a database, safely.

So I threw out the protocol stack and stripped the upload down to raw UDP. Even device identification cost zero bytes on the wire. Each 1NCE SIM had a fixed private IP on our carrier APN, mapped to its IMEI and tank in our database. The server knew which tank was talking, and which decryption key to use, the moment a packet arrived.

To get confidentiality, tamper detection and replay protection without a TLS handshake, I encrypted the reading with AES-GCM. Instead of sending a full 12-byte nonce on every packet, I sent a 13-bit rolling counter in the clear. The server rebuilt the full 12-byte nonce from the last recorded counter and the expected 15-minute cadence.

One of the 3 remaining bits in that 2-byte header was an "unchanged" heartbeat flag. If the tank level hadn't moved since the last upload, the gateway set the bit and left out the 2-byte reading entirely. That dropped a heartbeat from 48 bytes to 46 bytes. AES-GCM authenticates cleartext headers as additional authenticated data (AAD), so even a heartbeat with no payload still carried a valid 16-byte tag. It proved the counter and flag had not been spoofed.

A reading on the wire (48 bytes)
  • IPv4 header 20 bytes · cleartext

    Source and destination addresses. The source IP maps to the SIM and the tank.

  • UDP header 8 bytes · cleartext

    Ports, length and checksum. This is what makes the packet UDP, so it is not optional.

  • Counter and flags 2 bytes · cleartext

    A 13-bit counter, a 1-bit "unchanged" flag and 2 spare bits. All authenticated as AAD.

  • Tank reading 2 bytes · encrypted

    A 16-bit measurement.

  • AES-GCM tag 16 bytes · integrity check

    Proves neither the reading nor the header flags were tampered with.

Total: 48 bytes

A heartbeat on the wire (46 bytes)
  • IPv4 header 20 bytes · cleartext

    Source and destination addresses. The source IP maps to the SIM and the tank.

  • UDP header 8 bytes · cleartext

    Ports, length and checksum. This is what makes the packet UDP, so it is not optional.

  • Counter and flags 2 bytes · cleartext

    A 13-bit counter, a 1-bit "unchanged" flag and 2 spare bits. All authenticated as AAD.

  • AES-GCM tag 16 bytes · integrity check

    Proves neither the reading nor the header flags were tampered with.

Total: 46 bytes

The transport loop around it was stripped just as bare:

  • IP-first uploads. Even a DNS query costs bytes and round trips on a slow link. The gateway cached the server's IP address and sent UDP packets straight to it.
  • A one-byte reply. Instead of JSON or an HTTP 200 OK, the server returned a minimal UDP packet with a single byte of payload, 29 bytes in total on the wire, to confirm receipt.
  • DNS only on silence. If the gateway got no reply after a retry, it assumed packet loss or an IP change. Only then did it run a DNS lookup to refresh the cached address.
The server's reply
  • IPv4 header 20 bytes · cleartext

  • UDP header 8 bytes · cleartext

  • Acknowledgement 1 byte · cleartext

    One byte that says "got it".

Total: 29 bytes

At 46 to 48 bytes up and 29 back, 350,000 uploads over ten years add up to roughly 26 to 27MB of raw traffic on the wire.

I even considered going one step further with Non-IP Data Delivery (NIDD) and 1NCE's IoT Integrator. That pushes raw, IP-less frames over the NB-IoT control plane and lets the carrier's cloud forward them to my backend by SIM identity. It would have removed the 28 bytes of IP and UDP headers, dropped a heartbeat to 18 bytes, and eliminated DNS and server IP management altogether. Before going down that road, I tested the UDP stack on real hardware.

I wrote the client in Go, cross-compiled a stripped binary onto a Teltonika TRB256 NB-IoT gateway, and watched packets punch through factory dead zones where heavier protocols stalled.

What it did for the server

The smaller win was on the wire. The bigger one was on the server, and it came from what the protocol removed.

There is no TLS handshake and no persistent connection. Nothing sits open per device, so a million devices cost no more connection state than ten. The server is a UDP socket and a loop. A packet arrives, the source IP gives the tank, the tank gives the key, and AES-GCM decrypts and checks it in one step. There is no session to set up or tear down, and no connection table to grow.

The key lookup is the part that could have been slow, and I took it out of the request path. At startup the server uses AWS KMS (or similar) to unwrap its master key, decrypts every device key once, and holds them in memory. After that a packet never waits on a database or an API call. Per device the server holds a key and a counter, which is a few dozen bytes.

Here is the back-of-envelope maths. I haven't benchmarked it at this scale:

  • Packet rate. A million devices, each uploading every 15 minutes, is about 1,100 packets a second. Ten million is about 11,000.
  • Memory. A key plus a counter at a few dozen bytes each is tens of megabytes for a million devices, and a few hundred for ten million.
  • CPU. AES-GCM on modern hardware runs at gigabytes a second. At 48-byte packets, decryption is a rounding error next to the cost of the network stack.
  • Bandwidth. 1,100 packets of 77 bytes, counting the reply, is under 100KB a second.

That fits on a single small Hetzner server with room to spare, which is the point. MQTT at that scale means a broker cluster holding a million open connections. Here, scaling out is just running another copy of the socket loop.

The trade-offs are real. Keys in memory mean a restart has to reload them. A compromised process exposes every key at once. And one process holding the counters is a single point of failure until you design around it. I didn't take it that far.

Then I zoomed out

On the wire, the custom protocol was a triumph. Then I looked past the bit layout at the whole business, and reality hit.

Carriers don't bill by the raw byte. I discovered this after shaving every last bit off the packet. Cellular networks rarely meter usage at single-byte resolution. Each wake-up and data session is rounded up to a minimum billing increment, so the carrier counted far more than 46 bytes every time the modem transmitted. Shrinking a packet from 62 bytes to 46 looked great in Wireshark, while the SIM dashboard barely noticed.

You own the whole stack. Nothing speaks a custom UDP format out of the box. Cross-compiling stripped Go binaries for an embedded MIPS gateway is fun on a prototype. Handing that to another developer, or asking a field technician to debug custom binaries across industrial routers, is pure operational drag.

Key management is a rabbit hole. Stateless AES-GCM needs pre-shared symmetric keys on both ends. Loading keys by hand doesn't scale, so I built an HTTPS bootstrap endpoint keyed to pre-registered TRB256 hardware IDs. It worked, until I hit the edge cases. At 15-minute intervals a 13-bit counter wraps every 85 days, so a tank offline for three months could desync the nonce window. Hardening key rotation, revocation and recovery wasn't worth the engineering time.

The factory maths changed. Plenty of factories already have an IoT Wi-Fi network that makes a 500MB cap irrelevant. Where a site has no Wi-Fi, a single cellular router like a Teltonika RUT200 flips the economics. A standard SIM costs roughly £100 over 10 years, against £12 for a 1NCE SIM. For one tank, that gap is small. Once a site has 10 tanks, a single RUT200 running standard MQTT costs the same £100 and leaves one SIM to manage instead of ten.

Standards protect the customer. A proprietary protocol works nowhere else. If a factory installs hardware across its site and later wants to integrate another system or switch providers, a custom wire format traps them. You could call that lock-in a moat. I would rather win with a better product than by holding data hostage.

Exciting does not beat what already exists

This was my first hard, real-life lesson in something I now treat as a rule: the exciting, cutting-edge option usually loses to the boring one that already exists.

The custom protocol was the fun part. It had clever bit layouts, real byte savings and a great story. MQTT has none of that. What MQTT has is libraries, tooling, documentation, people who already know it, and customers who trust it. All of that is worth far more than the bytes I saved.

The future is lean, just not my egg to lay

I still believe this architecture is where IoT is heading, especially for mobile devices like car and asset trackers. They hop cell towers, roam through dead zones, can't hide behind a site Wi-Fi router, and suffer badly from heavy handshakes.

Raw UDP, AES-GCM and Non-IP Data Delivery aren't my inventions. This is one way of combining them for ultra-constrained links. With enough engineering behind it, and a standard key and device manager built into gateways, a stateless encrypted stack could become a real IoT standard.

For now, PocketBulk needs to ship reliable tank monitors, not pioneer a transport protocol. I'm not the one to lay that egg.