Pub/Sub
What is it?
Pub/Sub (publish/subscribe) is a messaging pattern where publishers send messages to topics and any number of subscribers receive them, without knowing each other.
Explain like I'm 5
Why was it created?
Direct point-to-point messaging couples senders to specific receivers. Pub/Sub was adopted so one message can fan out to many independent consumers.
Where is it used?
- Broadcasting events to many services
- Real-time notifications
- Decoupled microservices
- Fan-out data pipelines
Why should developers care?
Pub/Sub underlies event-driven systems and real-time fan-out, so backend engineers meet the pattern often.
How does it work?
Publishers send messages to a named topic instead of to specific receivers. Subscribers register interest in that topic and each receives a copy. A broker handles delivery, so publishers and subscribers stay decoupled.
Real-world example
An 'order placed' message is published to a topic; billing, shipping, and analytics each subscribe and react independently to the same event.
Common use cases
- One-to-many event broadcast
- Decoupling producers and consumers
- Real-time updates
- Event-driven architectures
Advantages
- Decouples senders and receivers
- One message reaches many subscribers
- Easy to add new consumers
- Scales fan-out
Disadvantages
- Harder to trace message flow
- Delivery guarantees vary
- Ordering can be tricky
- Needs a broker to operate
When should you use it?
When one event should reach multiple independent consumers.
When should you avoid it?
For simple one-to-one requests where a direct call or single queue is clearer.
Alternatives
Related terms
Interview questions
Beginner
- What does pub/sub stand for?
- What is a topic?
Intermediate
- How does pub/sub differ from a point-to-point queue?
- Why does it decouple services?
Senior
- How do you handle delivery guarantees and ordering in pub/sub?
- When would you choose pub/sub over a queue?
Common misconceptions
- "Pub/sub and a message queue are identical" — a queue typically delivers each message to one consumer; pub/sub fans out to many.
- "Publishers know their subscribers" — they publish to a topic and don't need to know who listens.
Fun facts
- Publishers and subscribers never reference each other directly — the topic is the only link.
- Pub/Sub is one of the foundational patterns of event-driven systems.
Timeline
- 2000s — Pub/Sub messaging becomes a core distributed-systems pattern
Learning resources
Quick summary
Pub/Sub lets publishers send messages to topics that any number of subscribers receive, decoupling senders from receivers for one-to-many delivery.
Cheat sheet
- Publish to topics, subscribe to receive
- One message, many consumers
- Decouples producers/consumers
- Fan-out for event-driven systems