Wildcard subscriptions (+ and #) in IOT Protocols - Time & Space Complexity
When using wildcard subscriptions in IoT protocols, it's important to know how the system handles matching topics. We want to understand how the work grows as more topics or subscriptions are involved.
The question is: how does the matching process scale when wildcards like + and # are used?
Analyze the time complexity of the following subscription matching logic.
function matchTopic(subscription, topic) {
let subLevels = subscription.split('/');
let topicLevels = topic.split('/');
for (let i = 0; i < subLevels.length; i++) {
if (subLevels[i] === '#') return true;
if (subLevels[i] !== '+' && subLevels[i] !== topicLevels[i]) return false;
}
return subLevels.length === topicLevels.length;
}
This code checks if a topic matches a subscription that may include + or # wildcards.
Look for loops or repeated checks.
- Primary operation: Loop through each level of the subscription topic.
- How many times: Up to the number of levels in the subscription (n).
The time to check a match grows as the number of topic levels increases.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 checks |
| 100 | About 100 checks |
| 1000 | About 1000 checks |
Pattern observation: The work grows linearly with the number of topic levels.
Time Complexity: O(n)
This means the matching time increases in direct proportion to the number of topic levels.
[X] Wrong: "Wildcard matching happens instantly regardless of topic length."
[OK] Correct: Each topic level must be checked one by one, so longer topics take more time to match.
Understanding how wildcard matching scales helps you design efficient IoT systems and shows you can think about performance in real-world messaging scenarios.
What if the subscription used multiple # wildcards in different positions? How would that affect the time complexity?
Practice
+ represent in topic subscriptions?Solution
Step 1: Understand the
The+wildcard meaning+wildcard matches exactly one topic level in MQTT subscriptions.Step 2: Compare with other wildcards
The#wildcard matches zero or more levels, so it is different from+.Final Answer:
It matches exactly one topic level. -> Option BQuick Check:
+= one level [OK]
- Confusing + with # wildcard
- Thinking + matches multiple levels
- Assuming + matches zero levels
home/kitchen including sub-levels?Solution
Step 1: Identify the wildcard for all sub-levels
The#wildcard matches all remaining topic levels, including zero or more sub-levels.Step 2: Apply to the topic
Usinghome/kitchenhome/kitchen/#subscribes tohome/kitchenand any deeper topics likehome/kitchen/temperature.Final Answer:
home/kitchen/# -> Option AQuick Check:
#matches all sub-levels [OK]
- Using + instead of # for multiple levels
- Placing # in the middle of the topic
- Using invalid syntax like # in the middle
building/+/temperature, which of these topics will be matched?Solution
Step 1: Understand the + wildcard position
The+wildcard matches exactly one topic level betweenbuildingandtemperature.Step 2: Check each topic against the pattern
Topicbuilding/floor1/temperaturematches becausefloor1fills the + wildcard. Others either have too many or too few levels or wrong last level.Final Answer:
building/floor1/temperature -> Option AQuick Check:
+matches one level only [OK]
- Matching topics with extra levels
- Ignoring the exact position of +
- Assuming + matches zero levels
sensor/#/temperature but it does not work. What is the problem?Solution
Step 1: Recall the rule for # wildcard placement
The#wildcard must be the last character in the topic filter and can only appear once.Step 2: Analyze the subscription
Here,sensor/#/temperature#is in the middle, which is invalid syntax and causes the subscription to fail.Final Answer:
The # wildcard must be at the end of the topic filter. -> Option DQuick Check:
#only at end [OK]
- Placing # in the middle of topic
- Confusing + and # usage
- Ignoring MQTT topic syntax rules
office but only one level deep, such as office/room1 and office/room2, but not office/room1/temperature. Which subscription should you use?Solution
Step 1: Understand the requirement for one level deep
You want to match topics exactly one level belowoffice, likeoffice/room1.Step 2: Choose the correct wildcard
The+wildcard matches exactly one level, sooffice/+matchesoffice/room1but notoffice/room1/temperature.Final Answer:
office/+ -> Option CQuick Check:
One level deep = use + wildcard [OK]
- Using # which matches all levels
- Using multiple + wildcards incorrectly
- Not including any wildcard
Ready to prove it?
Pass the test and this pattern moves unknown
