MQTT · Study deck

MQTT Session Management

Picture a delivery box that sleeps between door events.

Broker Bex is your guide for this deck.

sessions
Broker Bex, the module guide, in a scene from this chapter.
iotclass.org

After studying this chapter

Learning objectives

You will be able to:

  • Configure Session Persistence: Select and configure clean versus persistent sessions for specific device types and use cases
  • Implement Secure MQTT: Apply TLS encryption, certificate-based authentication, and topic-level ACLs for production deployments
  • Distinguish Session Behaviors: Compare clean session and persistent session behaviors and justify the choice for a given scenario
  • Design Reconnection Strategies: Construct exponential backoff with jitter to prevent thundering-herd reconnection storms
iotclass.org

Major section

In 60 Seconds

When it reconnects, it needs the right waiting messages, but an old event must not appear as a new opening.

  • Telemetry means readings and status sent by a remote device.
  • Message Queuing Telemetry Transport (MQTT) means a lightweight way for devices to exchange messages.
  • A broker means the service that passes messages from senders to receivers.

Key terms

Quality of service
Quality of service means the delivery promise chosen for a message; it is called QoS.
Latency
Latency means the delay between an action and its result.
iotclass.org

Major section

In 60 Seconds (continued)

Quality of service means the delivery promise chosen for a message; it is called QoS.

  • Latency means the delay between an action and its result.
  • Transport layer security means protection for a network stream; it is called TLS.
  • The box and receiver must agree on one final state.
iotclass.org

Major section

In 60 Seconds (continued)

This runway does not guarantee the physical door result or long-term storage.

  • MQTT session management determines whether the broker remembers a client's subscriptions and queues messages during disconnection.
  • A persistent session (clean_session=false) preserves subscriptions and buffers QoS 1/2 messages for offline clients, while a clean session starts fresh on every connection.
  • Misconfiguring sessions is a top production pitfall, causing either unbounded queue growth or silent message loss.
iotclass.org

Major section

Start With A Sleeping Device · Key Concepts

While it sleeps, the broker must decide whether to forget it, queue messages for it, expire its old session, or reject a duplicate client ID.

  • Session management is the story of what MQTT remembers while clients disappear and return.
iotclass.org

Major section

For Beginners: MQTT Session Management · The Power Nap Problem

"I sleep for 10 minutes at a time to save energy," said the battery. "But when I wake up, have I missed important messages?".

  • Any messages sent while you slept are gone.".
iotclass.org

Major section

Putting Numbers to It: Persistent Session Queue Memory Growth · Interactive Lab: MQTT QoS Comparison

(Units throughout this example use the decimal/SI convention established above: 1 kB = 1,000 bytes, 1 MB = 1,000,000 bytes, 1 GB = 1,000,000,000 bytes.).

  • Broker queue limit (MQTT 5.0 message expiry):: Set message_expiry_interval = 3600 (1 hour) to drop messages older than 1 hour: $$ M_{\text{queue\_max}} = 60 \times 140 = 8.4\text{ KB per sensor (capped)} $$.

Try it: Putting Numbers to It: Persistent Session Queue Memory Growth · Interactive Lab: MQTT QoS Comparison in the chapter

iotclass.org

Major section

Try It: QoS Delivery Simulator · Checkpoint: QoS Tradeoffs

QoS 0: ~80/100 delivered (80%), 0 duplicates, about 1,000 ms, baseline battery use (100%).

  • QoS 1: 100/100 delivered (100%), about 4-6 duplicates, about 1,500 ms, about 150% battery use.
  • QoS 2: 100/100 delivered (100%), 0 duplicates, about 2,500 ms, about 250% battery use.

Numbers to remember

1,000 msabout 1,000 ms, baseline battery use (100%).
100%baseline battery use (100%).
1,500 msabout 1,500 ms, about 150% battery use.
iotclass.org

Major section

Security Considerations · Unencrypted MQTT: A Critical Security Risk

The QoS lab answers "will the message arrive?" The production question is different: "who can read, publish, or replay it while it travels?".

  • Unencrypted MQTT transmits credentials and data in plain text--network sniffers can capture usernames, passwords, and all sensor readings.
