Problem
A producer that calls each interested service directly must know every consumer, and adding one means changing and redeploying the producer. Slow or failed consumers block the producer. Teams cannot evolve independently.
Also known as: pub-sub, topic-based messaging
Decouple producers from consumers by broadcasting messages to a topic that any number of independent subscribers receive.
A producer that calls each interested service directly must know every consumer, and adding one means changing and redeploying the producer. Slow or failed consumers block the producer. Teams cannot evolve independently.
Publish-subscribe lets a producer emit a fact once, such as OrderPlaced, and any number of subscribers react to it. The producer talks only to the broker, never to consumers, so the set of listeners can change at will.
Each subscription is an independent cursor over the topic. Billing, shipping and analytics each get every message, at their own pace, without affecting one another.
In practice teams choose between broker styles. Log-based brokers retain messages so new subscribers can replay history, while queue-based brokers delete after acknowledgement. Decide early which model you need, because replay changes how you handle bugs, backfills and new consumers.
The broker fans a published message out to every subscription. Each subscription tracks its own acknowledgements, so one slow consumer does not delay others.
producer --publish--> [ topic: orders ]
|-- sub: billing (offset 120)
|-- sub: shipping (offset 118)
\-- sub: analytics (offset 97)
Delivery is usually at least once, so a subscriber may see the same message again after a timeout. Subscription filters, such as matching on event type or tenant attribute, reduce wasted work. Operationally, monitor lag per subscription, since each one fails and recovers independently, and set alerts on the oldest unacknowledged message rather than only on queue size.
Medium - managed broker or cluster
Medium - topic, ACL and retention management
Medium - implicit flows
Slow subscriber backlog grows until retention drops messages
Duplicate delivery causes double side effects
Topic sprawl and unclear ownership of event contracts