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