2  MQTT Architecture

mqtt

2.1 Start With One Sensor Message

Picture a temperature sensor with one reading to share and three dashboards that may or may not be online. MQTT solves that problem by putting a broker in the middle: the device publishes once, subscribers express what they care about, and the broker owns the routing and state evidence. Start with that path before choosing topics, sessions, retained messages, or last-will behavior.

2.2 The Broker Separates Senders From Receivers

MQTT is a publish-subscribe protocol built around a broker. A publisher sends a message to a topic. A subscriber registers a topic filter. The broker compares the published topic with the filters and forwards matching messages.

The key architectural idea is decoupling. Publishers do not need subscriber addresses, and subscribers do not need publisher addresses. Both sides keep one application relationship with the broker instead of a growing set of direct relationships with each other.

Broker Bex, the messaging guide

Broker Bex

“A message with no subscriber is a tree falling in an empty forest — design the topic before the payload.”

In this chapter, Bex checks the architecture record for three things: who may publish, what the broker is asked to remember, and what still needs its own proof beyond delivery.

graph LR
    P1["Publisher: sensor"] -->|publish topic| B["Broker"]
    P2["Publisher: gateway"] -->|publish topic| B
    B -->|match filter| S1["Subscriber: dashboard"]
    B -->|match filter| S2["Subscriber: logger"]

If you only need the intuition, this layer is enough: MQTT architecture is a hub for application messages. The broker owns routing, session state, retained messages, and delivery negotiation; devices own the meaning of the payloads they publish and consume.

MQTT publish-subscribe framework showing publishers, subscribers, broker, and topic-based message routing.
MQTT publish-subscribe architecture: clients connect to the broker, and the broker fans messages out by topic match.

The Three Pieces To Name First

Client Role

A client can publish, subscribe, or do both. The role is per flow, not per device: a gateway may publish sensor events and subscribe to command topics.

Topic Contract

The topic tree is the routing contract. It should express stable ownership, location, asset, event type, or command boundary.

Broker State

The broker may hold subscriptions, session state, retained messages, queued messages for persistent sessions, and last-will configuration.

Beginner Examples

  • A temperature node publishes to building/a/floor/2/room/204/temperature; dashboards subscribe to the parts of the tree they need.
  • A maintenance service subscribes to fault topics without changing device firmware.
  • A gateway can bridge non-MQTT sensors into MQTT by publishing normalized events under agreed topics.

Overview Knowledge Check

If this gives you the broker model, you can stop here. Continue to Practitioner when you need to design and review a brokered MQTT deployment.

2.3 Build The MQTT Architecture Record

A reviewable MQTT design does not start with sample code. It starts with a short architecture record that says which clients connect, what they publish, what they subscribe to, what the broker stores, and what evidence proves the system is operating correctly.

MQTT broker architecture with clients, topic tree, subscription matching, session state, and retained message handling.
The broker is both a router and a state boundary: it matches topics, tracks sessions, applies policy, and coordinates delivery behavior.

Walkthrough: From Message Flow To Broker Decision

  1. Name the flow. Separate telemetry, command, alert, configuration, and state-feedback messages.
  2. Name the clients. Record publishers, subscribers, gateways, brokers, bridges, dashboards, and backend services.
  3. Name the topic tree. Define the stable topic levels and the wildcard filters subscribers are allowed to use.
  4. Name the session policy. Decide clean start, persistent subscription, queued delivery, retained message, and last-will behavior per flow.
  5. Name the trust boundary. Record client identity, ACLs, TLS policy, certificate or credential handling, and bridge boundaries.
  6. Name the evidence. Keep broker logs, connection counts, subscription records, publish acknowledgements, dead-letter or error handling, and application-level state feedback for commands.

Architecture Record Template

Decision
Question
Good Evidence
Review Risk
Broker placement
Where do clients connect?
Network diagram, port policy, broker ownership, backup path.
Hidden broker hops make latency, identity, and failure review unclear.
Topic taxonomy
Which topic levels carry tenant, site, device, metric, and command scope?
Topic examples, wildcard examples, ACL examples, and naming rules.
Ambiguous topics force subscribers to parse payloads or over-subscribe.
Session behavior
What happens when clients disconnect and reconnect?
Clean-session policy, persistent subscriptions, expiry rules, and replay expectations.
Offline clients can miss critical messages or receive stale command bursts.
Command confirmation
How does the system prove a device acted on a command?
Application-level state feedback, command correlation ID, timeout, and retry policy.
MQTT QoS can confirm delivery to MQTT peers, not that the physical action happened.

