10  MQTT Protocol Fundamentals

In 60 Seconds

MQTT (Message Queuing Telemetry Transport) is a lightweight publish-subscribe messaging protocol designed for constrained devices and unreliable networks. It decouples senders from receivers through a central broker, supports three QoS levels for tunable reliability, and has become the de facto standard for IoT communication powering systems from Amazon Alexa to industrial automation.

10.1 MQTT Protocol Overview

Time: ~60 min (full guide) | Intermediate | P09.C23

MQTT runs over TCP and uses a broker-centric architecture where publishers send messages to named topics without knowing who receives them. Subscribers register interest in topics and receive matching messages automatically. Originally designed in 1999 by Andy Stanford-Clark (IBM) and Arlen Nipper for satellite-linked oil pipeline monitoring, MQTT has become the dominant IoT messaging protocol with an estimated 7 billion+ connected devices using it worldwide.

10.2 Learning Objectives

By the end of this module, you will be able to:

  • Explain MQTT fundamentals: Describe the publish-subscribe pattern and justify when to select MQTT over alternative protocols
  • Design topic hierarchies: Construct scalable, maintainable topic structures applying wildcards correctly
  • Evaluate QoS trade-offs: Compare the three reliability levels, calculate overhead costs, and select the appropriate guarantee for a given use case
  • Configure secure deployments: Implement TLS encryption, authentication mechanisms, and access control lists for production MQTT brokers
  • Develop working applications: Create and demonstrate ESP32 and Python MQTT clients through hands-on implementation labs

MQTT is a lightweight messaging protocol that lets IoT devices communicate efficiently, even over slow or unreliable networks. Think of it as a group chat for your devices: sensors post updates to named topics, and any device that has joined that topic receives the message automatically through a central message broker.

The Sensor Squad Explains MQTT!

Imagine your school has a magical bulletin board that works like a super-smart postal service!

Temperature Terry wants to tell everyone how warm the classroom is. Instead of running to each person individually (that would take forever!), Terry walks to the bulletin board and pins a note under “Classroom Temperature.”

Now here’s the magic: - The Bulletin Board (called the Broker) remembers who wants what information - Your Phone App signed up for temperature updates - it gets Terry’s note automatically! - The Air Conditioner signed up too - it turns on when it sees “too hot!” - The School Computer logs everything for the principal

Terry only posted ONE note, but everyone who cared got the message! That’s MQTT - a super-efficient way for IoT devices to share information!

Fun Fact: MQTT was invented to help oil pipelines in the desert send tiny messages over slow satellite connections. Now Amazon Alexa uses it to talk to over 100 million smart home devices!

10.3 Quick Start

New to MQTT? Start with MQTT Introduction for fundamentals, terminology, and prerequisites.

10.4 MQTT Architecture Overview

MQTT broker architecture

The broker is the heart of MQTT - it receives all messages from publishers and routes them to interested subscribers. Publishers and subscribers never communicate directly, enabling loose coupling and easy scaling.

10.5 MQTT Chapter Guide

This comprehensive MQTT guide is organized into seven focused chapters:

Chapter Focus Estimated Time
MQTT Introduction Fundamentals, terminology, when to use MQTT 10 min
MQTT Architecture Broker architecture, pub-sub pattern, components 15 min
MQTT Topics & Wildcards Topic design, wildcards, naming best practices 10 min
MQTT QoS Levels QoS 0/1/2, battery considerations, delivery guarantees 15 min
MQTT Security TLS encryption, authentication, ACLs 15 min
MQTT Labs ESP32 labs, Python examples, interactive simulators 30 min
MQTT Advanced Topics Clustering, HA, performance, troubleshooting 20 min

10.6 Key Concepts at a Glance

Key features:

  • Publish-Subscribe: Decouples message producers from consumers
  • Quality of Service: Three levels (0, 1, 2) for reliability trade-offs
  • Retained Messages: Last value stored for new subscribers
  • Last Will: Automatic notification on unexpected disconnect
  • Topics: Hierarchical message routing with wildcards

Retained messages let new subscribers instantly get the latest value without waiting. But each retained message consumes broker memory:

Memory per retained message: \[ M_{\text{retained}} = M_{\text{topic}} + M_{\text{payload}} + M_{\text{metadata}} \]

