Publish/Subscribe (Pub/Sub)
Broadcast one event to many listeners, without the sender knowing who they are.
What is it?
A queue works when exactly one worker should handle each piece of work. But sometimes every interested party needs the event: a new order might trigger an email, an inventory update and an analytics event at once.
With publish/subscribe (pub/sub), a publisher sends a message to a named topic, and every subscriber listening to that topic gets its own copy.
Explain like I'm 10
It's like a radio broadcast. The station (publisher) doesn't know who's listening; it just broadcasts on its frequency (the topic). Anyone tuned in (a subscriber) hears the same broadcast, independently of everyone else.
Examples
One event, multiple independent subscribers
// Publisher
eventBus.publish("order.created", { orderId: 42 });
// Subscriber 1
eventBus.subscribe("order.created", (event) => {
sendConfirmationEmail(event.orderId);
});
// Subscriber 2 — completely independent of subscriber 1
eventBus.subscribe("order.created", (event) => {
updateAnalytics(event.orderId);
});How it works
A pub/sub system keeps a set of topics, each with zero or more subscribers. When a publisher sends a message to a topic, every current subscriber gets its own copy.
Publishers and subscribers never talk directly, and neither needs to know the other exists.
Why does it exist?
Pub/sub lets you add a new reaction to an existing event (a new notification type, a new analytics hook) without touching the publishing code. The publisher and each subscriber can be built, deployed and scaled independently.
When to use it
Use pub/sub when one event needs to trigger several independent reactions, especially across services or teams, and when new subscribers may be added later without changing the publisher.
When not to use it
If exactly one worker should handle each message (like processing a payment exactly once), use a queue. Pub/sub is for broadcasting to many listeners, not handing work to exactly one.
Common mistakes
Confusing pub/sub (every subscriber gets every message) with a queue (exactly one worker gets each message). They solve different problems.
Assuming a subscriber that's offline when a message is published will get it later. Depending on the system, it may simply be missed unless durability/replay is configured.
Publishing event data that assumes exactly which subscribers exist, which defeats the point of the publisher not needing to know who's listening.
Practice exercises
- Easy:
Explain the difference between a queue and pub/sub.
- Medium:
Pick a real feature (e.g. a new order) and list three independent subscribers that might react to it via pub/sub.
- Hard:
Explain what could go wrong if a subscriber is down when an important event is published, and how a system might avoid losing that event.
Interview questions
What is publish/subscribe (pub/sub)?
A messaging pattern where a publisher sends messages to a named topic and every current subscriber gets its own copy, without publisher and subscriber knowing about each other.
What's the key difference between pub/sub and a message queue?
A queue delivers each message to exactly one consumer. Pub/sub delivers each message to every subscriber of the topic.
Why is pub/sub useful for adding new features?
You can add a new subscriber to react to an existing event without changing the code that publishes it.