Archprimer
PatternBeginner 6 min read

Pub/Sub

Publishers emit events to a topic and subscribers consume them independently, decoupled through a broker so producers and consumers never know each other.

pub-submessagingevent-drivendecoupling

By Ratish Jain · Updated

Diagram with 4 components: Publisher, Broker, Subscriber A, Subscriber B. Connections: Publisher → Broker (1 publish); Broker → Subscriber A (2 deliver); Broker → Subscriber B (3 deliver).
ServiceQueue
Broker fans events out to subscribers

Definition

Publish/Subscribe is a messaging pattern where publishers send messages to a topic rather than to specific recipients, and subscribers register interest in topics to receive matching messages. A broker sits in between, decoupling the two sides.

Publishers and subscribers never reference each other directly — they only know the topic. The broker fans each published event out to all interested subscribers.

The Problem It Solves

Point-to-point calls tightly couple sender and receiver: the sender must know every consumer's address, and adding a new consumer means changing the producer. Synchronous calls also make the producer wait and fail if a consumer is down.

Pub/Sub decouples them in space, time, and synchronization — new subscribers can be added without touching publishers, and the broker buffers messages when consumers are offline.

How It Works

  1. 1A publisher sends a message tagged with a topic to the broker.
  2. 2Subscribers have previously subscribed to topics of interest.
  3. 3The broker matches the message's topic to subscriber subscriptions.
  4. 4It delivers a copy of the message to every matching subscriber (fan-out).
  5. 5Each subscriber processes the message independently, at its own pace.
  6. 6Subscribers can join or leave without the publisher's knowledge.

Pub/Sub vs message queue

Pub/Sub vs message queue

In a point-to-point queue, each message is consumed by exactly one worker (competing consumers). In pub/sub, each message is broadcast to all subscribers. Kafka blends both via consumer groups; RabbitMQ via exchanges and bindings.

Tradeoffs

Pros
  • Full decoupling of producers and consumers.
  • Easy to add new subscribers without changing publishers.
  • Asynchronous — publisher doesn't wait on consumers.
  • Natural fan-out to many independent consumers.
Cons
  • Broker becomes critical infrastructure to operate.
  • Harder to trace and debug event flows end-to-end.
  • Delivery guarantees (at-least/at-most-once) add complexity.
  • No direct response path; needs separate reply mechanism.

Interview Questions

How does pub/sub differ from a point-to-point queue?

A queue delivers each message to exactly one consumer among competing workers, ideal for work distribution. Pub/sub broadcasts each message to all subscribers, ideal for event notification and fan-out. The intent is one-of-N processing versus all-of-N notification.

How do you guarantee a subscriber doesn't lose events?

Use durable subscriptions and persistent topics so the broker retains messages, acknowledge only after successful processing so redelivery occurs on failure, and design consumers to be idempotent to tolerate at-least-once delivery.

60-Second Revision
  • →Publishers emit to topics; subscribers consume via a broker.
  • →Producers and consumers are fully decoupled.
  • →Each event is fanned out to all interested subscribers.
  • →Contrast with queues (one consumer per message).
  • →Needs durability + idempotency for reliable delivery.

Related

Everything on Archprimer is connected — follow the graph.