Topic design patterns for IoT in IOT Protocols - Time & Space Complexity
When designing topics for IoT devices, it is important to understand how the number of topics affects system performance.
We want to know how the time to process messages grows as the number of topics increases.
Analyze the time complexity of the following topic subscription handling code.
for topic in subscribed_topics:
if incoming_message.topic == topic:
process_message(incoming_message)
break
This code checks each subscribed topic to find a match for the incoming message topic, then processes it.
- Primary operation: Looping through the list of subscribed topics.
- How many times: Up to once per subscribed topic until a match is found.
As the number of subscribed topics grows, the time to find a matching topic grows roughly in direct proportion.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | Up to 10 topic checks |
| 100 | Up to 100 topic checks |
| 1000 | Up to 1000 topic checks |
Pattern observation: The number of checks grows linearly with the number of topics.
Time Complexity: O(n)
This means the time to find a matching topic grows directly with the number of subscribed topics.
[X] Wrong: "Checking topics is always fast and does not depend on how many topics there are."
[OK] Correct: Each topic must be checked one by one until a match is found, so more topics mean more checks and more time.
Understanding how topic matching scales helps you design efficient IoT systems and shows you can think about performance in real-world scenarios.
"What if the topics were stored in a hash map instead of a list? How would the time complexity change?"
Practice
Solution
Step 1: Understand the role of topics in IoT
Topics help organize messages by categorizing them into levels separated by slashes.Step 2: Identify the benefit of good topic design
Good topic design makes communication clear and scalable, allowing easy message filtering and management.Final Answer:
To organize messages clearly and make communication scalable -> Option DQuick Check:
Topic design = Clear, scalable communication [OK]
- Confusing topic design with security features
- Thinking topic design affects device hardware
- Assuming topic design saves battery life
Solution
Step 1: Recall MQTT wildcard rules
The plus sign (+) matches one topic level, and the hash (#) matches multiple levels at the end.Step 2: Check syntax correctness
home/+/temperature/# uses + for one level and # at the end for multiple levels, which is valid syntax.Final Answer:
home/+/temperature/# -> Option CQuick Check:
Multi-level wildcard ends with # [OK]
- Placing # in the middle of the topic
- Using + to match multiple levels
- Mixing order of + and # incorrectly
building/+/floor1/#, which published topic will NOT be received by the subscriber?Solution
Step 1: Analyze the subscription pattern
The + matches exactly one level after 'building', and # matches any levels after 'floor1'.Step 2: Test each published topic
Options A, B, and C match 'building/roomX/floor1/...' correctly. building/roomC/floor2/temperature has 'floor2' instead of 'floor1', so it does not match.Final Answer:
building/roomC/floor2/temperature -> Option AQuick Check:
Subscription requires floor1 level [OK]
- Ignoring the exact match required by + wildcard
- Assuming # matches levels before floor1
- Confusing floor1 and floor2 levels
home/+/+/temperature/#/humiditySolution
Step 1: Review MQTT wildcard placement rules
The # wildcard must be the last character in the topic filter and can only appear once.Step 2: Check the given topic
The # wildcard appears in the middle before /humidity, which is invalid syntax.Final Answer:
The # wildcard is not allowed in the middle of the topic -> Option AQuick Check:
# only at end of topic [OK]
- Thinking multiple + wildcards are invalid
- Believing topics must start with slash
- Assuming wildcards can't mix with normal levels
Solution
Step 1: Understand the hierarchy needed
Topics should include building, floor, room, and sensor type levels.Step 2: Analyze each option for scalability and correctness
building/+/floor2/+/temperature uses + to match any building and any room on floor2, then temperature sensor, which is clear and scalable. building/#/floor2/temperature misplaces # wildcard. building/+/+/floor2/temperature has extra + before floor2, which breaks hierarchy. building/+/floor2/temperature/# places # after temperature, which is unnecessary and less clear.Final Answer:
building/+/floor2/+/temperature -> Option BQuick Check:
Use + for flexible levels, keep clear order [OK]
- Misplacing # wildcard inside topic
- Adding extra + levels breaking hierarchy
- Using # after sensor type unnecessarily
Ready to prove it?
Pass the test and this pattern moves unknown
