← All posts
2025-11-08
IOT
8 min

MQTT is not a message queue, and other things I learned the hard way

Retained messages, QoS that does not mean what you think, and a home automation rig that would not stop replaying last Tuesday.

The rig came up, replayed a temperature reading from the previous Tuesday, and tripped a rule that turned the heating on in June. Nothing was broken. I had simply assumed a broker was a queue.

Retained messages are a value, not a backlog

A retained message is the broker's memory of the last value on a topic, handed to anyone who subscribes. It is a cache, not a mailbox, and it does not expire on its own. If a sensor publishes retained and then dies, its last reading is the truth forever as far as every new subscriber is concerned. Publish a zero-length retained payload to clear it, and treat every retained value as suspect until a fresh one arrives.

QoS is about delivery, not ordering, and not deduplication

QoS 2 gets a message delivered exactly once. It says nothing about the order two different topics arrive in, and nothing at all about your handler being idempotent. Design as if everything can arrive twice and out of order, because across a reconnect it will.

The useful mental model

A broker is a live blackboard, not a work queue. If you need a backlog that survives, keep one yourself. Once I stopped asking MQTT to remember things for me, the whole rig got simpler.

Written by Martin Dahl. If you want to argue about any of it, say hello.