CoAP
Understanding CoAP
The Internet of Things connects billions of devices, many of which are severely resource-constrained: microcontrollers with 10 to 256 KB RAM, running on coin-cell batteries that must last 5 to 10 years, communicating over radio links with 0.3 to 250 kbps bandwidth and 1 to 10 second latency. Standard web protocols (HTTP over TCP/TLS) were designed for powerful computers on reliable, high-bandwidth networks and are fundamentally unsuited for these environments. CoAP was purpose-designed by the IETF CoRE working group to bring RESTful web architecture to constrained devices while respecting their limitations.
CoAP's design philosophy is "web architecture for the constrained." It maps directly to HTTP semantics (GET reads a resource, PUT updates, POST creates, DELETE removes) but replaces HTTP's verbose text headers with a compact binary format. The 4-byte fixed header contains version, message type, token length, request/response code, and message ID. Variable-length options (analogous to HTTP headers) use delta-encoding for compactness. Payloads follow a single-byte 0xFF marker. This minimalism is critical for LPWAN: on LoRaWAN at SF12 (250 bps), transmitting 500 bytes of HTTP takes 16 seconds and drains 32 mJ from the battery, while 20 bytes of CoAP takes 0.64 seconds and 1.3 mJ, a 25x energy reduction for the same application function.
CoAP Protocol Metrics
Size = 4 (header) + TKL + Options + 1 (marker) + Payload
Energy per Message (LPWAN):
E = Ptx × Tair = Ptx × Size / Bitrate
Observe Savings:
Nsaved = (Tmonitor/Tpoll) - (Nchanges) (messages saved)
Where TKL = token length (0 to 8 bytes), Options = 0 to ~100 bytes typically. On LoRaWAN SF10 (980 bps): 20-byte CoAP takes 163 ms airtime. Ptx = 50 mW typical, E = 8 mJ per message. At 10 messages/day: 29 mJ/day = ~10 years on 1000 mAh coin cell.
IoT Protocol Comparison
| Protocol | Transport | Min Message | Pattern | Best For |
|---|---|---|---|---|
| CoAP | UDP | 10 to 20 bytes | RESTful req/resp | LPWAN, constrained sensors |
| MQTT | TCP | 20 to 50 bytes | Pub/sub (broker) | Wi-Fi/cellular, many subs |
| HTTP/2 | TCP + TLS | 200 to 500 bytes | RESTful req/resp | Cloud APIs, gateways |
| LwM2M | CoAP/UDP | 15 to 30 bytes | Object/resource | Device management, NB-IoT |
| MQTT-SN | UDP | 7 to 20 bytes | Pub/sub | ZigBee, Bluetooth Mesh |
Frequently Asked Questions
Why is CoAP better for LPWAN than HTTP or MQTT?
HTTP text headers exceed 200 bytes; TCP handshake needs 3 round trips at 1 to 10 s latency. MQTT requires TCP (poor on lossy radio links). CoAP uses UDP (no connection overhead), 4-byte header, 10 to 20 byte messages. Observe replaces polling (5 to 50x fewer transmissions). Block-wise transfer fits within LoRaWAN 51 to 242 byte payload limits.
How does CoAP reliability work over UDP?
Confirmable (CON) messages require ACK within 2 to 3 s timeout, with exponential backoff retransmission (up to 4 retries). Non-confirmable (NON) is fire-and-forget for periodic readings. 16-bit message IDs prevent duplicate processing. DTLS adds ~25 bytes encryption overhead vs TLS+TCP's much larger footprint.
How does CoAP compare to MQTT?
CoAP: RESTful, UDP, 10 to 20 bytes, supports multicast and resource discovery (/.well-known/core), adopted by 3GPP for NB-IoT NIDD. MQTT: pub/sub, TCP, 20 to 50 bytes, needs central broker, better for many subscribers. CoAP for constrained devices; MQTT for event distribution on reliable networks.