MQTT · Study deck
MQTT Packet and Broker Features
Picture a building sensor that publishes an alarm just before its receiver disconnects.
Broker Bex is your guide for this deck.

After studying this chapter
Learning objectives
You will be able to:
- Analyze MQTT Packet Structure: Decode binary MQTT packets field by field and calculate exact byte overhead for given topic lengths and QoS levels
- Design Topic Hierarchies: Construct scalable, maintainable topic naming conventions that support wildcard queries for large device fleets
- Calculate Bandwidth Savings: Apply optimization techniques — short topic names, binary payloads, topic aliases — and quantify savings across a device fleet
- Implement MQTT 5.0 Features: Configure message expiry, topic aliases, and shared subscriptions in code and explain when each feature is appropriate
Major section
In 60 Seconds
After reconnection, a new message, an old retained value, and a queued delivery can look alike unless each carries clear identity and time.
- Bandwidth means how much data a path can carry in a given time.
- A broker is the service that routes messages by topic.
- Telemetry means measurements and status sent from a remote device for review.
Major section
In 60 Seconds (continued)
MQTT means Message Queuing Telemetry Transport.
- Quality of service means the selected delivery level.
- QoS is its short name.
- This runway does not prove fleet capacity or exactly-once physical action.
- The deeper sections cover packet fields, topic cost, retained values, sessions, delivery choices, service sizing, and failure tests.
Major section
MQTT's Secret Features
So when a new phone app connects at midnight, it instantly sees '22 degrees' instead of waiting until my next reading.".
- Lila the LED shared her discovery: "And I set up a Last Will message!
- "My favorite," said Bella the Battery, "is clean session = false.
- Last will detects failures.
Major section
Designing Topic Hierarchy for Smart Building
Each floor has 20 rooms with temperature sensors, occupancy detectors, and smart lighting.
- End by: Access patterns: Dashboard shows all temps, HVAC controls one floor.
- The first three levels locate the source, the device-type level separates families such as sensors and lights, and the final level states what is measured or commanded.
Major section
Deep-Dive Note: Packet Evidence and Retained State
Advanced MQTT features are easiest to debug when packet evidence and broker state stores are kept separate.
- The fixed header's first byte identifies the control packet, and for PUBLISH the low nibble carries DUP, the two QoS bits, and RETAIN.
Major section
Deep-Dive Note: Packet Evidence and Retained State (continued)
A quiet client can prove it is alive with 0xC0 0x00 (PINGREQ, zero remaining bytes), while a retained QoS 1 publish might start with 0x33: packet type 3, DUP=0, QoS=01, and RETAIN=1.
- A useful packet note records the raw first byte, decoded flags, Remaining Length bytes, topic length, topic string, packet identifier when QoS is above 0, and payload length.
- Topic-name shortening and MQTT 5 topic aliases save bytes in the variable header, but they do not change QoS, retain, or session behavior.
- That evidence prevents two common misdiagnoses.
Deck summary
Key takeaways
After reconnection, a new message, an old retained value, and a queued delivery can look alike unless each carries clear identity and time.
- MQTT means Message Queuing Telemetry Transport.
- So when a new phone app connects at midnight, it instantly sees '22 degrees' instead of waiting until my next reading.".
- Each floor has 20 rooms with temperature sensors, occupancy detectors, and smart lighting.
- Advanced MQTT features are easiest to debug when packet evidence and broker state stores are kept separate.
Retrieval practice
Recall check 1 of 3

Broker Bex says: answer from memory, then check your reasoning.
Q1You're designing an MQTT topic structure for a fleet of 500 delivery trucks. Each truck has GPS, fuel level, and engine temperature sensors. Which topic hierarchy is most appropriate?
Show answer
Answer: A Correct!
Retrieval practice
Recall check 2 of 3

Broker Bex says: answer from memory, then check your reasoning.
Q2An IoT sensor publishes temperature readings every 10 seconds over a cellular link with a 42-byte topic name. After upgrading to MQTT 5.0, the developer enables topic aliases. How much bandwidth is saved per message after the alias is established, and which MQTT 3.1.1 feature could NOT be replaced by a topic alias?
Show answer
Answer: A Correct!
Retrieval practice
Recall check 3 of 3

Broker Bex says: answer from memory, then check your reasoning.
Q3Place each MQTT role where it lives so you can distinguish client intent, broker-held state, and what subscribers receive after connect or failure.
Show answer
Answer: A Separate publishers, broker state, and consumers so you can predict retained delivery and Last Will behaviour without confusing their owners.
Q4Complete the MQTT publisher with retained messages and Last Will and Testament:
Show answer
Answer: A will_set() pre-registers a message the broker publishes if this client disconnects unexpectedly.
Print reference
Answers
Answer key.
- A · Correct!
- A · Correct!
- A · Separate publishers, broker state, and consumers so you can predict retained delivery and Last Will behaviour without confusing their owners.
- A · will_set() pre-registers a message the broker publishes if this client disconnects unexpectedly.