Retained messages in IOT Protocols - Time & Space Complexity
When devices use retained messages, the broker stores the last message on a topic. We want to understand how the time to handle these messages changes as more topics have retained messages.
How does the broker's work grow when sending retained messages to new subscribers?
Analyze the time complexity of the following code snippet.
// When a new client subscribes:
for each topic in subscribed_topics {
if (topic has retained_message) {
send retained_message to client;
}
}
This code sends all retained messages for the topics a client subscribes to, right after subscription.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Looping over all subscribed topics to check for retained messages.
- How many times: Once per topic the client subscribes to.
As the number of subscribed topics grows, the broker must check and send more retained messages.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 checks and possible sends |
| 100 | 100 checks and possible sends |
| 1000 | 1000 checks and possible sends |
Pattern observation: The work grows directly with the number of subscribed topics.
Time Complexity: O(n)
This means the time to send retained messages grows linearly with the number of topics subscribed.
[X] Wrong: "Sending retained messages happens instantly no matter how many topics there are."
[OK] Correct: Each topic with a retained message requires a separate send operation, so more topics mean more work and time.
Understanding how retained messages affect broker performance shows you can think about real-world system scaling. This skill helps you design efficient IoT solutions.
"What if the broker cached retained messages and sent them in batches? How would the time complexity change?"
Practice
Solution
Step 1: Understand retained message concept
Retained messages keep the last message sent on a topic stored on the broker.Step 2: Identify the purpose for new subscribers
New subscribers receive the last retained message immediately upon subscribing, ensuring they have the latest state.Final Answer:
To store the last message on a topic for new subscribers -> Option BQuick Check:
Retained message = last stored message [OK]
- Confusing retained messages with message encryption
- Thinking retained messages delete old messages
- Assuming retained messages limit message size
Solution
Step 1: Identify publish options related to message retention
The retain flag controls whether the broker stores the message as retained.Step 2: Confirm correct flag for retention
Setting retain=true tells the broker to keep the message for future subscribers.Final Answer:
retain -> Option AQuick Check:
retain flag true = retained message [OK]
- Confusing retain with cleanSession flag
- Using qos instead of retain for message storage
- Mistaking willFlag for retention control
home/temperature?
client.publish('home/temperature', '22°C', retain=True)
client.publish('home/temperature', '23°C', retain=False)Solution
Step 1: Analyze retained flag on each publish
The first message '22°C' is published with retain=True, so it is stored on the broker.Step 2: Understand effect of second publish with retain=False
The second message '23°C' is published without retain, so it is sent but not stored as retained. Importantly, publishing with retain=False on the same topic clears the retained message.Step 3: Determine what new subscribers receive
New subscribers get no retained message because the last retained message was cleared by the second publish.Final Answer:
No message because retain is false on last publish -> Option CQuick Check:
Retain=False on last publish clears retained message [OK]
- Assuming last published message is always retained
- Thinking retain=False messages overwrite retained messages
- Expecting multiple retained messages per topic
device/status. Which publish command will correctly clear it?Solution
Step 1: Understand how to clear retained messages
Publishing an empty payload with retain=True clears the retained message on that topic.Step 2: Analyze the options
Publishing with retain=False does not update or clear the retained message. Using None as payload is invalid. Only publishing an empty string ('') with retain=True clears the retained message.Final Answer:
client.publish('device/status', '', retain=True) -> Option AQuick Check:
Empty payload + retain=True clears retained message [OK]
- Using retain=False when trying to clear retained message
- Sending None as payload which causes error
- Expecting retain=False to remove retained message
devices/+/status with retained messages. A new device subscribes to devices/device123/status. What will it receive and why?Solution
Step 1: Understand MQTT wildcard subscription
The subscription to devices/device123/status matches only that specific topic, not all devices.Step 2: Understand retained message delivery
The broker sends the last retained message on that exact topic immediately upon subscription.Step 3: Determine what the new device receives
The new device receives the last retained status message for device123 immediately, not messages from other devices.Final Answer:
It receives the last retained status message for device123 immediately -> Option DQuick Check:
Retained message sent on exact subscribed topic [OK]
- Thinking wildcard subscription delivers all retained messages
- Expecting no retained messages on subscription
- Confusing retained with non-retained message delivery
Ready to prove it?
Pass the test and this pattern moves unknown
