Skip to main content

Pub/Sub

Publish-Subscribe

Updated 2 min read

Share this page

Send the link, quote the definition with a link back, or show it as a card on your own site.

https://softwaredictionary.org/terms/pub-sub

In short

Pub/sub is a messaging pattern where publishers send messages to topics and every subscriber to a topic gets a copy, without either side knowing the other.

What is pub/sub?

In the publish-subscribe pattern, senders called publishers don't send messages to specific receivers. Instead, they publish each message to a topic, also called a channel, such as order.placed, and a message broker delivers a copy to every subscriber that has registered interest in that topic. Publishers don't know who, or how many, the subscribers are, and subscribers don't know who published.

When an online store publishes order.placed, the email service sends a receipt, the inventory service reserves stock, and the analytics service records the sale, each as an independent subscriber. Adding a new feature, such as a loyalty points service, means adding one more subscriber without changing the publisher at all. Pub/sub is offered by brokers and platforms such as Redis, RabbitMQ, NATS, MQTT brokers for IoT devices, Apache Kafka, and the managed messaging services of cloud providers.

Pub/sub works like a magazine subscription: the publisher puts out an issue, everyone subscribed receives their own copy, and people who aren't subscribed get nothing. Delivery guarantees vary a lot between systems. Some are fire-and-forget, so a subscriber that is offline simply misses the message, while others store messages durably so subscribers can catch up when they come back.

Pub/sub is often confused with a message queue and with the observer pattern. In a message queue, each message is processed by exactly one consumer, which is ideal for sharing work among workers, while pub/sub delivers each message to every subscriber; many systems combine the two, with a topic fanning out to one queue per subscribing service. The observer pattern is the in-process version of the same idea, where objects subscribe to another object's events inside one program, while pub/sub puts a broker in the middle and usually crosses the network.

At a glance

An online store publishes an order.placed message to a topic, and the email, inventory and analytics services, all subscribed to that topic, each receive their own copy.publishcopycopycopyPublisheronline storeTopicorder.placedSubscribersEmail servicesends a receiptInventory servicereserves stockAnalytics servicerecords the sale
The publisher doesn't know who is listening: every subscriber to the topic gets a copy, and adding one more never changes the publisher.

Key takeaways

  • Publishers send messages to topics, and every subscriber to a topic gets a copy.
  • Publishers and subscribers don't know about each other, which keeps services loosely coupled.
  • New subscribers can be added without changing the publisher.
  • Delivery guarantees vary: some systems drop messages for offline subscribers, others store them.
  • A queue sends each message to one consumer; pub/sub sends it to all subscribers.

Example

Publishing and subscribing with Redisjavascript
// Pub/sub with Redis, using the node-redis client
import { createClient } from "redis";

const publisher = createClient();
const subscriber = publisher.duplicate();
await Promise.all([publisher.connect(), subscriber.connect()]);

// Every service that subscribes to the channel gets its own copy
await subscriber.subscribe("order.placed", (message) => {
  const order = JSON.parse(message);
  console.log("Send receipt for order", order.id);
});

// The publisher doesn't know or care who is listening
await publisher.publish("order.placed", JSON.stringify({ id: 1001, total: 59.9 }));

Readers ask

What is the difference between pub/sub and a message queue?

In a message queue, each message goes to one consumer, so several workers can share the load. In pub/sub, every subscriber receives its own copy of each message, so several different services can react to the same event.

Is Kafka pub/sub?

Kafka supports pub/sub, because many consumer groups can read the same topic independently. Within a single consumer group, each partition is read by only one consumer, so Kafka can also behave like a queue for sharing work.

What happens to messages when a subscriber is offline?

It depends on the system. Fire-and-forget systems drop the message for that subscriber, while durable systems keep it until the subscriber acknowledges it or until a retention period expires.

Often compared

See also

Spotted a mistake or something missing on this page?Suggest an edit

Read a random page
Open today's review
Switch to the dark theme
Read this page in Türkçe

More

Settings