3 CoAP and MQTT: Overhead and Reliability
3.1 Overview
This first route compares the protocols from message flow through transport, reliability, packet overhead, and energy arithmetic.
This is part 1 of 2. Continue with CoAP and MQTT: Brokers and Selection Evidence for the second focused route.
3.2 Start Simple: Pick the Conversation Before the Header
Imagine two teams looking at the same soil sensor. One team wants the sensor to wake once a day and report moisture to a nearby gateway. A gateway is a device that passes data between different networks. The sensor then receives an acknowledgment, which confirms arrival, and returns to sleep. The other team wants a dashboard, rules engine, and maintenance app to see every update. Those are different conversations, even if both carry the same small reading.
For the first conversation, CoAP can feel natural because one endpoint asks or reports a resource and then goes quiet. For the second, MQTT can feel natural because a broker can fan out one publication to several subscribers and remember session state. Read the rest of the comparison as a set of proofs for that story: connection cost, reliability, broker ownership, battery impact, and the evidence a reviewer would keep.
This comparison is long because it answers several different design questions:
- First compare the interaction models: direct CoAP request/response versus MQTT broker publish/subscribe.
- Then inspect transport and reliability: UDP with CON/NON choices versus TCP plus MQTT QoS 0/1/2.
- Next count overhead with the chapter’s 68-byte CoAP and 154-byte MQTT example, then test the same assumptions in the calculator.
- After that use the selection framework and decision evidence table to turn the tradeoff into a reviewable record.
- Finally connect the quick reference, quizzes, and next chapters to the protocol you should study in more depth.
Checkpoint callouts summarize each major decision point. Interactive tools and quizzes are breathers: use them to test the current section before moving on.
3.3 Learning Objectives
By the end of this chapter, you will be able to:
- Compare Architecture Models: Distinguish between CoAP’s request-response and MQTT’s publish-subscribe patterns, explaining the coupling implications of each
- Evaluate Transport Trade-offs: Analyze UDP vs TCP implications for IoT deployments, justifying which is appropriate given device constraints and reliability needs
- Assess QoS Options: Compare reliability mechanisms in both protocols and select the appropriate QoS level for a given use case
- Calculate Protocol Overhead: Compute header sizes, per-message bandwidth, and annual energy expenditure to demonstrate efficiency trade-offs between CoAP and MQTT
- Select Protocols: Apply a decision framework to design protocol selection for real-world IoT deployments based on specific technical requirements
- Diagnose Configuration Errors: Identify and fix common protocol misconfiguration patterns such as persistent MQTT connections for infrequent sensors
Read these points in order. MQTT means Message Queuing Telemetry Transport and uses publish and subscribe. A Broker routes messages from senders to matching listeners. A Topic is a name such as home/bedroom/temperature that selects those listeners. A QoS Level trades extra message work for a chosen delivery behavior. A Retained Message is the last topic message stored for a new listener. A Last Will and Testament is sent by the broker after an unexpected client loss. A Persistent Session stores subscriptions and pending messages so a client can resume later.
- MQTT: Message Queuing Telemetry Transport — pub/sub protocol optimized for constrained IoT devices over unreliable networks
- Broker: Central server routing messages from publishers to all matching subscribers by topic pattern
- Topic: Hierarchical string (e.g., home/bedroom/temperature) used to route messages to interested subscribers
- QoS Level: Quality of Service 0/1/2 trading delivery behavior for message overhead
- Retained Message: Last message on a topic stored by broker for immediate delivery to new subscribers
- Last Will and Testament: Pre-configured message published by broker when a client disconnects ungracefully
- Persistent Session: Broker stores subscriptions and pending messages allowing clients to resume after disconnection
3.4 For Beginners: CoAP vs MQTT
CoAP and MQTT are two popular IoT communication protocols with different strengths. MQTT is like a radio broadcast — a central broker distributes messages to subscribers. CoAP is like a web request — devices directly ask each other for information. Choosing between them depends on your network setup, device resources, and communication patterns.
“I use MQTT to report my temperature readings,” said Temperature Terry. “Why would anyone use CoAP instead?”
the LED jumped in: “Because I don’t need a middleman! With CoAP, I talk directly to the device that wants my data — like sending a text message straight to a friend. With MQTT, I have to go through a broker — like posting on a bulletin board and hoping someone reads it.”
“But the broker is useful!” argued Sammy. “If the dashboard app is offline, the broker holds my messages until it comes back. With CoAP, if nobody answers my message, it’s just… gone.” the microcontroller nodded. “You’re both right. MQTT is better when you have unreliable connections and need the broker to buffer messages. CoAP is better when you want fast, direct request-response — like asking a sensor ‘what’s your temperature right now?’”
the battery settled the debate: “For my power budget, CoAP wins when I only need occasional readings — one request, one response, done. But MQTT wins when I need continuous updates pushed to me without asking every time. Pick the tool that fits the job!”
3.5 Prerequisites
Before diving into this chapter, you should be familiar with:
- Application Protocols Overview: Basic understanding of MQTT and CoAP roles in IoT
- Transport Fundamentals: TCP vs UDP characteristics
Header size alone cannot choose an application protocol. Figure 3.1 compares transport, minimum wire cost, fan-out, and state ownership for one constrained conversation.
The TRANSPORT row in Figure 3.1 pairs CoAP’s confirmable UDP retry with MQTT’s TCP session and keepalive. The FAN-OUT and STATE OWNER rows reverse the apparent two-byte advantage: a broker can distribute once and preserve session or topic QoS, while a direct client may repeat requests.
3.6 CoAP vs MQTT: Detailed Comparison
Choosing between CoAP and MQTT is not always straightforward. Both protocols excel in different scenarios. Here’s a comprehensive comparison:
3.6.1 Architecture and Communication Model
Before choosing between CoAP and MQTT, separate the interaction pattern from the transport details. Use Figure 3.2 to test whether the workload is a direct resource exchange or a broker-mediated flow, then connect that choice to overhead, reliability, and fan-out.
| Aspect | CoAP | MQTT |
|---|---|---|
| Pattern | Request-Response (client-server) | Publish-Subscribe (broker-based) |
| Communication | One-to-One | Many-to-Many |
| Coupling | Tight (direct connection) | Loose (decoupled via broker) |
| Discovery | Built-in resource discovery | Topic-based addressing |
Table: Detailed CoAP vs MQTT Comparison
| FACTOR | CoAP | MQTT |
|---|---|---|
| Main transport protocol | UDP | TCP |
| Typical messaging | Request/response | Publish/subscribe |
| Effectiveness in LLNs | Excellent | Low/fair (Implementations pairing UDP with MQTT are better for LLNs.) |
| Security | DTLS | SSL/TLS |
| Communication model | One-to-one | Many-to-many |
| Strengths | Lightweight and fast, with low overhead, and suitable for constrained networks; uses a RESTful model that is easy to code to; easy to parse and process for constrained devices; support for multicasting; asynchronous and synchronous messages. | TCP and multiple QoS options provide robust communications; simple management and scalability using a broker architecture. |
| Weaknesses | Not as reliable as TCP-based MQTT, so the application must ensure reliability. | Higher overhead for constrained devices and networks; TCP connections can drain low-power devices; no multicasting support. |
Inspect Figure 3.2 now that the detailed table has named each factor; follow the same rows in the visual from transport and messaging pattern through operational strengths and weaknesses.
Read Figure 3.2 from the messaging row outward. CoAP begins with request-response between known endpoints, whereas MQTT inserts a broker so publishers and subscribers need not know one another. Next compare UDP with TCP: CoAP can keep each exchange small and choose confirmation per message, while MQTT pays for a persistent connection and gains TCP delivery plus broker services. Finally inspect the strengths and weaknesses together; neither column is a universal winner. The right choice follows the workload boundary established here.
3.6.2 Network and Transport
| Aspect | CoAP | MQTT |
|---|---|---|
| Transport | UDP (User Datagram Protocol) | TCP (Transmission Control Protocol) |
| Reliability | Optional confirmable messages | TCP ensures delivery |
| Overhead | Very low (4-byte header) | Low (2-byte header + TCP) |
| Connection | Connectionless | Persistent connection |
| Latency | Lower (no handshake) | Slightly higher (TCP handshake) |
3.6.3 Quality of Service and Reliability
| Aspect | CoAP | MQTT |
|---|---|---|
| Message Types | CON (confirmable), NON (non-confirmable) | QoS 0, 1, 2 |
| Acknowledgment | Optional ACK messages | Depends on QoS level |
| Retransmission | Application handles it | QoS 1&2 handle automatically |
| Duplicate Detection | Message IDs | Packet identifiers |
3.6.4 Security
You have now compared the main message pattern and the reliability knobs. The next layer is security, because DTLS or TLS can change the very overhead and wake-up costs that made one protocol look attractive.
| Aspect | CoAP | MQTT |
|---|---|---|
| Security Layer | DTLS (Datagram TLS) | TLS/SSL |
| Authentication | Pre-shared keys, certificates | Username/password, certificates |
| Encryption | Yes (with DTLS) | Yes (with TLS) |
| Overhead | DTLS adds overhead to UDP | TLS integrated with TCP |
3.6.5 Resource Usage
| Aspect | CoAP | MQTT |
|---|---|---|
| Memory Footprint | Minimal | Small |
| Power Consumption | Very low (UDP, sleep modes) | Low (keep-alive messages) |
| Battery Life | Excellent | Very good |
| Bandwidth | Very efficient | Efficient |
3.6.6 Design Philosophy
Read the comparison by separating provenance from operating model. CoAP takes HTTP’s resource-oriented approach into the IETF constrained-node stack, so a client addresses a resource and exchanges a request and response. MQTT comes from SCADA-oriented messaging and standardisation through OASIS, so publishers and subscribers meet through a broker. The target row then explains the practical consequence: constrained direct interactions favour CoAP, while telemetry distribution and messaging favour MQTT. This is a difference in conversation shape, not a claim that one standards body or protocol is universally better.
| Aspect | CoAP | MQTT |
|---|---|---|
| Inspired By | HTTP (RESTful) | MQTT for SCADA |
| Paradigm | Resource-oriented | Message-oriented |
| Standards Body | IETF (RFC 7252) | OASIS (ISO/IEC 20922) |
| Target | Constrained nodes | Telemetry and messaging |
Checkpoint: Transport and Reliability
You now know:
- CoAP is UDP-based and request/response oriented; MQTT is TCP-based and publish/subscribe oriented through a broker.
- CoAP reliability is a per-message choice between CON and NON; MQTT reliability is selected with QoS 0, QoS 1, or QoS 2.
- A daily report on a 2% loss network favors CoAP CON when battery life matters, while many consumers and durable fan-out favor MQTT.
Scenario: A sensor sends a 10-byte temperature reading every 60 seconds for 1 year.
CoAP NON message (UDP):
MQTT QoS 0 message (TCP):
Annual bandwidth:
Battery impact (10 mW TX power, 250 kbps data rate):
3.6.7 Calculation Audit
The estimate uses decimal megabytes and treats the MQTT TCP acknowledgment as radio-on airtime under the same 250 kbps link budget. The raw arithmetic is:
Read these points as one connected sequence: start with One year at a 60-second interval is 365 x 24 x 3600 / 60 = 525,600 messages; then CoAP traffic is 525,600 x 68 = 35,740,800 bytes, or 35.7 MB using 1 MB = 1,000,000 bytes; then MQTT traffic is 525,600 x 154 = 80,942,400 bytes, or 80.9 MB on the same decimal basis; then The byte reduction is 80,942,400 - 35,740,800 = 45,201,600 bytes, so the saving is 45,201,600 / 80,942,400 = 0.558, about 56%; then CoAP airtime is 68 x 8 / 250,000 = 0.002176 s; at 10 mW = 0.010 J/s, that is 0.010 x 0.002176 = 0.00002176 J, or 21.76 uJ; and finish with MQTT airtime is 154 x 8 / 250,000 = 0.004928 s; at the same power, that is 49.28 uJ.
- One year at a 60-second interval is
365 x 24 x 3600 / 60 = 525,600messages. - CoAP traffic is
525,600 x 68 = 35,740,800bytes, or35.7 MBusing1 MB = 1,000,000bytes. - MQTT traffic is
525,600 x 154 = 80,942,400bytes, or80.9 MBon the same decimal basis. - The byte reduction is
80,942,400 - 35,740,800 = 45,201,600bytes, so the saving is45,201,600 / 80,942,400 = 0.558, about56%. - CoAP airtime is
68 x 8 / 250,000 = 0.002176 s; at10 mW = 0.010 J/s, that is0.010 x 0.002176 = 0.00002176 J, or21.76 uJ. - MQTT airtime is
154 x 8 / 250,000 = 0.004928 s; at the same power, that is49.28 uJ.
For a measured device budget, split transmit, receive, idle-listen, retries, and connection setup into separate states. This panel is a protocol-overhead comparison, not a battery-life proof for every radio.
Checkpoint: Overhead Arithmetic
You now know:
- In the worked example, one year at a 60-second interval is 525,600 messages.
- The chapter’s byte count gives 35.7 MB/year for CoAP and 80.9 MB/year for MQTT, a savings of about 56% under the stated assumptions.
- The energy comparison is 21.76 uJ versus 49.28 uJ per message at 10 mW and 250 kbps, but real budgets must still separate transmit, receive, idle-listen, retries, and setup.
3.7 Interactive Protocol Overhead Calculator
3.8 Protocol Selection Framework
Decision context: When selecting an application protocol for IoT communication
| Factor | HTTP | MQTT | CoAP |
|---|---|---|---|
| Battery impact | High (connection overhead) | Medium (persistent TCP) | Low (connectionless UDP) |
| Bandwidth | High (verbose headers) | Low (2-byte header) | Very low (4-byte header) |
| Latency | Medium-High (TCP + headers) | Low (persistent connection) | Lowest (UDP, no handshake) |
| Reliability | TCP ordered byte stream | QoS 0/1/2 options | Optional CON/NON |
| Complexity | Simple (universal) | Moderate (broker required) | Low (direct communication) |
| Pattern | Request-Response | Publish-Subscribe | Request-Response + Observe |
| Ecosystem | Universal | Strong IoT | Growing |
3.8.0.1 Mobile decision snapshot
HTTP Read these points as one connected sequence: start with Best when web compatibility and universal tooling matter most; and finish with Tradeoff: highest bandwidth and battery overhead because of verbose headers and TCP setup.
- Best when web compatibility and universal tooling matter most.
- Tradeoff: highest bandwidth and battery overhead because of verbose headers and TCP setup.
MQTT Read these points as one connected sequence: start with Best for many publishers, multiple subscribers, and event-driven cloud telemetry; and finish with Tradeoff: requires broker infrastructure and a persistent TCP session.
- Best for many publishers, multiple subscribers, and event-driven cloud telemetry.
- Tradeoff: requires broker infrastructure and a persistent TCP session.
CoAP Read these points as one connected sequence: start with Best for constrained nodes that need direct request-response or Observe semantics; and finish with Tradeoff: reliability is optional, so the application must choose CON/NON behavior intentionally.
- Best for constrained nodes that need direct request-response or Observe semantics.
- Tradeoff: reliability is optional, so the application must choose CON/NON behavior intentionally.
Choose HTTP when:
Read these points as one connected sequence: start with Integrating with existing web infrastructure and REST APIs; then Building mobile/web apps that communicate with gateways or cloud; then Debugging and development simplicity is priority; and finish with Bandwidth and battery constraints are not critical (Wi-Fi/Ethernet devices).
- Integrating with existing web infrastructure and REST APIs
- Building mobile/web apps that communicate with gateways or cloud
- Debugging and development simplicity is priority
- Bandwidth and battery constraints are not critical (Wi-Fi/Ethernet devices)
Choose MQTT when:
Read these points as one connected sequence: start with Many devices need to publish to or subscribe from a central system; then Event-driven architecture with multiple consumers per message; then Devices have intermittent connectivity (store-and-forward via broker); and finish with Smart home, telemetry dashboards, industrial monitoring.
- Many devices need to publish to or subscribe from a central system
- Event-driven architecture with multiple consumers per message
- Devices have intermittent connectivity (store-and-forward via broker)
- Smart home, telemetry dashboards, industrial monitoring
Choose CoAP when:
Read these points as one connected sequence: start with Constrained devices with limited RAM/flash (8-bit MCUs); then Battery-powered sensors on LPWAN (6LoWPAN, Thread); then Direct device-to-device or device-to-gateway communication; and finish with RESTful semantics needed on constrained networks.
- Constrained devices with limited RAM/flash (8-bit MCUs)
- Battery-powered sensors on LPWAN (6LoWPAN, Thread)
- Direct device-to-device or device-to-gateway communication
- RESTful semantics needed on constrained networks
Default recommendation: MQTT for cloud/broker-based IoT systems, CoAP for constrained edge networks, HTTP only when interfacing with web systems or during prototyping
3.9 Continue to Part 2
Continue with CoAP and MQTT: Brokers and Selection Evidence.