iotclass.org

Major section

Common Pitfall: Misunderstanding MQTT QoS Levels · Try It: QoS Level Selection Advisor

The mistake: MQTT QoS levels (0, 1, 2) are often misunderstood.

  • QoS 0 offers no delivery guarantee, QoS 1 guarantees at-least-once delivery (may duplicate), and QoS 2 guarantees exactly-once delivery.
  • Diagnose the problem from cause to corrective action.
iotclass.org

Major section

Pitfall: Expecting Clean Session to Queue Messages for Publishers · Pitfall: Using Random Client IDs with Persistent Sessions

The Mistake: Developers configure clean_session=false on publishing devices (sensors), expecting the broker to buffer their outbound messages when the network is down.

  • Developers assume that if subscribers get queued messages, publishers should too.
  • For ESP32, use the MAC address or chip ID.

Key terms

Default
Default is infinite, which can consume broker memory if clients use random IDs.

Why it matters

The Mistake: Developers enable persistent sessions (clean_session=false) but use auto-generated or random client IDs like ESP32_ + random() or allow the library to generate one.

iotclass.org

Major section

Checkpoint: Persistent Session Hygiene · Pitfall: QoS Mismatch Between Publisher and Subscriber

Finish with Expiry is not optional at fleet scale: MQTT 5 Session Expiry Interval or Mosquitto persistent_client_expiration 7d bounds how long unused state can consume memory.

  • They're confused when messages are duplicated or lost at the subscriber despite using QoS 2 for publishing.

Key terms

If exactly-once delivery
If exactly-once delivery is required, both publisher and subscriber must use QoS 2.

Why it matters

During a network glitch, that mismatch can still produce lost or repeated stop events because the subscriber leg has been downgraded.

iotclass.org

Major section

Pitfall: Session Expiry Flooding on Broker Restart · Try It: Exponential Backoff with Jitter Visualizer

The Mistake: Developers deploy hundreds of IoT devices with persistent sessions (clean_session=false) and long keep-alive intervals (300+ seconds).

  • When the broker restarts or fails over, all devices attempt to reconnect simultaneously, overwhelming the broker with CONNECT packets and queued message delivery.

Why it matters

Ensure broker memory can handle 2-3x this peak load.

iotclass.org

Major section

Deep-Dive Note: Session State Boundaries

A persistent session is a broker-side state budget, not a general reliability switch.

  • The useful state is specific: subscriptions, queued QoS 1 or QoS 2 messages for an offline subscriber, and in-flight QoS acknowledgement exchanges that have not finished.
  • QoS 1 is PUBLISH -> PUBACK, so a lost PUBACK can create a duplicate.

Key terms

QoS 2
QoS 2 is PUBLISH -> PUBREC -> PUBREL -> PUBCOMP, so the broker and client need packet-id state to resume the exchange exactly once after a reconnect.
iotclass.org

Major section

Deep-Dive Note: Session State Boundaries (continued)

MQTT 3.1.1's clean_session=false combines "start fresh" and "keep the session" into one flag.

  • MQTT 5 separates those choices with Clean Start plus Session Expiry Interval, so a client can resume existing state but still give the broker a retention budget.
  • QoS 2 is PUBLISH -> PUBREC -> PUBREL -> PUBCOMP, so the broker and client need packet-id state to resume the exchange exactly once after a reconnect.
  • A clean session discards that in-flight state; a persistent session can preserve it if the broker's expiry and queue limits have not removed the session.
iotclass.org

Deck summary

Key takeaways

When it reconnects, it needs the right waiting messages, but an old event must not appear as a new opening.

  • Quality of service means the delivery promise chosen for a message; it is called QoS.
  • This runway does not guarantee the physical door result or long-term storage.
  • While it sleeps, the broker must decide whether to forget it, queue messages for it, expire its old session, or reject a duplicate client ID.
  • "I sleep for 10 minutes at a time to save energy," said the battery. "But when I wake up, have I missed important messages?".
iotclass.org

Retrieval practice

Recall check 1 of 4

