MQTT vs CoAP at a Glance
| Area | MQTT | CoAP |
|---|---|---|
| Model | Brokered publish/subscribe | Client/server REST-style resources |
| Typical transport | TCP; TLS commonly used | UDP in RFC 7252; DTLS/OSCORE for security |
| Reliability | QoS 0/1/2 | Confirmable vs Non-confirmable messages |
| State | Broker sessions/retained messages | Endpoint resource state |
| Main trust boundary | Broker + topic policy | Endpoint/resource + proxies |
Neither protocol is secure simply because it is lightweight. The architecture determines where identities, authorization and keys must be enforced.
Architecture and Message Flow
MQTT clients connect to a broker, publish to topics and subscribe to topic filters. Producers and consumers are decoupled, which is useful for telemetry and event fan-out, but the broker becomes a high-value trust point.
CoAP addresses resources directly using REST-like methods. Tokens correlate requests and responses; Confirmable messages add lightweight acknowledgement/retransmission. Proxies can bridge CoAP and HTTP, so proxy boundaries must be included in the threat model.
Related Ammune reading: HTTP/3 and QUIC API security and WebSocket API security.
Reliability, QoS and State
MQTT QoS controls delivery semantics: QoS 0 is at-most-once, QoS 1 at-least-once and QoS 2 exactly-once at the MQTT protocol layer. Applications can still see duplicate business effects through reconnects, bridges or downstream retries, so sensitive operations should be idempotent.
CoAP reliability is message-oriented. Confirmable messages are retransmitted until acknowledged or timeout; Non-confirmable messages reduce overhead. An acknowledgement confirms receipt at the protocol layer, not successful business completion.
Security Model Comparison
| Concern | MQTT | CoAP |
|---|---|---|
| Confidentiality | TLS common | DTLS for transport; OSCORE for end-to-end CoAP protection |
| Authentication | Client credentials, certificates or enhanced auth mechanisms | DTLS/OSCORE security context and deployment profile |
| Authorization | Broker topic ACLs | Resource/method authorization |
| Intermediaries | Broker sees/routes messages unless payload protected | OSCORE can protect through proxies |
| DoS | Connections, subscriptions, retained queues | UDP floods, retransmission and constrained-resource exhaustion |
How to Secure MQTT
- Require TLS across untrusted networks and validate broker certificates.
- Use unique device/workload identities rather than fleet-wide shared secrets.
- Apply least-privilege publish/subscribe topic ACLs; avoid broad
#privileges. - Restrict retained messages, persistent-session queues, payload sizes and connection rates.
- Separate broker administration from client identities and protect management APIs.
- Rotate certificates/secrets with a lifecycle suitable for intermittently connected devices.
How to Secure CoAP
DTLS protects a transport hop. RFC 8613 OSCORE protects CoAP request/response information at the application layer and is designed for end-to-end protection across certain intermediaries.
- Use DTLS or another approved secure transport when hop-by-hop protection is sufficient.
- Use OSCORE when intermediaries must route messages without seeing protected application content.
- Authorize resources and methods explicitly; authenticated peers do not automatically get every resource.
- Limit expensive resources, message sizes and request rates.
- Protect provisioning and key lifecycle; protocol encryption cannot compensate for default shared credentials.
Threats That Affect Both
Compromised devices can use valid credentials to send malicious commands or telemetry. Other risks include credential theft, replay, malformed payloads, resource exhaustion and weak provisioning. Validate payload schemas and command semantics, then monitor behavior after authentication.
When to Choose MQTT or CoAP
| Requirement | Likely fit |
|---|---|
| Brokered fan-out / telemetry streams | MQTT |
| Retained messages and topic subscriptions | MQTT |
| REST-like reads/writes on constrained resources | CoAP |
| Very lightweight UDP exchanges | CoAP |
| End-to-end protected CoAP through proxies | CoAP + OSCORE |
| Centralized topic authorization | MQTT |
Hybrid designs are common: a gateway may speak CoAP locally and publish normalized telemetry upstream through MQTT. Secure that translation boundary as both a protocol and trust gateway.
IoT APIs and Gateways
IoT deployments often expose HTTP APIs for management, analytics and partner integrations even when field devices use MQTT or CoAP. Those APIs need their own authorization, sensitive-data controls and runtime monitoring.
Related Ammune reading: Internal API security best practices and Enterprise API monitoring.
Device Onboarding, Key Rotation and Gateway Bridging
IoT protocol security often fails before the first application message is sent: devices ship with shared factory credentials, provisioning channels are weaker than production channels, or certificates cannot be rotated once the device is deployed. Treat onboarding as part of the protocol architecture. Each device or workload should acquire an identity that can be revoked independently, with secure bootstrap material protected against extraction and reuse.
For MQTT, device identity usually terminates at the broker. Broker ACLs should derive publish/subscribe rights from that identity rather than from client-supplied topic metadata. For CoAP, the security context may be established with DTLS or OSCORE; ensure the enrollment mechanism binds the credential to the intended device and authorization profile.
Gateways deserve special attention when they translate between CoAP, MQTT and HTTP. A gateway can decrypt one protocol, transform payloads and re-publish them under another identity. Define whether the downstream service trusts the original device identity, the gateway identity or both, and preserve enough authenticated context for authorization and forensics. Do not copy device identifiers from untrusted payload fields into privileged broker topics or API paths without validation.
| Lifecycle stage | Security question |
|---|---|
| Manufacturing/initial bootstrap | How is the first credential protected and uniquely assigned? |
| Enrollment | How is device identity bound to an authorization policy? |
| Normal operation | Which topics/resources may this device access? |
| Rotation | Can keys/certificates change without physical recall? |
| Compromise | Can one device be revoked without disabling the fleet? |
| Decommission | Are credentials, retained messages and cloud registrations removed? |
Telemetry vs Command-and-Control Security
Telemetry and remote commands have very different consequences even when they use the same protocol. A forged sensor reading may corrupt analytics; a forged actuator command can create physical or safety impact. Separate permissions for publish-only telemetry, command consumption and administrative topics/resources instead of giving a device one broad identity.
For MQTT, use distinct command and telemetry namespaces, narrow wildcard permissions and an explicit retained-message policy. Retained commands can be dangerous if reconnecting devices execute stale state. For CoAP, distinguish safe read resources from state-changing methods and require stronger authorization or freshness/replay controls for high-impact actions.
| Traffic | Primary concern | Useful control |
|---|---|---|
| Sensor telemetry | Spoofing / privacy | Device identity, schema validation, topic/resource ACL |
| Actuator command | Unauthorized state change | Stronger authorization, freshness/replay defense, audit |
| Firmware/config update | Integrity / supply chain | Signed artifacts, version policy, privileged management path |
| Device administration | Fleet-wide blast radius | Separate admin identity and isolated management plane |
MQTT vs CoAP: Deployment Decision Matrix
The protocol decision should follow the workload rather than the popularity of a specific IoT stack. Start with how devices communicate, how constrained they are, which intermediaries are trusted and whether the application needs brokered fan-out or direct resource semantics.
| Requirement | MQTT fit | CoAP fit |
|---|---|---|
| Many consumers need the same telemetry | Strong: publish once, broker fans out | Possible, but not the core brokered model |
| Direct read/write of a constrained device resource | Usually requires topic conventions/application design | Strong: REST-style resource model |
| Intermediaries must route data without seeing protected payload | Requires separate payload protection design | OSCORE can preserve protected CoAP information through supported intermediaries |
| Centralized authorization policy | Natural at the broker via topic ACLs | Usually enforced at resources/gateways |
| Intermittently connected telemetry devices | Strong with sessions, retained state and QoS options | Good for compact request/response but different state model |
A hybrid design is valid when trust boundaries are explicit. The important security question is not “which protocol is safer?” but “where does identity terminate, where is authorization enforced, and which component can see or modify the application message?”
MQTT / CoAP Security Checklist
- Document the communication model and trust boundaries.
- Use unique device identities and credential rotation.
- Encrypt untrusted links.
- Apply least-privilege topic/resource authorization.
- Limit message size, rates and expensive operations.
- Protect broker/gateway management planes separately.
- Validate payload schemas and commands.
- Monitor anomalous device behavior.
- Use OSCORE where CoAP needs end-to-end protection across intermediaries.
Frequently Asked Questions
Is MQTT more secure than CoAP?
Neither is inherently more secure. MQTT commonly relies on TLS and broker ACLs; CoAP can use DTLS and OSCORE. Architecture and key/authorization design determine security.
Does MQTT require TLS?
MQTT does not mandate one security technology, but secure deployments commonly use TLS and strong client authentication.
What is OSCORE?
OSCORE is an application-layer security mechanism for CoAP that can preserve end-to-end protection across certain proxies and translators.
When should I choose MQTT?
Choose MQTT when brokered publish/subscribe, fan-out and retained state are central.
When should I choose CoAP?
Choose CoAP for lightweight REST-like resource access in constrained environments.
Primary References
Protect the APIs Around Your IoT Environment
Use protocol-native security for MQTT and CoAP, then add runtime visibility to the HTTP APIs, gateways and management services around them.
