How MQTT Works in IoT: QoS, Sessions, and Topics

How MQTT Works in IoT: Simple to Start, Easy to Misconfigure

The publish/subscribe pattern - a publisher sends a message to a topic, a broker routes it, subscribers receive it is genuinely simple, and most MQTT introductions stop there. But that simplicity hides a set of configuration decisions that determine whether a deployment behaves correctly under real-world conditions: intermittent connectivity, devices going offline unexpectedly, and clients that need current state the moment they reconnect.

Every one of MQTT's most useful features exists specifically to handle conditions a clean demo network never produces. Getting how MQTT works in IoT right means understanding what each feature actually guarantees, not just that the feature exists - because the difference between QoS 0 and QoS 1, or between a clean and persistent session, is often the difference between a deployment that recovers gracefully from real network conditions and one that doesn't.

QoS Levels: What Each One Actually Guarantees

MQTT QoS levels explained honestly means being specific about what each level promises and what it costs, because "higher QoS is safer" is true but incomplete — each level trades reliability for overhead, and the wrong choice either wastes bandwidth or loses data silently.

QoS Level

Delivery Guarantee

Mechanism

Overhead

QoS 0

At most once — no guarantee of delivery

Fire and forget, no acknowledgment

Lowest — single packet

QoS 1

At least once — guaranteed delivery, duplicates possible

Publisher waits for a PUBACK; resends if none arrives

Moderate — two packets, possible duplicate messages

QoS 2

Exactly once — guaranteed delivery, no duplicates

Four-part handshake (PUBLISH, PUBREC, PUBREL, PUBCOMP)

Highest — four packets per message

QoS 0 fits telemetry where an occasional lost reading doesn't matter — a temperature sensor publishing every few seconds doesn't need every single reading confirmed, since the next one arrives shortly anyway. QoS 1 fits most command-and-control scenarios, where losing a message matters more than occasionally processing a duplicate — a duplicate "turn on" command is usually harmless, but a lost one isn't. QoS 2 fits situations where a duplicate is genuinely unacceptable, like a billing event or an action that shouldn't fire twice, and it's worth the overhead specifically because that guarantee doesn't exist at QoS 1.

Sessions: What Happens While a Device Is Disconnected

MQTT persistent session behavior is what determines whether a subscriber misses messages published while it was briefly offline, or catches up on everything once it reconnects. When a client connects with a persistent session (clean session flag set to false), the broker remembers that client's subscriptions and queues QoS 1 and QoS 2 messages for it while it's disconnected, delivering them the moment it reconnects.

A clean session, by contrast, discards all of that state - subscriptions and queued messages both the moment the client disconnects, meaning a device that briefly drops off the network with a clean session simply misses anything published during that gap.

Session Type

State on Disconnect

Message Delivery on Reconnect

Best Fit

Persistent

Broker retains subscriptions and queues messages

Queued QoS 1/2 messages delivered on reconnect

Devices on unreliable networks, control systems

Clean

All state discarded on disconnect

None — subscriber starts fresh

Stateless clients like dashboards showing only current values

For IoT devices on unreliable cellular or Wi-Fi connections, persistent sessions are usually the right default; clean sessions make more sense for stateless clients like a dashboard that only cares about current values, not historical gaps.

Retained Messages and Last Will: Solving the "What's the Current State" Problem

Two MQTT features exist specifically to answer a question publish/subscribe doesn't naturally handle: what's the current state of a device right now, especially for a client that just connected and missed every prior message on a topic.

  • Retained messages : when a publisher marks a message as retained, the broker stores it and immediately delivers it to any new subscriber on that topic, even if the message was published before the subscriber connected. This is how a newly opened dashboard immediately shows a device's last known state instead of a blank screen until the next update arrives.

  • Last Will and Testament (LWT) : a client registers a message with the broker at connection time, which the broker automatically publishes if that client disconnects ungracefully (network drop, power loss) without a clean disconnect. This is how other clients learn a device went offline, rather than just seeing silence and guessing.

MQTT retained messages and last will together solve the "state on reconnect" and "detect an unexpected disconnect" problems that a plain publish/subscribe system has no built-in way to handle and both are commonly skipped in a first MQTT implementation, which is why so many IoT dashboards show stale data or never notice a device went dark.

Broker Architecture: The Part Deployments Often Underestimate

MQTT broker architecture in a demo is usually a single broker instance, and that's fine until device count or reliability requirements grow past what one instance can handle. Production deployments commonly use broker clustering (multiple broker nodes sharing load and state) or bridging (connecting separate broker instances, often across sites or cloud regions) to handle scale and avoid a single point of failure.

The broker also enforces authentication and authorization - which clients can connect, and which topics they can publish or subscribe to and skipping this in favor of an open broker is a common security gap in early IoT deployments that only gets addressed after an incident forces the issue.

  • Single broker — simplest to operate, fine for smaller device counts, but a single point of failure

  • Clustered brokers — multiple nodes share load and state, improving both capacity and uptime

  • Bridged brokers — separate broker instances connected together, common when devices and cloud services sit in different networks or regions

Checklist: Configuring MQTT for a Real Deployment

How MQTT works in IoT under real conditions comes down to configuring these deliberately, not leaving them at whatever the client library defaults to:

  • Which QoS level does each type of message actually need is duplicate tolerance acceptable, or does exactly-once matter?

  • Should devices use persistent sessions to avoid losing messages during brief disconnects, or is a clean session sufficient?

  • Are retained messages set on topics where a new subscriber needs current state immediately?

  • Does every client register a Last Will message so other parts of the system notice an unexpected disconnect?

  • Is the topic hierarchy structured so wildcard subscriptions can be used cleanly, without over-broad or overly narrow subscriptions?

  • Does the broker enforce authentication and topic-level authorization, or is it open to any connecting client?

The Features That Actually Determine Reliability

How MQTT works in IoT comes down to more than publish and subscribe — QoS levels, session persistence, retained messages, and Last Will are what determine whether a deployment handles real network conditions gracefully or loses data silently the first time a device drops offline. Getting these settings right for each part of a system, rather than leaving everything at default, is what separates a reliable IoT messaging layer from one that only worked in testing. If you're architecting an MQTT-based IoT deployment and want a second opinion on QoS strategy, session design, or broker architecture, CoreFragment's team can review your requirements and flag the trade-offs early.

Author

Parthraj Gohil

Parthraj Gohil is the Founder and CEO of CoreFragment Technologies. He run the team of IoT developers, embedded engineers, app developers and AI engineers. With more than 10 years of industry experience, he has delivered projects across Healthcare IoT, Industrial IoT, Consumer IoT and AIoT .

Have Something on Your Mind? Contact Us : info@corefragment.com or +91 79 4007 1108

Share this blog

Share this on social channels to benefit others.