Pub/sub is a way of decoupling the sender from the receivers: instead of direct calls, the publisher posts messages to named channels (topics), and an intermediary — a message broker or an event bus — distributes them to all subscribers. The publisher knows neither the receivers nor how many there are; the subscribers do not know the source. A new subscriber is added without changing the publisher’s code.
How it works
- The publisher publishes a message to a topic — for example,
orders.created— and immediately gets on with its work. - The broker copies the message to every subscriber of the topic: one message per subscriber.
- The subscribers work independently: one writes a record to the database, another sends a notification, the third builds a report.
- If a subscriber is down, the broker stores the messages until it comes back — on brokers with storage, such as Kafka.
Topics and filtering
A topic is a named channel through which the broker finds subscribers. In MQTT, you subscribe to a topic hierarchy with wildcards: sensors/+/temperature is every sensor, sensors/# is their entire stream. In AMQP 0-9-1 brokers such as RabbitMQ, the publisher writes to an exchange, and the exchange type — fanout, topic, direct — along with the bindings decides which queues the message ends up in.
Delivery guarantees
| Model | What it means | Where it is found |
|---|---|---|
| at-most-once | the message is delivered once or not at all; if the subscriber fails, it is lost | classic Redis pub/sub |
| at-least-once | the broker stores the message and re-delivers it until the subscriber acknowledges processing; duplicates are possible | Kafka, Service Bus |
| exactly-once | delivery with no losses and no duplicates — it costs more and requires support on both sides | Kafka transactions |
Two practical rules follow from this: subscribers make their processing idempotent, so that a repeated message does not change the outcome, and for the “message got lost” case there is a dead-letter queue, where the broker puts the messages it could not process.
How pub/sub differs from a queue
In a message queue, each message goes to one consumer — that is how work is divided. In pub/sub, every subscriber of the topic gets the message — that is how events are fanned out. The two models combine: in Kafka, a consumer group reads the partitions of a topic like a queue, while different groups are independent — each sees the whole stream. A queue does not fan a message out to every subscriber, and in pure pub/sub the subscribers work blindly: the publisher learns nothing about who processed its message, or how.
Sources
the term is written from these pages- Redis — Redis Pub/Subredis.io/docs/latest/develop/pubsub/(opens in a new tab)
- MQTT Version 5.0 — OASIS Standarddocs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html(opens in a new tab)
- Microsoft Learn — Publisher-Subscriber patternlearn.microsoft.com/en-us/azure/architecture/patterns/publisher-subscriber(opens in a new tab)
Verified 8 Nov