Chapters

3 CoAP and MQTT: Overhead and Reliability

app-protocols
coap
mqtt
comparison

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.

In 60 Seconds

An application protocol is a shared set of rules for exchanging messages. The Constrained Application Protocol (CoAP) lets one device ask another device for data or an action. It sends each exchange with the User Datagram Protocol (UDP). UDP does not keep a connection open. CoAP also helps a client find the resources that a device offers.

Telemetry means measurements or status reports sent by a device. Message Queuing Telemetry Transport (MQTT) lets senders publish those reports for many listeners. It uses the Transmission Control Protocol (TCP). TCP keeps an ordered connection open. A broker is the service that receives each MQTT message and sends it to the right listeners.

CoAP has a 4-byte base header. MQTT can have a 2-byte base header, but its open TCP connection adds other costs. That connection can spread its setup cost across many messages. Choose CoAP for occasional, direct device requests. Choose MQTT when continuous telemetry must reach several listeners.

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.

Chapter Roadmap

This comparison is long because it answers several different design questions:

  1. First compare the interaction models: direct CoAP request/response versus MQTT broker publish/subscribe.
  2. Then inspect transport and reliability: UDP with CON/NON choices versus TCP plus MQTT QoS 0/1/2.
  3. Next count overhead with the chapter’s 68-byte CoAP and 154-byte MQTT example, then test the same assumptions in the calculator.
  4. After that use the selection framework and decision evidence table to turn the tradeoff into a reviewable record.
  5. 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:


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.

Two-column protocol ledger: CoAP direct request-response over UDP with a four-byte base header versus MQTT brokered publish-subscribe over TCP with a two-byte fixed header.
Figure 3.1: CoAP and MQTT comparison across transport, minimum header, fan-out, and state ownership.

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.

AspectCoAPMQTT
PatternRequest-Response (client-server)Publish-Subscribe (broker-based)
CommunicationOne-to-OneMany-to-Many
CouplingTight (direct connection)Loose (decoupled via broker)
DiscoveryBuilt-in resource discoveryTopic-based addressing

Table: Detailed CoAP vs MQTT Comparison

FACTORCoAPMQTT
Main transport protocolUDPTCP
Typical messagingRequest/responsePublish/subscribe
Effectiveness in LLNsExcellentLow/fair (Implementations pairing UDP with MQTT are better for LLNs.)
SecurityDTLSSSL/TLS
Communication modelOne-to-oneMany-to-many
StrengthsLightweight 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.
WeaknessesNot 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.

Comparison table showing CoAP versus MQTT across dimensions including transport protocol (UDP vs TCP), messaging pattern (request-response vs publish-subscribe), effectiveness in Low-power Lossy Networks, security mechanisms (DTLS vs SSL/TLS), communication models (one-to-one vs many-to-many), and relative strengths and weaknesses for constrained IoT applications
Figure 3.2: Visual comparison of CoAP and MQTT protocol characteristics

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.

Format Discovery: CoAP Negotiates, MQTT Assumes

The “Discovery” row above is about finding a resource. A separate question is whether the two endpoints agree on what format that resource is returned in, and the two protocols answer it differently.

CoAP carries Content-Format and Accept options in its header, so a client can ask which representations a server can return, and the server can label the format it sends back. That built-in negotiation lets constrained devices probe each other for a compatible payload format before committing to one.

MQTT has no equivalent metadata field. A publisher and its subscribers must agree on the message format out of band — through documentation, a schema registry, or plain convention — before anyone uses the topic. MQTT will move whatever bytes a client hands it, but the protocol itself will not tell a receiver what those bytes mean.

Record the format-agreement mechanism alongside the topic or resource map: CoAP’s content negotiation is protocol-level evidence, while MQTT’s format agreement is a design decision that has to be documented and enforced separately.

3.6.2 Network and Transport

AspectCoAPMQTT
TransportUDP (User Datagram Protocol)TCP (Transmission Control Protocol)
ReliabilityOptional confirmable messagesTCP ensures delivery
OverheadVery low (4-byte header)Low (2-byte header + TCP)
ConnectionConnectionlessPersistent connection
LatencyLower (no handshake)Slightly higher (TCP handshake)

3.6.3 Quality of Service and Reliability

AspectCoAPMQTT
Message TypesCON (confirmable), NON (non-confirmable)QoS 0, 1, 2
AcknowledgmentOptional ACK messagesDepends on QoS level
RetransmissionApplication handles itQoS 1&2 handle automatically
Duplicate DetectionMessage IDsPacket identifiers
Knowledge Check: QoS and Reliability

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.

AspectCoAPMQTT
Security LayerDTLS (Datagram TLS)TLS/SSL
AuthenticationPre-shared keys, certificatesUsername/password, certificates
EncryptionYes (with DTLS)Yes (with TLS)
OverheadDTLS adds overhead to UDPTLS integrated with TCP

3.6.5 Resource Usage

