When to reach for it
- Many agents or services are loosely coupled.
- Events go to multiple receivers.
- Extensibility without central dependencies is important.
Topics, not addresses. Agents subscribe to what they care about.
Agents publish to and subscribe to named topics on a shared bus instead of calling each other directly, so a publisher never knows who consumes its events and subscribers can be added or removed without touching the publisher.
In practiceA sensor-monitoring platform publishes `anomaly.detected` events on a shared bus; three independent agents — alerting, logging, and auto-remediation — each subscribe and react independently without knowing about each other.
Agents do not call each other. They publish messages to a named topic and subscribe to topics, and a broker in between delivers every message to everyone who subscribed.
1 · Set up
2 · At run time
| Subscriber | Reaction | Subscribed |
|---|---|---|
| Alerting | Pages the engineer on call | From the start |
| Logging | Writes the event to the incident log | From the start |
| Remediation | Restarts the affected pump controller | From the start |
| Reporting | Adds the event to the weekly report | Three months later, with no change to the sensor agent |
class Broker:
def __init__(self):
self.subscribers = {} # topic -> list of callbacks
def subscribe(self, topic, callback):
self.subscribers.setdefault(topic, []).append(callback)
def publish(self, topic, message):
# one copy per subscriber
for callback in self.subscribers.get(topic, []):
callback(message) # the publisher never sees who they are
bus = Broker()
bus.subscribe("anomaly.detected", alerting_agent)
bus.subscribe("anomaly.detected", logging_agent)
bus.subscribe("anomaly.detected", remediation_agent)
bus.publish("anomaly.detected", {"sensor": "pump-7", "value": 9.4})When to reach for it
When it backfires
Topics are coined ad-hoc; near-synonyms multiply; agents subscribe to one and miss messages published to the other. The naming is the system's bug surface.
Countermeasure
Centralize topic definitions in a registry with descriptions and ownership. Reject publishes to unregistered topics at the broker.
The bus redelivers an event after a consumer ack times out. A non-idempotent handler runs the side effect twice — a second charge, a duplicate order, a doubled metric.
Countermeasure
Make handlers idempotent keyed by event id, and dedupe within a window at the consumer; never assume exactly-once from the broker.
Keep going
Search patterns, frameworks, and pages.