Pub/Sub Pattern
Decouple producers from consumers with publish-subscribe messaging.
1The Newspaper Analogy
Pub/Sub works the same way. Publishers send to topics, subscribers listen to topics they care about.
Publish-Subscribe (Pub/Sub): A messaging pattern where senders (publishers) send messages to topics, and all subscribers to that topic receive the message.
2Pub/Sub vs Point-to-Point
Point-to-Point (Queue)
Pub/Sub (Topic)
3Use Cases
Event Broadcasting
Order placed → notify inventory, shipping, email, analytics
Real-time Updates
Stock price changes → all subscribers see updates
Microservice Communication
Decouple services through events
Log Aggregation
Multiple services publish logs to central topic
Cache Invalidation
Data changed → notify all cache instances
Webhooks
Event occurs → notify all registered endpoints
4Popular Implementations
Apache Kafka
High-throughput event streamingDistributed event streaming. Persists messages, replay supported.
AWS SNS
AWS-native event fanoutManaged pub/sub service. Integrates with SQS, Lambda.
Google Pub/Sub
GCP event-driven systemsGlobal, scalable messaging. At-least-once delivery.
Redis Pub/Sub
Real-time notificationsSimple, in-memory. No persistence, fire-and-forget.
5Trade-offs
Advantages
- ✓ Loose coupling between services
- ✓ Easy to add new subscribers
- ✓ Scales independently
- ✓ Async, non-blocking
Challenges
- ✗ Message ordering complexity
- ✗ Duplicate handling needed
- ✗ Debugging distributed flow
- ✗ Eventual consistency
6Key Takeaways
?Quiz
1. In pub/sub, how many subscribers receive each message?
2. When should you use pub/sub over a queue?