In September 2019, a GSM bill nearly designed our protocol architecture before I could. The field team had installed 200 next-generation devices in a pilot fleet, and at the end of the month accounting noticed that data usage per M2M line had climbed to three times that of the old devices. 55 MB per device per month instead of 18 MB. Scaled up to thousands of devices, that difference added up to a serious line item every year. The new devices had only one big difference: the old generation spoke our handmade binary TCP protocol, the new ones spoke MQTT.
You'll read "MQTT is an efficient IoT protocol" everywhere. And it's true. But that efficiency depends ruthlessly on how you use it, and on our first attempt we had gotten almost every setting wrong.
The Comfort and the Burden of a Handmade Protocol
Let me describe the old world first. Our first-generation devices opened a plain TCP socket and spoke our own binary framing: a 2-byte magic number, a 2-byte length, a 1-byte message type, the data, and a 2-byte CRC. A position packet came to a grand total of 34 bytes. On the server side, a single epoll-based socket server held those connections. We controlled every byte, and in the stingy world of GPRS that was priceless.
The burden was everything we had built around the protocol. Delivery guarantees? We wrote our own ACK and retransmission logic, and it produced three different bugs across three firmware versions. Sending commands to a device (cut the relay, change the reporting interval)? That took a whole layer that tracked whether the connection was up at that moment and queued the command if it wasn't. Adding a new message type meant the device and server teams working in lockstep for weeks. What MQTT sold us wasn't byte savings; it was getting those layers out of the box: a broker, topics, QoS, retained messages, and disconnect detection through Last Will.

Three Suspects Inflating the Bill
So where were the 55 MB coming from? We dumped the traffic into Wireshark and built a per-device byte ledger. Suspect one: a keep-alive frenzy. Thanks to a setting left over from the integrator's sample code, the devices sent a PINGREQ every 30 seconds. PINGREQ+PINGRESP itself is 4 bytes, but with TCP/IP headers each packet costs 80-90 bytes on the wire; at 2,880 pings a day, that alone is more than 7 MB a month. Suspect two: topic waste. Every position message was published to a 48-character topic like fleet/vehicles/{imei}/telemetry/position/v1, and in an MQTT PUBLISH packet the topic travels as full text every single time. Suspect three: the data was JSON. With the field names included, a position message took 176 bytes; in the old binary framing, the same thing was 34 bytes.
We fixed all three, one by one. We tuned the keep-alive to the operator's NAT timeout, and there's a field reality here: in our measurements, the CGNATs of Turkey's GSM operators drop idle connections after anywhere from 5 to 15 minutes, and that window varies by operator and even by APN. A 240-second keep-alive turned out to be safe on all three operators. We shortened the topics to the form fv/{short-id}/p. And we threw out the JSON; MQTT doesn't care what's inside the payload, so we embedded a simplified version of our old binary framing as the payload. The best of both worlds: MQTT's session management, our byte stinginess.

The Fine Print of QoS 1
For delivery guarantees we chose QoS 1: the device keeps a message and retransmits it until the broker returns a PUBACK. Spotless on paper. In the field it had two surprises. The first is that QoS 1 means "at least once": when a device coming out of a tunnel republished the 40 messages it had never received ACKs for, duplicate records appeared on the server side. Duplicate records look harmless in position data, but not once they get counted twice in the mileage calculation. We added a device-side sequence number to the payload and made the processing layer idempotent; if the same (device, sequence number) pair arrives a second time, it's silently dropped.

We never seriously considered QoS 2 ("exactly once"); its four-way handshake doubles the byte and latency cost of every message, and handshakes left half-finished on dropped GPRS connections are a headache of their own. Resolving duplicates on the server with idempotency is always cheaper than buying a guarantee on the wire. Last Will, by the way, turned out to be an unexpected gift: when connecting, each device leaves the broker a will that says "if I drop, write offline to this topic." In our old system, the code that tracked whether a device was online or offline was a 400-line module watching for socket timeouts; in MQTT it comes down to one retained message and one will.
The second surprise was the clean session flag. In the first setup we had left it set to true; every time a device reconnected, its session on the broker was reset, and any commands sent to it during the disconnect simply evaporated. When a customer asks "did the cut-relay command go through or not," you can't answer "it went through if it happened to catch a connection." Once we set clean session to false and subscribed to the command topics with QoS 1, the broker held commands for the duration of a disconnect and delivered them when the device came back. That single flag retired the entire command queue layer that had taken us three months to write in the old system.
The Decision: Two Protocols, One Principle
We started with Mosquitto as the broker; at 3,000 devices on a single machine it didn't break a sweat (around 6 percent CPU and 400 MB of RAM). What really kept us busy wasn't the broker but what sat behind it: the bridge process that takes messages from MQTT and reliably hands them to the processing pipeline. Its first version took each message and wrote it to the database synchronously; when the database slowed down, the bridge slowed down and the queue on the broker ballooned. We decoupled the bridge by putting a buffer queue in between, but that's a topic for another post.
What about the old devices? Moving the 9,000-odd devices running in the field to MQTT meant a firmware update, and a field firmware update, as experience has proven, carries the risk of bricking 2-3 out of every 100 devices. We didn't move them. The old generation kept living with the raw TCP server, and every new device shipped with MQTT. For two years we ran the two protocols side by side, and we never once regretted that "unclean" decision. Architectural purity isn't worth putting 9,000 working devices at risk.
A confession: during the pilot we didn't use TLS; we leaned on the isolation provided by a private APN. In 2019, a TLS handshake on GPRS modems meant both 4-6 KB of extra traffic and connection times of up to 10 seconds in areas with weak signal, and a device that drops and reconnects pays that cost again every single time. Private APN + username/password + an IMEI whitelist on the broker side was a deliberate trade-off under the conditions of the day. Whether I'd make the same call today is a separate discussion, but I also keep my distance from the grandstand rhetoric of "no TLS means the project is garbage." You write the threat model, you measure the cost, you make the decision.
Back to the end-of-month bill: after the fixes, the MQTT devices' usage dropped from 55 MB to 11 MB. Yes, below the old raw TCP devices too. That's because in the old protocol our ACK/retransmission layer retried blindly over broken connections; MQTT's session awareness eliminated that waste as well. The lesson: choosing a protocol isn't a matter of identity, it's a matter of measurement. If you'd like to run these numbers for your own device fleet and strike the balance between keep-alive and NAT, drop us a line and let's talk; reading Wireshark dumps is still my favorite after-hours activity.