Where:

  • \(M_{\text{topic}}\): Topic string storage (~30-60 bytes typical)
  • \(M_{\text{payload}}\): Message content (variable, 10-1000 bytes typical)
  • \(M_{\text{metadata}}\): QoS, timestamp, client ID (~30 bytes)

Typical retained message: ~120 bytes total

Scaling example (smart city sensors): 100,000 sensors each retaining latest reading: \[ M_{\text{total}} = 100{,}000 \times 120\text{ bytes} = 12\text{ MB} \]

Best practices:

  • Use retained messages for state (e.g., device/123/status = online)
  • Don’t retain telemetry (e.g., temperature readings every 10 sec)
  • Set expiry for retained messages in MQTT 5.0 to auto-cleanup

Real-world limit: Mosquitto default max retained: 100,000 messages (~12 MB RAM) EMQX enterprise: millions (configurable, can use disk backing)

When to use retained:

  • Device online/offline status: YES (subscribers need current state)
  • Configuration values: YES (new subscribers need current config)
  • Frequent sensor readings: NO (use normal publish, next value comes soon)

10.7 MQTT vs Other Protocols

Feature MQTT CoAP HTTP
Pattern Publish-Subscribe Request-Response Request-Response
Transport TCP UDP TCP
Min Overhead 2 bytes 4 bytes ~200 bytes
Best For Frequent updates, many subscribers Direct queries, RESTful APIs Web integration

10.8 Learning Path

Beginner Path (45 min):

  1. Introduction - Start here
  2. Architecture - Understand the broker
  3. Topics & Wildcards - Design your topics

Intermediate Path (add 30 min):

  1. QoS Levels - Master reliability trade-offs
  2. Security - Secure your deployment

Advanced Path (add 50 min):

  1. Labs - Hands-on implementation
  2. Advanced Topics - Production deployment

10.9 Video Overview

MQTT Protocol Tutorial
MQTT Protocol Overview
Overview of MQTT publish/subscribe model, QoS, retained messages, and LWT.

10.10 Quick Reference

10.10.1 MQTT Ports

Port Purpose
1883 Unencrypted MQTT (development only)
8883 MQTT over TLS (production)
443 MQTT over WebSocket with TLS

10.10.2 QoS Levels

Level Name Guarantee Use Case
0 At most once Fire and forget Frequent sensor readings
1 At least once Acknowledged Important alerts
2 Exactly once Guaranteed once Critical commands

10.10.3 Topic Wildcards

Wildcard Matches Example
+ Single level home/+/temp matches home/bedroom/temp
# Multi-level home/# matches home/bedroom/sensor/temp

10.11 Interactive Tools

Practice MQTT concepts hands-on with these simulators:

Try It Yourself
  • MQTT Topic Designer: Design and validate topic hierarchies with 12 best-practice lint rules
  • MQTT QoS Simulator: Visualize message delivery with QoS 0, 1, and 2
  • MQTT Session Manager: Explore persistent sessions and clean session behavior

10.12 MQTT Calculators

Use these interactive calculators to explore MQTT sizing and performance trade-offs for your IoT deployments.

10.12.1 MQTT Broker Connection Calculator

Estimate the number of broker connections needed for your deployment, comparing MQTT’s hub-and-spoke model against direct peer-to-peer connections.

10.12.2 MQTT Retained Message Memory Calculator

Calculate broker memory consumed by retained messages across your device fleet.

10.12.3 MQTT QoS Overhead Estimator

Compare the bandwidth overhead for each QoS level given your message rate and payload size.

10.12.4 MQTT Topic Depth Analyzer

Evaluate topic hierarchy depth and estimate matching overhead for wildcard subscriptions.

10.12.5 MQTT Battery Life Estimator

Estimate battery consumption impact from MQTT message publishing on constrained devices.

Quiz: Match Concepts and Sequence the Protocol

Test your conceptual recall before attempting the multiple-choice checks below.

10.13 Knowledge Check

Test your understanding of MQTT fundamentals:

Common Mistake: Retained Messages Without Lifecycle Management

The Mistake: A smart home system publishes device status (devices/livingroom-light/status) as retained messages with retain=true. Devices publish “online” when they connect and “offline” when they disconnect gracefully. However, when devices are permanently removed (sold, broken, replaced), their retained “online” messages stay on the broker forever.

Why This Happens: Retained messages persist until explicitly cleared with an empty payload. Most developers focus on publishing status but forget that decommissioning requires cleanup.

