Understanding MQTT Retain, Will, and QoS in IoT Gateways: What Matters Most?

As the Internet of Things (IoT) ecosystem scales, ensuring reliable, lightweight, and efficient communication between devices becomes paramount. MQTT (Message Queuing Telemetry Transport) is a publish-subscribe protocol that’s tailor-made for IoT, known for its simplicity, low bandwidth consumption, and ability to function in unstable network environments.

In the context of IoT gateways, which aggregate and transmit data between edge devices and the cloud, MQTT's features—Retain, Will, and QoS—become crucial. Each of these controls how messages are delivered, stored, and handled in failure scenarios. Misconfiguring them can lead to system inefficiencies, message loss, or even critical failures in industrial environments.

What is MQTT and Why It’s Ideal for IoT Gateways

MQTT is designed to work over TCP/IP and supports:

  • Lightweight messaging (small code footprint and minimal header size)

  • Persistent sessions

  • Reliable communication in unstable networks

  • Publish-subscribe mechanism for scalability

IoT gateways are intermediaries that:

  • Collect data from sensors or edge devices using various protocols (Zigbee, Modbus, BLE, etc.)

  • Translate and transmit this data to cloud platforms using MQTT

  • Ensure devices stay connected, monitored, and synchronized

Why MQTT for IoT Gateways?

  • Supports thousands of devices concurrently

  • Works with intermittent connectivity

  • Enables real-time status tracking via retained messages and Will messages

  • Reduces communication overhead

MQTT Retain Flag: Keeping Messages Alive

The retain flag ensures the last known message on a topic is stored on the broker and immediately sent to any new subscriber to that topic.

Use Cases of Retained Messages

  1. Sensor State Preservation

  • When a temperature sensor sends a reading (temp/room1 = 25°C) with retain=true, a new subscriber (like a dashboard) instantly receives this value upon subscribing, even if the sensor isn’t currently publishing.

Gateway and Device Availability Status

  • Gateways can publish device/xyz/status = online with retain=true when a device connects. Subscribers monitoring device health will instantly know the current state without waiting for new data.

Initial Configuration Broadcast

  • Configuration topics like config/device1/settings can be retained so any new device joining the network can retrieve the latest parameters immediately.

Pitfalls to Avoid

  1. Stale Data Risk

  • If a retained message is never updated or removed, new subscribers might receive obsolete information. This can lead to incorrect decisions or device behavior.

Data Overwriting Errors

  • Devices inadvertently overwriting retained topics without understanding their significance can disrupt system logic.

Proper Deletion

  • To remove a retained message, publish an empty payload with the retain flag set to true. Failing to do so may leave unwanted messages in the broker.

MQTT “Last Will and Testament” (LWT): Fail-Safe for Unexpected Disconnects

The Last Will and Testament is a message the broker sends on behalf of a client (e.g., IoT gateway or device) if the client disconnects unexpectedly.

How Will Messages Work

  • When a client connects to the broker, it specifies:

    • The topic where the Will message will be published

    • The message content (e.g., “offline”)

    • The QoS level

    • Whether the message is retained

  • If the client disconnects ungracefully (e.g., power cut, network failure), the broker sends the Will message.

Real-World Scenarios for LWT

  1. Device Health Monitoring

  • Devices or gateways publish a retained “online” status upon connection and register an LWT message with “offline” status. Dashboards or services subscribed to these topics can immediately detect device failures.

Critical Infrastructure

  • In oil rigs, factories, or power plants, failure detection is crucial. A gateway managing 50 sensors registering LWTs ensures operational teams are alerted when something goes wrong.

Failover Logic

  • Cloud services can trigger backup processes, reassign loads, or issue alerts when a gateway goes offline based on Will messages.

MQTT QoS (Quality of Service) Levels: Ensuring Message Delivery

MQTT offers three QoS levels to balance between delivery guarantees and network overhead.

QoS 0 – At Most Once

  • Message sent once, no acknowledgment

  • Fastest, least reliable

  • If the message is lost, it is not resent

Use Cases:

  • Environmental telemetry like temperature, humidity

  • High-frequency non-critical data

QoS 1 – At Least Once

  • Requires acknowledgment from the receiver

  • May be delivered multiple times

  • Publisher resends until it gets an acknowledgment

Use Cases:

  • Alerts or alarms (e.g., fire warning)

  • Device commands where duplication is tolerable (e.g., LED ON)

QoS 2 – Exactly Once

  • Ensures one-time delivery

  • Uses a four-step handshake

  • Higher latency and bandwidth usage

Use Cases:

  • Financial transactions

  • Mission-critical control signals (e.g., shutting down a turbine)

Trade-Offs Between Reliability and Overhead

QoS Level

Reliability

Latency

Bandwidth

Use Case

QoS 0

Low

Low

Low

Streaming telemetry

QoS 1

Medium

Medium

Medium

Alerts, control commands

QoS 2

High

High

High

Mission-critical actions

What Matters Most for IoT Gateways

Retain vs. Non-Retain

  • Retain should be used strategically:

    • For device states like "online/offline"

    • For last sensor readings needed immediately by UIs or cloud apps

  • Don’t retain high-volume or rapidly-changing data

Will Messages for Device Health Monitoring

  • Gateways should register a Will for:

    • Their own availability

    • Any device they manage (if device doesn’t connect directly)

  • Helps in building redundancy, auto-recovery, and real-time monitoring

QoS Optimization in Constrained Environments

  • In low-bandwidth areas (e.g., rural IoT), use:

    • QoS 0 for frequent, loss-tolerant data

    • QoS 1 for moderate reliability

  • Avoid QoS 2 unless absolutely necessary due to its high overhead

Best Practices for MQTT Gateway Configuration

  • Use Retain for device status and configuration data

  • Register LWT for all gateways and edge devices

  • Match QoS to use-case; don’t blindly use QoS 2

  • Avoid stale retained messages — clean them up as needed

  • Use consistent topic structures (e.g., gateway/device123/status)

  • Monitor broker logs and metrics for message delivery stats and LWT usage

  • Secure your MQTT communication with TLS and authentication to avoid spoofing

Conclusion

MQTT’s Retain, Will, and QoS features are simple yet incredibly powerful when properly understood and configured. They offer IoT gateways the tools needed to:

  • Provide instant state updates

  • Maintain visibility in case of failure

  • Balance delivery guarantees with resource constraints

By leveraging these features wisely, developers and architects can create IoT systems that are scalable, responsive, and robust, even in the most demanding environments.

FAQs

1. Can I use Retain, Will, and QoS together?

Yes. They serve different purposes and complement each other when applied thoughtfully.

2. How can I remove a retained message?

Publish an empty payload to the topic with the retain flag set to true.

3. Is it possible to use different QoS levels for the same topic?

Yes. The publisher and subscriber can each set their own QoS preferences. The effective QoS is the minimum of the two.

4. Will messages be retained?

Will messages can be retained if explicitly configured, but by default they are not.

Enjoyed this article? Stay informed by joining our newsletter!

Comments

You must be logged in to post a comment.

About Author