Pub/Sub Pattern Explained (Publish/Subscribe)
On this page
The registration handler that keeps growing
When a user signs up, the registration endpoint creates the account, then calls the email service, then the CRM sync, then billing setup. Last month marketing asked for an analytics call too. Next month it'll be a fraud check. Every new reaction means editing, testing and deploying the registration code, and any of those calls failing breaks sign-up.
Point-to-point vs publish/subscribe
Point-to-point (a work queue): each message is processed by one consumer. Several workers share the load. "Resize this image" should happen once.
Publish/subscribe: each message is delivered to every subscriber. "User registered" is news that several services each act on independently.
The publisher sends an event: a statement of fact in the past tense, like UserRegistered, not a command like SendWelcomeEmail. It doesn't know or care who listens.
Worked example: publishing a UserRegistered event
The registration endpoint now does one thing after creating the account: it publishes this event.
{ "type": "UserRegistered", "user_id": 8812, "email": "sam@example.com", "plan": "trial", "at": "2026-10-10T09:14:00Z" }Subscribers, each with its own queue:
- Notifications sends the welcome email.
- CRM sync creates the sales record.
- Billing starts the trial clock.
Adding analytics next month means a new subscriber with its own subscription. Registration code doesn't change. If the CRM is down, its messages wait in its own queue; email and billing carry on.
How to implement pub/sub: SNS, RabbitMQ and Kafka
Each subscriber needs its own copy of the stream, so one slow subscriber doesn't hold up the others:
- Amazon SNS + SQS: publish to an SNS topic; each subscriber has its own SQS queue subscribed to it (the fan-out pattern).
- RabbitMQ: a fanout or topic exchange with one queue per subscriber (Day 44).
- Kafka: one topic; each subscriber is its own consumer group (Day 43).
- Google Cloud Pub/Sub, Azure Service Bus topics: subscriptions per consumer, built in.
Redis Pub/Sub also exists, but it delivers only to subscribers connected at that moment and stores nothing. Fine for live notifications, not for work that must not be lost.
A new subscriber only sees events published after it subscribes. With SNS and SQS, or RabbitMQ, the analytics service you add next month has no idea who registered before it existed; you'll need a one-off backfill from the source database. With Kafka, the new consumer group can read the retained history instead.
Pub/sub drawbacks
- Hidden flow. In a call chain you can read one function and see everything that happens. With events, "what happens after a user registers?" means finding every subscriber. Keep an event catalog and use distributed tracing.
- Event contracts. Subscribers depend on the event's shape. Treat it like a public API: add fields freely, never remove or rename without a versioned migration (Day 55).
- No answer back. The publisher doesn't learn whether subscribers succeeded. Each subscriber needs its own retries, dead-letter queue and monitoring.
- Duplicates and order. At-least-once delivery means subscribers must be idempotent.
- Personal data spreads. The
UserRegisteredexample carries an email address. Once published, it sits in every subscriber's queue, logs and copies, maybe for the whole retention period. When that user asks to be deleted, every copy is in scope. Publish an id and let subscribers that need the email fetch it, or keep personal fields to the minimum.
Use pub/sub where the set of interested parties is likely to grow or change, which in most businesses is often.
Check yourself
Pub/sub only delivers events published after the subscription exists. Backfill existing users from the source database once, or use a retained log like Kafka for streams new consumers must replay.
With one queue per subscriber, every subscriber receives every message, and each consumes at its own pace. A slow or crashed subscriber only builds up its own backlog. With a shared queue, subscribers would compete for messages, so each would see only some of them.
They state facts that have happened, which any service may react to. Commands like SendEmail tie the publisher to a specific receiver and action.
In point-to-point, each message is processed by one consumer, typically a job for a pool of workers. In publish/subscribe, every interested subscriber gets its own copy of each message. A SendWelcomeEmail command fits the first; a UserRegistered event fits the second.
The publisher no longer knows who depends on its events, so changing an event's shape can break consumers it has never heard of. You need versioned event schemas, an event catalog and tracing to keep the system understandable.