Real-World Impact:

  • HomeAssistant dashboard shows 12 “online” devices, but only 8 exist. User can’t distinguish real devices from ghosts
  • Automation triggers fire for devices that don’t exist, causing confusing behavior
  • Monitoring system alerts on “device offline” for devices that were removed months ago

The Fix: Implement device lifecycle management:

# When removing a device, clear ALL its retained messages
def decommission_device(client, device_id):
    topics_to_clear = [
        f"devices/{device_id}/status",
        f"devices/{device_id}/config",
        f"devices/{device_id}/state"
    ]

    for topic in topics_to_clear:
        # Empty payload with retain=true clears retained message
        client.publish(topic, payload="", qos=1, retain=True)
        print(f"Cleared retained message: {topic}")

MQTT 5.0 Solution: Use Message Expiry Interval to auto-expire retained status:

from paho.mqtt.properties import Properties
from paho.mqtt.packettypes import PacketTypes

props = Properties(PacketTypes.PUBLISH)
props.MessageExpiryInterval = 3600  # Expire after 1 hour

client.publish(
    "devices/sensor01/status",
    payload="online",
    qos=1,
    retain=True,
    properties=props
)
# If device stops publishing heartbeats, status auto-expires

Best Practices:

  1. Heartbeat pattern: Devices publish status every N minutes. If retained status expires, device is considered dead
  2. Admin cleanup script: Periodically scan for stale retained messages (last update >30 days)
  3. Topic TTL: Use MQTT 5.0 Message Expiry for automatic cleanup
  4. Monitoring: Alert when retained message count grows unexpectedly

Prevention: Always implement decommission_device() function in your device management code from day one, not as an afterthought.

Common Pitfalls

Unencrypted MQTT exposes device credentials and sensor data to network eavesdroppers — in a building IoT deployment on shared WiFi, this means any connected device can read all sensor data. Always enable TLS 1.2+ on the broker and generate unique client certificates for each device class.

Without LWT, there is no automatic notification when a device disconnects ungracefully — missed timeout alarms and false-healthy device status are common consequences. Configure LWT on every device connection to publish an offline status message, enabling real-time fleet health monitoring.

A single MQTT connection serializes all publishes through one TCP socket — at 100 messages/second with QoS 1, TCP backpressure creates queuing latency. Use multiple parallel MQTT connections or partition topics across connection pools for throughput above 1,000 messages/second.

10.14 Summary

MQTT provides a lightweight, efficient messaging solution for IoT through its publish-subscribe architecture:

  • Central broker routes messages between publishers and subscribers without direct connections
  • Topic hierarchies with wildcards (+ single level, # multi-level) enable flexible message routing
  • Three QoS levels balance reliability against bandwidth and battery consumption
  • Built-in features like retained messages, Last Will, and persistent sessions handle real-world IoT challenges

When to choose MQTT:

  • Frequent sensor data updates (temperature, humidity, motion)
  • Battery-powered devices needing minimal overhead
  • Many-to-many communication patterns (one sensor, multiple consumers)
  • Unreliable networks requiring graceful disconnection handling

When to consider alternatives:

  • Direct request-response patterns: Use CoAP or HTTP
  • Real-time streaming with ultra-low latency: Use WebSockets
  • Peer-to-peer without broker infrastructure: Use CoAP multicast

10.15 Concept Relationships

MQTT Protocol connects to:

Seven-chapter learning path: Introduction (basics) -> Architecture (broker) -> Topics (naming) -> QoS (reliability) -> Security (production) -> Labs (hands-on) -> Advanced (deployment).

10.16 See Also

10.17 What’s Next

Chapter Focus Why Read It
MQTT Introduction Terminology, history, and prerequisites Start here to build a solid conceptual foundation before exploring architecture details
MQTT Architecture Broker internals, connection lifecycle, persistent sessions Understand how the broker routes messages and manages client state at scale
MQTT QoS Levels QoS 0/1/2 semantics, bandwidth trade-offs, battery impact Apply the correct reliability guarantee to each use case and calculate real overhead costs
MQTT Security TLS encryption, username/password auth, ACLs, certificate pinning Transition from development (port 1883) to a production-hardened deployment
CoAP Fundamentals Request-response model, UDP transport, RESTful IoT Evaluate when CoAP is a better fit than MQTT for direct device queries and constrained meshes
Application Protocols Overview MQTT vs CoAP vs HTTP vs AMQP side-by-side Select the right protocol for any IoT scenario using a structured comparison framework