AspectCoAPMQTT
Memory FootprintMinimalSmall
Power ConsumptionVery low (UDP, sleep modes)Low (keep-alive messages)
Battery LifeExcellentVery good
BandwidthVery efficientEfficient

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.

AspectCoAPMQTT
Inspired ByHTTP (RESTful)MQTT for SCADA
ParadigmResource-orientedMessage-oriented
Standards BodyIETF (RFC 7252)OASIS (ISO/IEC 20922)
TargetConstrained nodesTelemetry and messaging
Broker BexCheckpoint: 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):

CoAP header=4 bytesToken + options=6 bytesPayload=10 bytesUDP header=8 bytesIPv6 header=40 bytesTotal=68 bytes per message\begin{align} \text{CoAP header} &= 4 \text{ bytes} \\ \text{Token + options} &= 6 \text{ bytes} \\ \text{Payload} &= 10 \text{ bytes} \\ \text{UDP header} &= 8 \text{ bytes} \\ \text{IPv6 header} &= 40 \text{ bytes} \\ \text{Total} &= 68 \text{ bytes per message} \end{align}

MQTT QoS 0 message (TCP):

MQTT fixed header=2 bytesTopic length + topic=2+20=22 bytesPayload=10 bytesTCP header=20 bytesIPv6 header=40 bytesTCP ACK (return)=60 bytesTotal=154 bytes per message\begin{align} \text{MQTT fixed header} &= 2 \text{ bytes} \\ \text{Topic length + topic} &= 2 + 20 = 22 \text{ bytes} \\ \text{Payload} &= 10 \text{ bytes} \\ \text{TCP header} &= 20 \text{ bytes} \\ \text{IPv6 header} &= 40 \text{ bytes} \\ \text{TCP ACK (return)} &= 60 \text{ bytes} \\ \text{Total} &= 154 \text{ bytes per message} \end{align}

Annual bandwidth:

Messages/year=365×24×360060=525,600CoAP annual=525,600×68=35.7 MBMQTT annual=525,600×154=80.9 MBSavings=80.935.780.9×100%=56%\begin{align} \text{Messages/year} &= \frac{365 \times 24 \times 3600}{60} = 525{,}600 \\ \text{CoAP annual} &= 525{,}600 \times 68 = 35.7 \text{ MB} \\ \text{MQTT annual} &= 525{,}600 \times 154 = 80.9 \text{ MB} \\ \text{Savings} &= \frac{80.9 - 35.7}{80.9} \times 100\% = 56\% \end{align}

Battery impact (10 mW TX power, 250 kbps data rate):

TX time (CoAP)=68×8250,000=2.2 msTX time (MQTT)=154×8250,000=4.9 msEnergy per message:CoAP 22μJ vs MQTT 49μJ (2.2×)\begin{align} \text{TX time (CoAP)} &= \frac{68 \times 8}{250{,}000} = 2.2 \text{ ms} \\ \text{TX time (MQTT)} &= \frac{154 \times 8}{250{,}000} = 4.9 \text{ ms} \\ \text{Energy per message} &: \text{CoAP } 22 \mu\text{J vs MQTT } 49 \mu\text{J (2.2×)} \end{align}

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,600 messages.
  • CoAP traffic is 525,600 x 68 = 35,740,800 bytes, or 35.7 MB using 1 MB = 1,000,000 bytes.
  • MQTT traffic is 525,600 x 154 = 80,942,400 bytes, or 80.9 MB on the same decimal basis.
  • 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%.
  • 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.
  • MQTT airtime is 154 x 8 / 250,000 = 0.004928 s; at the same power, that is 49.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.

The mathematical gist. At one message per minute, the chapter’s 21.76 µJ CoAP and 49.28 µJ MQTT events cost 11.4 J and 25.9 J per year. Against an illustrative derated 1900.8 J coin-cell budget, those are radio-only bounds of 166 and 73.4 years—not deployment predictions, because sleep, sensing, retries, self-discharge, and cutoff are still absent.

Math Bridge · guided foundationsWhy do 11.4 J and 25.9 J not predict battery life?Let Bex turn per-message energy into a bounded annual ledger without hiding the missing drains.

Broker BexCheckpoint: 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

Tradeoff: HTTP vs MQTT vs CoAP

Decision context: When selecting an application protocol for IoT communication

FactorHTTPMQTTCoAP
Battery impactHigh (connection overhead)Medium (persistent TCP)Low (connectionless UDP)
BandwidthHigh (verbose headers)Low (2-byte header)Very low (4-byte header)
LatencyMedium-High (TCP + headers)Low (persistent connection)Lowest (UDP, no handshake)
ReliabilityTCP ordered byte streamQoS 0/1/2 optionsOptional CON/NON
ComplexitySimple (universal)Moderate (broker required)Low (direct communication)
PatternRequest-ResponsePublish-SubscribeRequest-Response + Observe
EcosystemUniversalStrong IoTGrowing

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.