Worked Example: Building Telemetry And Command Channels

Telemetry

Devices publish readings under stable measurement topics. Dashboards and analytics subscribe with filters that match their scope.

Commands

Operators publish command requests to device-specific command topics. Devices publish state feedback to separate state topics.

Review Trigger

Reopen the architecture decision when topic filters broaden, client counts grow, broker ownership changes, or command confirmation becomes safety relevant.

Bex’s Topic Board

  • Topic: the taxonomy names tenant, site, device, metric, and command scope — topic, wildcard, and ACL examples make it reviewable.
  • QoS: the record already warns that QoS can confirm delivery to MQTT peers, not that the physical action happened.
  • Subscriber: dashboards and analytics subscribe with filters that match their scope, while operators publish commands to device-specific topics.

Practitioner Knowledge Check

2.4 Routing, Packets, Sessions, And State

At the protocol level, MQTT clients exchange control packets with the broker. The common architecture path is CONNECT, CONNACK, SUBSCRIBE, SUBACK, PUBLISH, acknowledgement packets for higher QoS levels, and DISCONNECT. The details matter because the broker's state machine is what turns topic strings into reliable operating behavior.

MQTT control methods including CONNECT, PUBLISH, SUBSCRIBE, UNSUBSCRIBE, acknowledgements, and DISCONNECT.
MQTT control methods define connection setup, subscription management, publishing, acknowledgements, and disconnect behavior.

Topic Matching Is Deterministic

The broker compares each published topic name with stored subscription filters. The single-level wildcard + matches exactly one topic level. The multi-level wildcard # matches the remaining levels and must appear at the end of the filter. Published topic names are concrete names; wildcard characters belong in subscription filters.

MQTT topic wildcard diagram showing a home topic tree, single-level plus wildcard examples, multi-level hash wildcard examples, and wildcard placement rules.
Wildcard topic filters are the broker's routing index. A careful hierarchy reduces client-side filtering and accidental over-subscription.

State Boundaries To Review

Session State

The broker may remember subscriptions and queued messages for a client across reconnects, depending on the session policy and broker configuration.

Retained Message

A retained message gives new subscribers the latest retained value for a topic. It is useful for state snapshots and risky for one-time commands.

Last Will

A last-will message lets the broker publish a preconfigured status if a client disconnects unexpectedly. Treat it as a signal to investigate, not a complete fault diagnosis.

Bex’s Topic Board

  • Topic: a single-level + matches exactly one level; a multi-level # takes the rest and must end the filter.
  • QoS: it describes the client-to-broker and broker-to-subscriber exchange — not that a machine moved or a database committed.
  • Subscriber: a retained message gives new subscribers the latest value for a topic — useful for a snapshot, risky for a one-time command.

Implementation Sketch

publish topic:     factory/line-a/pump-17/temperature
subscriber filter: factory/+/+/temperature
match result:      yes, because + matches line-a and pump-17

publish topic:     factory/line-a/pump-17/command/start
subscriber filter: factory/+/+/temperature
match result:      no, because the final level differs

Reliability boundary: MQTT QoS describes message exchange between an MQTT client and broker, and between broker and subscriber. It does not prove that a machine moved, a valve opened, or a database committed the business action. Critical actions need application-level confirmation.

Under-The-Hood Knowledge Check

2.5 Summary

MQTT architecture is broker centered. Clients publish concrete topic names, subscribers register topic filters, and the broker routes matching messages while managing session-related state. The strongest designs treat topics, sessions, retained messages, last will, identity, and command confirmation as architecture decisions rather than code details.

2.6 Key Takeaway

Design MQTT around broker responsibilities and review evidence: who connects, which topics route messages, what state the broker stores, and how the application proves important actions actually happened.

2.7 See Also

MQTT Publish-Subscribe

Use this next to practice the client-side publishing and subscribing pattern that sits on top of the broker architecture.

MQTT QoS

Connect architecture decisions to QoS tradeoffs, acknowledgements, duplicates, and delivery boundaries.