The Pub/Sub Pattern: Decoupling With Events
Publish-subscribe decouples a producer from many consumers. Learn the topic, the fan-out, the delivery, and the ordering.
By Velloreth
The pattern
Publisher -> [ topic ] -> Subscriber A
-> Subscriber B
-> Subscriber C
Every subscriber receives a copy.
The publisher does not know the subscribers.
Queue versus topic
| Queue | Topic | |
|---|---|---|
| Delivery | one consumer | every subscriber |
| Purpose | distribute the load | notify several |
| Example | a job queue | an event bus |
A queue is the work-queue pattern.
A topic is the publish-subscribe pattern.
The fan-out
One event, several consumers.
order.created
-> the email service sends a confirmation
-> the inventory service reserves the stock
-> the analytics service records the sale
The order service does not know about any of them.
A new consumer is added without a change to the publisher.
The decoupling
The publisher does not know:
- How many subscribers there are
- Who they are
- What they do
The benefit: a new consumer is added without a change to the publisher.
The cost: the flow is harder to follow, because it is not in the code.
The delivery
| Guarantee | Loss | Duplicate |
|---|---|---|
| At-most-once | possible | no |
| At-least-once | no | possible |
| Exactly-once | no | no |
At-least-once is the common default.
A subscriber must be idempotent, because a duplicate is possible.
The idempotency
async function handleOrderCreated(event) {
const seen = await db.processed.findByEventId(event.id);
if (seen) return; // already handled
await sendConfirmation(event.orderId);
await db.processed.insert({ eventId: event.id });
}
A unique constraint on the event id is the guard.
A subscriber that sends an email twice is a bug.
The ordering
Ordering is per partition or per key.
key=order-1 -> partition 0
key=order-2 -> partition 1
The order is preserved for a key, not across the keys.
A global order requires a single consumer.
The schema
{
"type": "order.created",
"version": 1,
"id": "evt_123",
"occurredAt": "2026-10-26T10:00:00Z",
"data": { "orderId": "o1", "total": 4200 }
}
The schema is the contract between the publisher and the subscribers.
A compatible change adds an optional field.
A breaking change needs a new version.
A schema registry enforces it.
The event design
An event is a fact, not a command.
| Bad | Good |
|---|---|
SendEmail | order.created |
UpdateInventory | order.created |
A fact can have several consumers.
A command has one, and it couples the publisher to the action.
The event carries the data the consumers need, so they do not call back.
The failure
A subscriber that fails must not block the others.
A dead letter queue holds the event that failed repeatedly.
A retry with a backoff.
An alert on the dead letter queue, because a silent failure is the risk.
The tools
| Tool | Notes |
|---|---|
| Kafka | a distributed log |
| RabbitMQ | a broker with a topic exchange |
| SNS | a managed topic |
| Redis | a pub/sub channel |
| NATS | a lightweight broker |
Kafka retains the event, so a new consumer can replay.
SNS is a managed fan-out.
Common mistakes
A command named as an event. A coupling.
A non-idempotent subscriber. A duplicate.
No dead letter queue. A lost event.
No schema. A breaking change.
A global order assumed. A wrong result.
An event with no id. No idempotency.
An event that is too large. A slow broker.
A subscriber that calls back to the publisher. A coupling.
No alert on the dead letter queue. A silent failure.
No replay. A new consumer cannot catch up.
A quotable summary
The publish-subscribe pattern sends an event to a topic, and every subscriber receives a copy, which decouples the publisher from the number and the identity of the consumers. A fan-out delivers one event to several consumers, a subscriber must be idempotent because the delivery is at-least-once, and the ordering is per partition or per key rather than global. An event is a fact rather than a command, and a schema on the event is what keeps the consumers compatible.
Related reading
For the related topics, see message-queues-explained and eventual-consistency-guide. For the broader context, idempotency-keys-explained and background-jobs-guide.