Core Module
12 min forge
Pub/Sub Pattern
Master the communication backbone of distributed systems. Learn how to broadcast messages to multiple subscribers in real-time.
π£ Pub/Sub (Publish-Subscribe) Pattern
Pub/Sub is a messaging pattern where senders (publishers) do not programmatically send messages to specific receivers. Instead, characterize published messages into classes without knowledge of which subscribers there may be.
π‘ The Logic (ELI5)
Think of a Newspaper Subscription:
- The Publisher: The New York Times. They write articles and put them in a "Topic" (e.g., Sports, Business).
- The Subscriber: You. You don't call the reporter every day. You just "Subscribe" to the Sports section.
- The Decoupling: The reporter doesn't know you exist. They just write. The delivery truck (Message Broker) makes sure everyone who subscribed to "Sports" gets the paper.
π The Deep Dive
Components
- Topic: A specific category of messages (e.g.,
user_login,order_shipment). - Publisher: The service that creates a message for a topic.
- Subscriber: The service that listens to a topic.
- Message Broker: The middleman (Kafka, Google Pub/Sub, Redis) that handles the logic of who gets what.
Benefits
- Many-to-Many: One message can hit 100 different services.
- Independence: You can take down a subscriber for maintenance without breaking the publisher.
- Fan-out: Triggering multiple actions from one event (e.g., "User Bought Item" triggers Email, Stock Update, and Analytics).
π― Interview Pulse
Pub/Sub vs. Message Queues
- Queue: Usually 1-to-1. One task for one worker.
- Pub/Sub: Always 1-to-Many. One event for many listeners.
The Problem of "Consumer Groups"
In big systems like Kafka, if you have 10 instances of your "Email Service," you don't want all 10 to send the same email to the same user. The Solution: Group them as a "Consumer Group." The broker will give the message to only one instance in that group.
Popular Tools
- Apache Kafka (Highest scale, persistent).
- Google Cloud Pub/Sub.
- Redis Pub/Sub (Fast, but messages are lost if no one is listening). π‘