Gauntlet 04: Pub/Sub Broker with Topic Wildcards
Enterprise message brokers like MQTT and RabbitMQ route events using hierarchical topic trees. Subscribers match routing patterns using single-level wildcards (+) and multi-level catch-all wildcards (#), decoupling producers from specific consumer topologies.
# MQTT topic matching:
# 'sensor/+/temp' matches 'sensor/us/temp'
# 'sensor/#' matches 'sensor/us/chicago/temp'
# Gauntlet 04: Pub/Sub Broker with Topic Wildcards In microservice meshes and IoT telemetry pipelines, producers should never know who is listening. A sensor publishes temperature to `facility/building-2/floor-3/temp`. 🔄📡 A **Topic-Based Pub/Sub Broker** matches topics using hierarchical delimiters (`/`) and standard wildcards: - **Exact match**: `facility/building-2/floor-3/temp` - **Single-level wildcard (`+`)**: Matches exactly one path segment between slashes. - `facility/+/floor-3/temp` matches building 1, 2, or 99. - **Multi-level wildcard (`#`)**: Must appear at the end of a pattern; matches all remaining path segments to infinity. - `facility/#` matches every single event produced in that facility. ```python # Topic matching engine: # Pattern: "orders/+/created" # Topic: "orders/us-east/created" -> MATCH ``` ## Pop Quiz What is the difference between the `+` and `#` topic wildcards in MQTT/AMQP message brokers? - [ ] `+` matches exactly one single topic level, whereas `#` matches all remaining levels down the hierarchy. - [ ] `+` routes messages across UDP sockets while `#` routes messages over TCP. - [ ] `#` can only appear at the root of a topic string. - [ ] Because `+` compresses topic strings while `#` encrypts the payload. ## Real-World Challenge Implement `TopicBroker`: - `subscribe(pattern: str, callback_fn)`: Registers a callback for topic patterns supporting `+` and `#`. - `publish(topic: str, payload) -> int`: Delivers `payload` to all matching subscriber callbacks and returns the number of callbacks triggered. <details> <summary>Solution</summary> ```python def match_topic(pattern: str, topic: str) -> bool: p_parts = pattern.split("/") t_parts = topic.split("/") p_idx = 0 t_idx = 0 while p_idx < len(p_parts) and t_idx < len(t_parts): curr_p = p_parts[p_idx] if curr_p == "#": return True # Catch-all matches everything remaining elif curr_p == "+" or curr_p == t_parts[t_idx]: p_idx += 1 t_idx += 1 else: return False if p_idx == len(p_parts) and t_idx == len(t_parts): return True # Trailing '#' with zero remaining topic levels (e.g. 'a/#' matching 'a') if p_idx == len(p_parts) - 1 and p_parts[p_idx] == "#": return True return False class TopicBroker: def __init__(self): self._subscribers = [] # list of (pattern, callback) def subscribe(self, pattern: str, callback_fn): self._subscribers.append((pattern, callback_fn)) def publish(self, topic: str, payload) -> int: match_count = 0 for pattern, callback in self._subscribers: if match_topic(pattern, topic): callback(topic, payload) match_count += 1 return match_count # Verification broker = TopicBroker() dispatched_events = [] # Subscriber 1: Specific metric across any datacenter broker.subscribe("metrics/+/cpu", lambda t, p: dispatched_events.append(("cpu_metric", t, p))) # Subscriber 2: Catch-all audit listener broker.subscribe("metrics/#", lambda t, p: dispatched_events.append(("audit_all", t, p))) # Event 1: Matches both c1 = broker.publish("metrics/us-west/cpu", 88.4) assert c1 == 2 # Event 2: Matches only catch-all c2 = broker.publish("metrics/eu-central/memory/rss", 1024) assert c2 == 1 assert len(dispatched_events) == 3 assert dispatched_events[0] == ("cpu_metric", "metrics/us-west/cpu", 88.4) assert dispatched_events[1] == ("audit_all", "metrics/us-west/cpu", 88.4) assert dispatched_events[2] == ("audit_all", "metrics/eu-central/memory/rss", 1024) ``` </details>