4 CoAP and MQTT: Brokers and Selection Evidence
4.1 Start With the Situation
Two protocols can meet the payload and energy budget but place responsibility in different parts of the system. The team must decide whether a broker should own session and delivery state or whether endpoints should communicate directly.
4.2 Overview
This route compares broker and direct paths, then applies selection tools, reliability ownership, configuration checks, and deployment evidence.
This is part 2 of 2. Review CoAP and MQTT: Overhead and Reliability when you need the first route.
4.3 Learning Objectives
By the end of this chapter, you will be able to:
- compare broker-based and direct communication responsibilities
- select CoAP, MQTT, or HTTP from deployment evidence
- validate reliability and configuration choices with explicit checks
4.4 Chapter Roadmap
Follow the original sections below in order. They begin at the reviewed split boundary and keep every worked example, figure, check, and supporting banner with the section that owns it.
4.5 Decision Evidence Checkpoint
A protocol decision is defensible only when the scorecard names its assumptions. Capture these fields before treating the choice as complete:
↔ Scroll the decision table horizontally to compare the CoAP, MQTT, and HTTP evidence.
| Evidence field | CoAP-favoring signal | MQTT-favoring signal | HTTP-favoring signal |
|---|---|---|---|
| Communication pattern | Direct request/response, resource state, or Observe near the edge | Many publishers or subscribers through a broker | Browser, administration, or integration API |
| Power and session cost | Sleepy node cannot hold a TCP session and sends infrequent readings | Device can maintain a session or gateway owns the session | Mains-powered or gateway/cloud endpoint |
| Reliability need | Per-message choice between CON and NON is enough | Queued offline delivery or QoS levels are needed | TCP request/response semantics are sufficient |
| Operations owner | Gateway can own retry, translation, and discovery | Broker team can monitor topics, queues, retained state, and dead letters | API team can monitor status codes, auth, rate limits, and schemas |
| Retest trigger | Packet loss, battery life, or firmware memory changes | Subscriber count, queue depth, or reconnect behavior changes | Client compatibility, API versioning, or auth boundary changes |
Keep the selection record reviewable: include the weights, the measured packet or energy assumptions, the owner of the gateway or broker boundary, and a trigger that forces the team to rerun the comparison after deployment conditions change.
Use Figure to decide where coordination should live. The point is not merely to count hops, but to expose which component owns discovery, fan-out, retry, and offline delivery. Option A: Use a broker-based architecture where all messages flow through a central MQTT broker Option B: Use direct device-to-device or device-to-server communication with CoAP or HTTP
Decision Factors:
| Factor | Broker-Based (MQTT) | Direct (CoAP/HTTP) |
|---|---|---|
| Coupling | Loose (devices don’t know each other) | Tight (must know endpoint addresses) |
| Discovery | Topic-based (subscribe to patterns) | Requires discovery protocol |
| Fan-out | Built-in (1 publish to N subscribers) | Must implement multi-cast or repeat |
| Single point of failure | Broker is critical | Distributed, no central point |
| Latency | +1 hop through broker | Direct path, minimal latency |
| Offline handling | Broker stores messages | Client must retry |
| Scalability | Scales horizontally with broker cluster | Scales naturally (peer-to-peer) |
| Infrastructure | Requires broker deployment/management | Simpler deployment |
Read Figure down the responsibility columns. The broker-based side buys loose coupling, topic fan-out, and offline buffering by making the broker critical operated infrastructure. The direct side removes that hop and can reduce latency, but each client must know endpoints and own retry, discovery, and repeated delivery. Connect those consequences to the evidence checkpoint above: the protocol choice is complete only when the team has named the owner of whichever coordination boundary it selects.
Broker-based
- Loose coupling and topic-based discovery.
- Built-in fan-out and offline buffering through the broker.
- Tradeoff: the broker becomes critical infrastructure and adds one extra hop.
Direct
- Minimal-latency path between known endpoints.
- No central broker and fewer deployment dependencies.
- Tradeoff: clients must handle retries, discovery, and repeated fan-out themselves.
Inspect Figure 4.1 from the direct path to the brokered path and then the gateway hybrid, tracking which component inherits discovery, buffering, translation, and recovery.
Reading Figure 4.1 left to right, direct CoAP leaves retry and endpoint knowledge with the peers, while brokered MQTT centralizes routing, fan-out, and offline behavior. The hybrid path keeps constrained CoAP exchanges local and makes the gateway validate, translate, buffer, and publish into MQTT. That third route is not a free compromise: the gateway becomes an explicit reliability and operations boundary. The selection bullets below should therefore be read as ownership choices, not protocol popularity.
Choose Broker-Based (MQTT) when:
- Multiple consumers need the same data (dashboards, logging, analytics)
- Devices should not know about each other (decoupled architecture)
- Message persistence is needed for offline devices
- Topic-based routing simplifies message organization
- Enterprise-scale deployments with centralized management
Choose Direct Communication (CoAP/HTTP) when:
- Latency-critical control loops require minimal hops
- Simple point-to-point communication between known endpoints
- Broker infrastructure is impractical (resource-constrained networks, isolated sites)
- RESTful semantics and HTTP-style resources are natural fit
- Avoiding single points of failure is priority
Default recommendation: Broker-based (MQTT) for telemetry collection and event distribution; direct communication (CoAP) for device control and local sensor networks
Checkpoint: Selection Evidence
You now know:
- HTTP is strongest when web compatibility matters; MQTT is strongest when broker fan-out and subscriptions matter; CoAP is strongest for constrained direct request/response.
- A decision record should name the communication pattern, power/session cost, reliability need, operations owner, and retest trigger.
- Broker-based MQTT reduces coupling and adds fan-out, while direct CoAP or HTTP removes broker infrastructure but leaves retry, discovery, and repeated fan-out to the application.
4.6 Interactive Protocol Selection Tool
4.7 Knowledge Check
Use the checks in increasing operational depth. First decide whether CoAP at the edge and MQTT toward cloud consumers are complementary roles rather than redundant fallbacks. Then hold the factory’s latency target and packet-loss condition together when choosing confirmable delivery. The later questions add fleet scale and gateway translation, so justify each answer from interaction pattern, reliability, overhead, and ownership instead of recognising a protocol name.
The first half of the chapter gave you the protocol selection tools. The next compact layer explains why the same choice can change when devices sleep behind NAT, subscribers go offline, or a lossy link makes ordered TCP delivery stall behind one missing segment.
4.8 Overview: Two Interaction Models, Not Just Two Headers
CoAP and MQTT are often compared byte for byte, but their real difference is architectural. MQTT is broker-mediated publish and subscribe over a persistent TCP connection: clients connect to a broker, publish to topics, and subscribe to receive pushes. CoAP is a RESTful request and response over connectionless UDP, with an Observe option that adds a lightweight push. One keeps a standing connection to an intermediary; the other exchanges independent datagrams end to end.
That distinction drives everything else. TCP gives MQTT ordered, reliable byte delivery but requires a connection handshake and keepalive traffic to maintain. UDP lets CoAP send a single request as a single datagram with no connection to set up or hold open, at the cost of handling reliability per message when it needs it. Neither is universally better; they fit different traffic patterns.
For a smart-parking sensor that wakes once every ten minutes, measures occupancy, and reports through a nearby gateway, CoAP can send one confirmable message and let the radio sleep again. The gateway can expose a resource such as `/spaces/17/occupancy`, return a response code, and retry only that observation if no acknowledgment arrives. For a building platform that streams temperature, humidity, and alarm topics to a dashboard, rules engine, and maintenance app, MQTT's broker gives each consumer its own subscription without making the sensor know every destination.
The decision is therefore a boundary decision. CoAP keeps the endpoint relationship direct: the client asks a resource, the server answers, and any fan-out must be built elsewhere. MQTT centralizes fan-out: publishers do not know subscribers, but the broker becomes infrastructure that must be operated, secured, and monitored. A gateway may use both when constrained field devices speak CoAP locally and the site gateway republishes normalized events to MQTT for cloud consumers.
Intuition only: MQTT shines when a device wants a durable pipe to a broker that pushes to many subscribers. CoAP shines when a constrained device wants to send or fetch occasionally without maintaining a connection.
Where They Differ
Transport
MQTT over TCP (connection-oriented, ordered); CoAP over UDP (connectionless datagrams).
Pattern
MQTT broker pub/sub with push; CoAP RESTful request/response plus Observe.
Reliability
MQTT QoS levels backed by the broker; CoAP confirmable messages retransmitted end to end.
Security
MQTT commonly uses TLS; CoAP uses DTLS over UDP; both add a handshake cost.
Overview Knowledge Check
4.9 Practitioner: Count The Connection Overhead For Your Traffic
For a constrained, infrequent reporter, the deciding cost is often not the payload but the connection. MQTT pays a TCP handshake to open a connection and keepalive traffic to hold it; CoAP pays neither, sending each request as an independent datagram.
Worked Example: A Sensor Reporting Every 5 Minutes
The device sends one small reading every 300 s.
- CoAP: each report is one confirmable request datagram plus one acknowledgment datagram - about one round trip, no connection to set up or keep. Between reports the radio can be fully off.
- MQTT, reconnecting each time: every report first pays a TCP three-way handshake, then an MQTT CONNECT/CONNACK, and a TLS handshake if secured - several round trips of setup before the one-message payload.
- MQTT, persistent connection: avoids re-handshaking but must send keepalive pings to hold the connection. With a 60 s keepalive, the device wakes roughly five times between reports just to say "still here," doing no useful work.
For this infrequent, battery-powered pattern CoAP avoids both the per-report handshake and the keepalive tax, so it is usually the lighter choice. If instead the device needed a broker to push commands down to it at any moment, MQTT's standing connection would be the feature, not the cost.
Make the estimate explicit before choosing. A CoAP confirmable exchange on IPv4 might carry a 4-byte CoAP header, 8-byte UDP header, 20-byte IPv4 header, and the link-layer frame. An MQTT report over a cold connection pays TCP SYN/SYN-ACK/ACK, TLS setup if enabled, MQTT CONNECT/CONNACK, PUBLISH, and DISCONNECT unless it holds the socket open. A persistent MQTT client avoids repeated setup but then the keepalive interval becomes part of the battery budget. For a five-minute report interval, a 60-second keepalive can create several radio wakes between useful payloads.
The practical worksheet should record report interval, payload size, expected packet loss, security handshake choice, broker requirement, NAT direction, and whether downlink commands are time-critical. If the device only reports and can tolerate direct request/response, CoAP usually wins. If the device needs asynchronous command delivery, retained state, or several subscribers, MQTT may justify the connection cost.
Overhead Comparison Ledger
Practitioner Knowledge Check
4.10 Under The Hood: Broker Persistence Versus End-To-End Datagrams
The reliability models differ in a way that decides which protocol fits. MQTT's delivery behavior runs through the broker: with a higher QoS and a persistent session, the broker stores messages and delivers them to a subscriber that was offline, and retained messages give a late joiner the last known value. That store-and-forward behavior is exactly what a device behind a network address translator needs, since it initiates the connection outward and the broker pushes down to it. CoAP has no broker in the middle. A confirmable message is retransmitted with backoff until the far endpoint acknowledges, but if that endpoint is offline there is no intermediary holding the data - reliability is end to end, with no built-in offline queue.
Transport choice adds a second effect on lossy links. TCP delivers bytes in order, so a single lost segment blocks everything behind it until it is retransmitted - head-of-line blocking that can stall an MQTT stream on a flaky radio. CoAP's independent UDP datagrams have no such coupling: losing one message does not delay the others, which suits high-loss networks. The honest conclusion is that there is no universal winner. Favor MQTT for push to many subscribers, offline delivery, and NAT traversal; favor CoAP for infrequent constrained request/response, lossy links, and designs with no broker to run. Match the protocol to the traffic pattern rather than to a benchmark of header bytes.
Under the hood, the broker changes failure ownership. With MQTT persistent sessions, the broker must track client id, session expiry, subscriptions, queued QoS 1 or QoS 2 messages, retained values, and Last Will state. That helps a sleeping or disconnected client resume, but it also means broker storage, access control, topic hygiene, and expiry policy are part of the protocol decision. A poorly chosen session expiry can either drop needed commands too early or retain stale commands that should no longer run.
CoAP moves that responsibility to the endpoints. Message IDs, tokens, ACK timeouts, retransmission counters, and Observe sequence numbers tell a client whether a specific exchange completed. There is no central place to queue work for an offline device, so the application must decide whether to retry later, store the command elsewhere, or mark the endpoint unavailable. That makes CoAP simpler for local constrained exchanges and less suitable for cloud-to-device command fan-out when devices sleep behind NAT.
Model Differences That Decide The Choice
Offline delivery
MQTT's broker can hold messages for an offline subscriber; CoAP has no broker queue.
NAT traversal
An MQTT device dials out and the broker pushes down; reaching a CoAP server behind NAT is harder.
Head-of-line blocking
TCP stalls the whole stream on one lost segment; CoAP's UDP datagrams are independent.
No universal winner
Choose by traffic pattern: push-to-many and offline favor MQTT; sparse, lossy, brokerless favor CoAP.
Under-the-Hood Knowledge Check
Use this checkpoint to connect transport mechanics to delivery ownership. The scenario combines an offline device with network address translation, so test the complete path: who retains the command while the device is absent, how the device re-establishes reachability, and which component resumes delivery. Begin at disconnection. An MQTT persistent session can leave the broker holding eligible queued messages for the returning client, subject to the chosen QoS and session-expiry policy. The device later opens an outbound TCP connection, re-establishes its MQTT session, and receives work through that broker-managed path. A CoAP confirmable exchange solves a different problem: it retransmits toward a reachable endpoint and matches the resulting acknowledgement, but it does not create an intermediary that stores a command throughout an arbitrary offline interval. NAT reinforces the distinction because an outbound client connection is normally easier to maintain than unsolicited delivery to a constrained server behind the boundary. A correct choice must therefore explain both persistence and connection direction. Comparing UDP and TCP header size alone cannot resolve the operational requirement, and neither protocol removes the need to reject obsolete or unsafe commands after a long disconnection.
Checkpoint: Reliability Ownership
You now know:
- MQTT reliability is broker-mediated: persistent sessions, retained messages, queued QoS 1 or QoS 2 messages, and Last Will state all become broker operations concerns.
- CoAP reliability is endpoint-mediated: message IDs, tokens, ACK timeouts, retransmission counters, and Observe sequence numbers describe a specific exchange.
- For 200 pressure sensors with 5% packet loss and a <100 ms alert target, the chapter favors CoAP CON over CoAP NON, MQTT QoS 2, or HTTP polling.
4.11 Summary Table: Quick Reference
| Criterion | CoAP | MQTT |
|---|---|---|
| Best Use Case | Direct device queries | Event distribution |
| Communication | Request-Response | Publish-Subscribe |
| Transport | UDP (lightweight) | TCP (reliable) |
| Power | Ultra-low | Low |
| Reliability | Optional | Built-in (QoS) |
| Scalability | Good | Excellent |
| Complexity | Low | Medium |
| Browser Support | Limited | Good (WebSockets) |
| Setup | No broker needed | Requires broker |
| Latency | Low | Medium |
| Data Type | Sensor data | Events/telemetry |
CoAP quick reference Read these points as one connected sequence: start with Best use case: direct device queries and local control; then Transport: UDP with very low overhead and low latency; then Setup: no broker required; reliability is optional; and finish with Best suited to sensor data on constrained devices.
- Best use case: direct device queries and local control.
- Transport: UDP with very low overhead and low latency.
- Setup: no broker required; reliability is optional.
- Best suited to sensor data on constrained devices.
MQTT quick reference Read these points as one connected sequence: start with Best use case: event distribution and telemetry fan-out; then Transport: TCP with built-in QoS and strong browser support via WebSockets; then Setup: requires a broker and slightly more complexity; and finish with Best suited to events and dashboard telemetry.
- Best use case: event distribution and telemetry fan-out.
- Transport: TCP with built-in QoS and strong browser support via WebSockets.
- Setup: requires a broker and slightly more complexity.
- Best suited to events and dashboard telemetry.
4.12 Key Takeaways
Core Concepts:
Read these points as one connected sequence: start with CoAP uses UDP with request-response pattern; MQTT uses TCP with publish-subscribe; then CoAP has 4-byte minimum header; MQTT has 2-byte minimum header (plus TCP overhead); then CoAP offers optional reliability (CON/NON); MQTT provides QoS 0/1/2 levels; and finish with Both protocols target constrained environments but with different design philosophies.
- CoAP uses UDP with request-response pattern; MQTT uses TCP with publish-subscribe
- CoAP has 4-byte minimum header; MQTT has 2-byte minimum header (plus TCP overhead)
- CoAP offers optional reliability (CON/NON); MQTT provides QoS 0/1/2 levels
- Both protocols target constrained environments but with different design philosophies
Practical Applications:
Read these points as one connected sequence: start with Use CoAP for battery sensors, direct device control, and constrained networks; then Use MQTT for telemetry dashboards, event-driven systems, and cloud integration; and finish with Hybrid architectures combine both protocols for optimal efficiency.
- Use CoAP for battery sensors, direct device control, and constrained networks
- Use MQTT for telemetry dashboards, event-driven systems, and cloud integration
- Hybrid architectures combine both protocols for optimal efficiency
Design Considerations:
Read these points as one connected sequence: start with Evaluate latency requirements, power constraints, and communication patterns; then Consider broker infrastructure costs for MQTT vs direct communication for CoAP; and finish with Factor in existing ecosystem and tooling support for your platform.
- Evaluate latency requirements, power constraints, and communication patterns
- Consider broker infrastructure costs for MQTT vs direct communication for CoAP
- Factor in existing ecosystem and tooling support for your platform
4.13 Concept Relationships
Protocol comparison connects to:
Individual Protocol Deep Dives:
Read these points as one connected sequence: start with CoAP Fundamentals - Request-response architecture and resource discovery; then MQTT Fundamentals - Publish-subscribe model and QoS levels; and finish with AMQP vs MQTT - Enterprise messaging comparison.
- CoAP Fundamentals - Request-response architecture and resource discovery
- MQTT Fundamentals - Publish-subscribe model and QoS levels
- AMQP vs MQTT - Enterprise messaging comparison
Architecture Patterns:
Read these points as one connected sequence: start with Hybrid Architecture - CoAP at edge + MQTT for cloud telemetry; and finish with Edge Computing - Protocol placement in edge-cloud architectures.
- Hybrid Architecture - CoAP at edge + MQTT for cloud telemetry
- Edge Computing - Protocol placement in edge-cloud architectures
Use Cases:
Read these points as one connected sequence: start with Smart Home Protocols - MQTT for home automation; and finish with Industrial IoT - CoAP for device control, MQTT for monitoring.
- Smart Home Protocols - MQTT for home automation
- Industrial IoT - CoAP for device control, MQTT for monitoring
4.14 See Also
Implementation Guides:
Read these points as one connected sequence: start with CoAP Implementation Labs - Python and Arduino CoAP examples; and finish with MQTT Labs and Implementation - Hands-on MQTT with ESP32.
- CoAP Implementation Labs - Python and Arduino CoAP examples
- MQTT Labs and Implementation - Hands-on MQTT with ESP32
Decision Frameworks:
Read these points as one connected sequence: start with IoT Protocol Selection - Comprehensive protocol decision tree; and finish with Application Protocols Overview - High-level protocol comparison.
- IoT Protocol Selection - Comprehensive protocol decision tree
- Application Protocols Overview - High-level protocol comparison
Performance Analysis:
Read these points as one connected sequence: start with Protocol Overhead Calculation - Detailed bandwidth and latency analysis; and finish with Battery Impact - Power consumption by protocol.
- Protocol Overhead Calculation - Detailed bandwidth and latency analysis
- Battery Impact - Power consumption by protocol
Tools:
Read these points as one connected sequence: start with Wireshark CoAP Dissector Reference - Packet analysis for CoAP; and finish with MQTTX - MQTT client for testing and debugging.
- Wireshark CoAP Dissector Reference - Packet analysis for CoAP
- MQTTX - MQTT client for testing and debugging
4.15 What’s Next?
| Chapter | Focus | Why Read It |
|---|---|---|
| HTTP and Modern Protocols for IoT | HTTP/2, HTTP/3, WebSockets for IoT | Understand when HTTP is viable on constrained devices and how modern HTTP versions reduce overhead |
| CoAP Fundamentals and Architecture | CoAP request-response model, resource discovery, Observe extension | Deep-dive the protocol you just compared — implement CON/NON messages and /.well-known/core discovery |
| MQTT Protocol Deep Dive | MQTT broker architecture, QoS levels, topic design | Master publish-subscribe internals: retained messages, last will, clean vs. persistent sessions |
| AMQP vs MQTT and Use Cases | Enterprise messaging comparison: AMQP, MQTT, and STOMP | Extend the comparison to enterprise-grade brokers when MQTT’s feature set is insufficient |
| Edge and Fog Computing Architecture | Protocol placement in multi-tier IoT architectures | See how CoAP at the edge and MQTT in the cloud fit together in real deployments |
| IoT Protocol Selection Framework | Comprehensive decision tree across all IoT protocols | Apply a systematic scoring approach to select protocols beyond just CoAP and MQTT |
4.15.0.1 HTTP and Modern Protocols for IoT
Continue with HTTP/2, HTTP/3, and WebSockets for IoT, focusing on the conditions that make modern HTTP viable on constrained devices.
4.15.0.2 CoAP Fundamentals and Architecture
- Focus: request-response model, resource discovery, and Observe.
- Why next: Put CON/NON messaging and
/.well-known/coreinto practice.
4.15.0.3 MQTT Protocol Deep Dive
- Focus: broker internals, QoS levels, and topic design.
- Why next: Understand retained messages, last will, and session persistence.
4.15.0.4 AMQP vs MQTT and Use Cases
Continue with the enterprise messaging tradeoffs across AMQP, MQTT, and STOMP when MQTT’s feature set alone is not enough for the application.
4.15.0.5 Edge and Fog Computing Architecture
Continue into protocol placement across a multi-tier IoT system, tracing how constrained CoAP exchanges at the edge can hand off to MQTT toward the cloud.
4.15.0.6 IoT Protocol Selection Framework
Finish with the wider protocol decision tree and apply its systematic scoring approach when the candidate set extends beyond CoAP and MQTT.
