HTTP vs MQTT trade-offs in IOT Protocols - Performance Comparison
When comparing HTTP and MQTT protocols, it is important to understand how their communication patterns affect performance as the number of devices grows.
We want to see how the work each protocol does changes when more devices send or receive messages.
Analyze the time complexity of message delivery in HTTP and MQTT.
// HTTP example: each device sends a request to server
for device in devices:
send_http_request(device)
wait_for_response()
// MQTT example: devices subscribe to topics
mqtt_broker = start_broker()
for device in devices:
device.subscribe(topic)
publish(topic, message)
This code shows HTTP sending individual requests per device, while MQTT uses a broker to publish messages to many devices subscribed to a topic.
Look at what repeats as devices increase.
- Primary operation: Sending messages to each device.
- How many times: HTTP sends one request per device; MQTT sends one publish to broker regardless of device count.
As the number of devices grows, HTTP sends more individual requests, while MQTT sends one message that the broker distributes.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | HTTP: 10 sends, MQTT: 1 publish |
| 100 | HTTP: 100 sends, MQTT: 1 publish |
| 1000 | HTTP: 1000 sends, MQTT: 1 publish |
Pattern observation: HTTP work grows linearly with devices; MQTT work stays mostly the same for publishing.
Time Complexity: O(n) for HTTP message sending, O(1) for MQTT message publishing
This means HTTP's work grows directly with the number of devices, while MQTT can send one message to many devices efficiently.
[X] Wrong: "MQTT always uses less time because it sends fewer messages."
[OK] Correct: MQTT's broker handles distributing messages, which can add overhead as devices increase, so the total work is not always constant.
Understanding how protocols scale helps you explain design choices clearly and shows you can think about system performance in real projects.
What if MQTT broker had to send individual messages to each device instead of one publish? How would the time complexity change?
Practice
Solution
Step 1: Understand HTTP communication style
HTTP works on a request-response model where the client asks and the server replies, making it simple and direct.Step 2: Compare with MQTT style
MQTT uses publish-subscribe, which is more complex and suited for many devices and continuous data flow.Final Answer:
HTTP -> Option CQuick Check:
Request-response = HTTP [OK]
- Confusing MQTT as request-response
- Thinking HTTP supports many-to-many easily
- Mixing up protocol purposes
Solution
Step 1: Identify MQTT communication model
MQTT uses a publish-subscribe model where devices publish messages to topics and others subscribe to those topics.Step 2: Eliminate other options
Request-response is HTTP, peer-to-peer and broadcast are different models not used by MQTT.Final Answer:
Publish-subscribe -> Option AQuick Check:
MQTT = Publish-subscribe [OK]
- Choosing request-response for MQTT
- Confusing peer-to-peer with publish-subscribe
- Assuming MQTT uses broadcast
Solution
Step 1: Analyze network usage for many devices
MQTT uses small message headers and keeps connections open, reducing overhead for frequent messages.Step 2: Compare with HTTP overhead
HTTP opens a new connection per request with larger headers, increasing bandwidth use.Final Answer:
MQTT because it uses lightweight messages and persistent connections -> Option DQuick Check:
Low bandwidth = MQTT lightweight messages [OK]
- Assuming HTTP is lighter due to caching
- Thinking MQTT has large headers
- Ignoring connection overhead
Solution
Step 1: Understand MQTT trigger mechanism
MQTT devices receive triggers by subscribing to topics; without subscription, they can't wait for triggers.Step 2: Identify why device sends continuously
If not subscribed, device may publish data on its own schedule instead of waiting for server commands.Final Answer:
Not configuring the device to subscribe to the server's trigger topic -> Option BQuick Check:
MQTT triggers need subscriptions [OK]
- Confusing publish-subscribe with request-response
- Thinking QoS affects trigger behavior
- Switching protocols unnecessarily
Solution
Step 1: Consider network constraints
Unreliable network and limited bandwidth require efficient, reliable message delivery without repeated data loss.Step 2: Evaluate protocol features
MQTT supports persistent sessions to resume after disconnects and QoS 1 or 2 to ensure message delivery.Step 3: Compare options
HTTP polling wastes bandwidth and lacks delivery guarantees; MQTT QoS 0 and no persistence risk data loss.Final Answer:
MQTT with persistent sessions and QoS 1 or 2 -> Option AQuick Check:
Reliable delivery + low bandwidth = MQTT QoS 1/2 + persistence [OK]
- Choosing HTTP for unreliable networks
- Using MQTT QoS 0 when reliability needed
- Ignoring session persistence
Ready to prove it?
Pass the test and this pattern moves unknown
