Pub/Sub
Publish-Subscribe
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
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
// 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
- Message QueueBackend & APIs, p. 29A message queue is a component that stores messages from one service until another is ready to process them, so parts of a system can work asynchronously.
- Event-Driven ArchitectureSoftware Architecture, p. 18Event-driven architecture is a software design style in which services communicate by producing and reacting to events, such as an order being placed.
- Observer PatternSoftware Architecture, p. 31The observer pattern is a behavioral design pattern in which an object, the subject, automatically notifies a list of subscribers whenever its state changes.
- Event StreamingBackend & APIs, p. 14Event streaming is the practice of recording events as a continuous, ordered and durable log that many applications can read, replay and process in real time.
- MicroservicesSoftware Architecture, p. 27Microservices are an architectural style where an application is split into small, independently deployable services that communicate over a network.
- Loose CouplingSoftware Architecture, p. 26Loose coupling is a design principle in which components depend on each other as little as possible, so one can change without breaking the others.
- KafkaBackend & APIs, p. 26Apache Kafka is a distributed event streaming platform that stores events in durable, ordered logs for many services to publish, read in real time or replay.
Spotted a mistake or something missing on this page?Suggest an edit