Bookmarks

Publish–subscribe (pub/sub)

A messaging scheme in which a publisher sends messages to a topic without knowing who will receive them, while subscribers read only the topics they are subscribed to.

Also: pub/sub, pub-sub, publishers and subscribers, publisher-subscriber, publish/subscribe model

checked 8 Nov

All terms

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

ModelWhat it meansWhere it is found
at-most-oncethe message is delivered once or not at all; if the subscriber fails, it is lostclassic Redis pub/sub
at-least-oncethe broker stores the message and re-delivers it until the subscriber acknowledges processing; duplicates are possibleKafka, Service Bus
exactly-oncedelivery with no losses and no duplicates — it costs more and requires support on both sidesKafka 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

Verified 8 Nov

Not to be confused with

what it is mixed up with and how it differs

  • message queue

    A queue gives each message to a single consumer from the pool — the competing consumers share the work; pub/sub gives the message to all subscribers of the topic.

  • EDAevent-driven architecture

    Event-driven architecture builds a system out of events and reactions to them; pub/sub is merely the mechanism for delivering those events.

In depth in the references

where the topic is covered in detail