Broker Bex says: answer from memory, then check your reasoning.

Q1Why is using port 1883 for MQTT in production environments a security risk?

APort 1883 is too slow for production use
BPort 1883 requires expensive hardware encryption
CPort 1883 is plaintext.
DPort 1883 is only supported by legacy brokers
Show answer

Answer: C Port 1883 transmits data in plain text without encryption, making it vulnerable to eavesdropping and man-in-the-middle attacks.

iotclass.org

Retrieval practice

Recall check 2 of 4

Broker Bex says: answer from memory, then check your reasoning.

Q2A smart home system uses a public MQTT broker (test.mosquitto.org) for controlling door locks. What are the main security concerns?

AThe broker may add latency, but lock commands stay authenticated
BThe broker charges fees, while topic permissions remain private
CQoS 2 is unavailable, so every message must be sent as QoS 0
DAnyone can read or publish lock traffic.
Show answer

Answer: D see answers page

Q3What security measures should be implemented for a production MQTT system controlling critical infrastructure?

AOnly TLS/SSL encryption on port 8883
BOnly username/password authentication
COnly topic-level access control lists (ACLs)
DUse layered security controls.
Show answer

Answer: D

iotclass.org

Retrieval practice

Recall check 3 of 4

Broker Bex says: answer from memory, then check your reasoning.

Q4A fleet of 500 delivery vehicles uses MQTT to receive route updates from a dispatch server. Vehicles frequently enter tunnels where they lose connectivity for 5–15 minutes. Which session configuration is most appropriate?

AClean session with QoS 0, so tunnel updates can be ignored
BPersistent session, QoS 1 updates, and a stable vehicle client ID
CPersistent session with QoS 0, so the broker keeps no queued updates
DClean session with QoS 2, so delivery while offline is automatic
Show answer

Answer: B Command receivers that experience intermittent connectivity (vehicles in tunnels, sensors in basements, mobile devices) need persistent sessions (clean_session=false) combined with QoS 1 or 2 to ensure messages are queued during disconnection and delivered on reconnection.

iotclass.org

Retrieval practice

Recall check 4 of 4

Broker Bex says: answer from memory, then check your reasoning.

Q5Place each persistent-session event where it lives so you can diagnose whether a reconnect resumes stored subscriptions and queued messages or silently starts a new session.

AClient reconnects (clean_session=False)
BBroker checks stored session
CCONNACK (session_present=1)
DQueued messages delivered
Show answer

Answer: A Place each persistent-session event where it lives so you can diagnose whether a reconnect resumes stored subscriptions and queued messages or silently starts a new session.

Q6Complete the MQTT v5 client with session expiry and message expiry interval:

Aconnect_props.SessionExpiryInterval = 3600 # 1 hour
Bconnect_props.SessionTimeout = 3600 # 1 hour
Cconnect_props.ExpiryInterval = 3600 # 1 hour
Dconnect_props.session_expiry = 3600 # 1 hour
Show answer

Answer: A MQTT v5 uses SessionExpiryInterval (seconds) to control how long the broker retains session state after disconnect.

iotclass.org

Print reference

Answers 1 of 2

Answer key.

  1. C · Port 1883 transmits data in plain text without encryption, making it vulnerable to eavesdropping and man-in-the-middle attacks.
  2. D · Major security risks: 1) Anyone worldwide can subscribe to your topics and see your lock commands, 2) No authentication means anyone can publish commands to unlock your door, 3) Shared infrastructure with unknown users, 4) No data encryption, 5) No topic-level access control.
  3. D
iotclass.org

Print reference

Answers 2 of 2

Answer key.

  1. B · Command receivers that experience intermittent connectivity (vehicles in tunnels, sensors in basements, mobile devices) need persistent sessions (clean_session=false) combined with QoS 1 or 2 to ensure messages are queued during disconnection and delivered on reconnection.
  2. A · Place each persistent-session event where it lives so you can diagnose whether a reconnect resumes stored subscriptions and queued messages or silently starts a new session.
  3. A · MQTT v5 uses SessionExpiryInterval (seconds) to control how long the broker retains session state after disconnect.
iotclass.org