10  Debate: MQTT vs. CoAP Protocol Selection

A Worked Cost-Model Trade-off Scenario

app-protocols
protocol
debate
mqtt

10.1 MQTT vs CoAP

Discussion Prompt

Scenario: You’re designing a smart agriculture system with 500 soil moisture sensors across a 100-hectare farm. Sensors send readings every 15 minutes. Cellular connectivity is available but expensive.

Debate: Should you use MQTT or CoAP?

Team A (MQTT): Argue for persistent connections, QoS guarantees Team B (CoAP): Argue for lower overhead, UDP efficiency

Consider:

  • Power consumption per message
  • Handling intermittent connectivity
  • Cost per MB of cellular data
  • Message delivery guarantees needed
Putting Numbers to It

This debate gets clearer when you quantify monthly traffic for each protocol strategy.

\[ D_{month}=\frac{N_{msg/day}\times S_{msg}\times 30}{1024^2}\text{ MB} \]

Worked example: 500 sensors sending every 15 minutes:

\[ N_{msg/day}=500\times 96=48{,}000 \]

Assume: - CoAP message size \(S_{msg}=20\) bytes (small UDP payload pattern) - MQTT publish size \(=40\) bytes plus keepalive traffic (30-byte heartbeat every 60 s)

\[ \begin{aligned} D_{CoAP} &= \frac{48{,}000\times 20\times 30}{1024^2} \approx 27.5\text{ MB/month}\\ D_{MQTT,pub} &= \frac{48{,}000\times 40\times 30}{1024^2} \approx 54.9\text{ MB/month}\\ D_{MQTT,keepalive} &= \frac{500\times 1440\times 30\times 30}{1024^2} \approx 618.0\text{ MB/month} \end{aligned} \]

Total MQTT traffic is about \(672.9\) MB/month. At \(0.10\)/MB cellular pricing, that is roughly \(\$67.29\) vs \(\$2.75\) for CoAP-style telemetry, which is a meaningful design trade-off.

10.1.1 MQTT vs CoAP Data Cost

Facilitate MQTT vs CoAP Debate

Setup: Study group of 4 students, each assigned a role.

[0-5 min] Context Setting (Facilitator)

  • Scenario: Smart agriculture with 500 soil sensors
  • Constraint: Cellular data costs $0.10/MB
  • Average message: 20 bytes
  • Frequency: Every 15 minutes
  • Reliability requirement: 95% delivery

[5-15 min] Position Arguments (5 min each)

Team A (MQTT): Maria presents - QoS 1 ensures 95% delivery with ACKs - Persistent connection allows instant alerts - Broker-based fan-out and retained state simplify downstream integrations - Math: Using the same assumptions from the calculator above, MQTT publish traffic is about 54.9 MB/month, and a 60-second keepalive adds roughly 618.0 MB/month more. That is much more expensive on cellular, but Maria argues the extra cost may be worth it when real-time alerts and operational simplicity matter.

Team B (CoAP): Jason presents - Confirmable messages provide reliability - No persistent connection saves battery - Smaller request/response exchanges keep routine telemetry lightweight - Math: 500 sensors × 96 msg/day × 20 bytes × 30 days is about 27.5 MB/month, or $2.75/month at $0.10/MB - BUT: Sleep mode between messages saves 200mAh/day per sensor = 6 months longer battery life

[15-20 min] Cross-Examination (Facilitator moderates)

Q: “Maria, what happens when cellular drops during 15-min sleep?” A: “MQTT broker stores messages. Device resends on reconnect. Data is never lost.”

Q: “Jason, how do you handle real-time alerts with CoAP’s request-response model?” A: “CoAP Observe extension allows push notifications. Gateway polls sensors every 5 min for urgent alerts.”

[20-25 min] Role Reversal

  • Maria argues FOR CoAP battery savings
  • Jason argues FOR MQTT’s simplicity
  • Learning: Students see both sides’ merits

[25-30 min] Facilitator Synthesis “No right answer. Trade-offs:” - MQTT: Better for real-time alerts, higher data cost - CoAP: Better for battery life, more complex implementation - Real projects often use BOTH: CoAP for sensors, MQTT for gateway-to-cloud

Student Feedback: “Switching sides forced me to see why someone would choose the other protocol. Now I understand it’s about project constraints, not ‘better’ vs ‘worse’.”

Time Breakdown Success: 5 min context + 10 min arguments + 5 min questions + 5 min role-switch + 5 min synthesis = exactly 30 min