Skip to content
Velloreth
Technology 4 min read

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

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

QueueTopic
Deliveryone consumerevery subscriber
Purposedistribute the loadnotify several
Examplea job queuean 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

GuaranteeLossDuplicate
At-most-oncepossibleno
At-least-oncenopossible
Exactly-oncenono

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.

BadGood
SendEmailorder.created
UpdateInventoryorder.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

ToolNotes
Kafkaa distributed log
RabbitMQa broker with a topic exchange
SNSa managed topic
Redisa pub/sub channel
NATSa 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.

For the related topics, see message-queues-explained and eventual-consistency-guide. For the broader context, idempotency-keys-explained and background-jobs-guide.

Frequently asked questions

What is the publish-subscribe pattern?
A messaging pattern where a publisher sends an event to a topic and every subscriber to that topic receives a copy. The publisher does not know the subscribers, which decouples the two. It is different from a queue, where each message goes to one consumer.
What is the difference between a queue and a topic?
A queue delivers each message to one consumer, which is the work-queue pattern and it distributes the load. A topic delivers each message to every subscriber, which is the publish-subscribe pattern and it notifies several systems. A system such as Kafka is a log that several consumer groups read independently.
What is a fan-out?
One event delivered to several consumers, each of which does its own work. It is how a single order event can trigger an email, an inventory update and an analytics write without the order service knowing about any of them.
Why must a subscriber be idempotent?
Because the delivery is usually at-least-once, so a duplicate is possible. A subscriber that sends an email twice or charges twice is a bug. The fix is a unique constraint on an event id, or a check that the work is already done.
What is the ordering guarantee?
Ordering is per partition or per key, not global. An event with the same key goes to the same partition, so the order is preserved for that key. A global order requires a single consumer, which limits the throughput.
What is an event schema?
The definition of the event payload, with the field names and the types. It is the contract between the publisher and the subscribers, so a change must be compatible. A schema registry enforces it, and a versioned event allows a subscriber to migrate.