What Is Pub/Sub?
Publish-Subscribe Pattern
Publish-Subscribe (pub/sub) is a messaging pattern where publishers send messages to topics (not specific receivers) and subscribers receive messages from topics they're interested in. Publishers and subscribers are completely decoupled — they don't know about each other.
How Pub/Sub Works
Redis pub/sub: publisher sends to channel 'order-events'. Three subscribers listen: email service (sends confirmation), analytics (records sale), inventory (updates stock). Add a fourth subscriber without changing the publisher or existing subscribers. Google Cloud Pub/Sub and Amazon SNS provide managed pub/sub.
Key Concepts
- Topics/Channels — Named categories for messages — subscribers choose which topics to listen to
- Decoupling — Publishers don't know who subscribes; subscribers don't know who publishes
- Fan-Out — One published message delivered to all subscribers on that topic
Frequently Asked Questions
Pub/sub vs message queue?
Pub/sub delivers messages to all subscribers (fan-out). Message queues deliver each message to one consumer (work distribution). Use pub/sub for notifications/events, queues for task processing.