Last Updated: January 9, 2026
In the previous chapter, we explored message queues where each message is delivered to exactly one consumer. But what happens when multiple services need to react to the same event?
Consider what happens when a user places an order. The inventory service needs to reserve stock. The notification service needs to send a confirmation email. The analytics service needs to log the event. The fraud detection service needs to check for suspicious patterns.
If we used a traditional queue, only one of these services would receive the message.
This is where publish-subscribe, or pub/sub, comes in. In the pub/sub model, messages are broadcast to all interested subscribers. The publisher does not know or care who is listening. It simply announces that something happened, and anyone interested can react.
Pub/sub is foundational to event-driven architecture and enables loose coupling between services at scale.
In this chapter, you will learn:
Publish-subscribe is a messaging pattern where publishers send messages to a topic (or channel), and all subscribers to that topic receive a copy of the message.
Key differences from message queues:
| Aspect | Message Queue | Pub/Sub |
|---|---|---|
| Delivery | One message → one consumer | One message → all subscribers |
| Coupling | Producer may know about consumer | Publisher does not know about subscribers |
| Purpose | Task distribution, work queues | Event notification, fanout |
| Consumption | Consuming removes message | Each subscriber gets a copy |
Every Pub/Sub system has four essential components.
Publishers are the message producers. They create events and send them to topics. A publisher doesn't care who consumes the message or how many consumers there are.
Topics are named channels that organize messages. They:
orders, user-events)Subscribers express interest in one or more topics. When a message arrives on a topic, the system delivers it to all interested subscribers.
Messages are the data packets flowing through the system. They typically contain:
Example message:
Push delivery: The pub/sub system pushes messages to subscribers as they arrive.
Pull delivery: Subscribers poll the topic for new messages.
| Aspect | Push | Pull |
|---|---|---|
| Latency | Lower (immediate delivery) | Higher (polling interval) |
| Control | Broker controls delivery rate | Subscriber controls fetch rate |
| Complexity | Subscriber needs endpoint | Subscriber manages polling |
| Backpressure | Harder to manage | Natural (subscriber pulls when ready) |
| Use case | Real-time, webhooks | Batch processing, rate control |
Fanout is the core capability of pub/sub: one message reaches multiple subscribers.
Every subscriber gets every message:
Subscribers only receive messages matching their filter:
Inventory only sees OrderPlaced. Notification only sees OrderShipped. Analytics sees everything.
Simple, single-level topic names:
Good for simple systems but can become unwieldy at scale.
Topics organized in a tree structure:
Subscribers can subscribe to patterns:
orders/* - all order eventsorders/placed - only order placed events# - all events (in some systems)Another approach: single topic with event types in the message:
Subscribers filter by message content rather than topic name.
Each subscription has exactly one subscriber. Common for stateful services:
Multiple subscribers share a subscription. Messages are load-balanced:
This combines pub/sub fanout with competing consumers:
Durable: Messages are stored until acknowledged. If subscriber disconnects, messages accumulate and are delivered when it reconnects.
Ephemeral: Messages only delivered to currently connected subscribers. Miss messages while disconnected.
Different technologies implement Pub/Sub with varying trade-offs.
Kafka works differently. It is a distributed log where messages persist and consumers track their position:
Best for: High-throughput event streaming, event sourcing, data pipelines
SNS (Simple Notification Service) is a fully managed Pub/Sub service from AWS.
Best for: AWS-native applications, fan-out to multiple services, notifications
Google's managed Pub/Sub service with global message delivery.
| Feature | Details |
|---|---|
| Type | Fully managed, global |
| Delivery | At least once, push or pull |
| Ordering | Optional per-message key |
| Retention | Up to 7 days |
| Best for | GCP workloads, global scale |
Best for: Global applications, GCP-native workloads, exactly-once processing needs
Redis provides lightweight in-memory Pub/Sub.
Best for: Real-time features, cache invalidation, simple notifications where loss is acceptable
| System | Persistence | Replay | Ordering | Managed | Best For |
|---|---|---|---|---|---|
| Kafka | Yes | Yes | Per-partition | Self/Managed | Event streaming |
| AWS SNS | No | No | No | Yes | Fan-out, notifications |
| Google Pub/Sub | Yes | Yes | Per-key | Yes | Global apps |
| Redis | No | No | No | Self/Managed | Real-time, simple cases |
The simplest pattern: publish an event to tell subscribers that something happened. Each subscriber decides what to do next.
This works well when subscribers are independent and you want loose coupling between services.
Use pub/sub for fan-out, then hand off to per-consumer queues for reliable processing and backpressure.
Each service has its own queue. The pub/sub layer handles fanout. The queues provide reliability and allow independent scaling.
Store state as an append-only sequence of events. Pub/sub distributes those events so other services can build and maintain their own derived views (read models).
Split writes and reads into separate models. Events become the bridge between the write side and the read side.
Most pub/sub systems do not guarantee global ordering. Two subscribers can see the same events in different sequences.
eventTime, sequenceNumber, or versionMost pub/sub systems provide at-least-once delivery. Subscribers may receive duplicates.
Message sent once → may be delivered 1, 2, or many times.
INSERT ... ON CONFLICT DO UPDATE)Failures are inevitable. The question is what happens to the message when processing fails.
As events evolve, you need to handle:
Let us design a notification system using pub/sub:
Design decisions:
Publish-subscribe is a messaging pattern where messages are broadcast to all interested subscribers:
Core concepts:
Key patterns:
Delivery modes:
Design considerations:
Pub/sub enables event-driven architectures where services react to events without tight coupling. Combined with message queues, it provides the foundation for scalable, resilient distributed systems.
Now that we understand both message queues and pub/sub, an obvious question arises: when should you use each? In the next chapter, we will directly compare these two patterns and provide guidance on choosing between them.