Design publish-subscribe messaging: distinguish fanout from work sharing
Publish-subscribe (pub/sub) is a pattern where a publisher sends a message to a topic or exchange and subscribers receive it through their own subscription paths, instead of sending directly to one recipient. It suits independent systems that each need to react to the same event. First decide whether every subscriber needs its own copy or whether one worker from a pool should handle each task; those are different delivery models.
Publish-subscribe (pub/sub) is a pattern where a publisher sends a message to a topic or exchange and subscribers receive it through their own subscription paths, instead of sending directly to one recipient. It suits independent systems that each need to react to the same event. First decide whether every subscriber needs its own copy or whether one worker from a pool should handle each task; those are different delivery models.
For example, an OrderPlaced event may need to reach order history, inventory reservation, and analytics independently. By contrast, if several workers are sharing inventory-reservation jobs, a work queue should distribute each job to one worker.
Pub/sub versus a work queue
With pub/sub, one event is fanned out to multiple logical subscribers. Each can process at its own pace and retry failures independently. A work queue lets several consumers divide the same set of tasks; adding consumers does not copy every task to all of them.
| Need | Suitable model | Design question |
|---|---|---|
| Inventory, analytics, and notifications each process an order event | A queue or consumer group per subscriber | May each service retry or lag independently? |
| One worker in a pool sends a particular email | Shared work queue or one consumer group | How will duplicate work be safe after uncertain acknowledgement? |
| A new service must read historical events | Retained log with its own subscriber position | What retention and replay range are available? |
| Events must survive a subscriber outage | Durable queue or retained stream | Must it receive events published before it subscribed? |
Calling something a subscriber does not guarantee persistence. A broker may not store messages for an ephemeral subscription, or the subscription route may not exist when a message is published. RabbitMQ’s fanout tutorial shows a log-broadcast case where each active receiver binds a queue and messages may be discarded when no queue is bound. For durable business events, check per-subscriber durable queues, message persistence, publish confirms, permissions, and retention against the product documentation.
How products represent subscriber paths
**RabbitMQ:** publishers send to an exchange rather than directly to a queue. A fanout exchange routes to every bound queue, so independent subscribers need separate queues. Multiple consumers on one queue share that queue’s work. A server-named temporary queue suits a live log listener, not retention while offline.
**Amazon SNS and SQS:** a message published to an SNS topic can fan out to multiple subscribed SQS queues. Each queue stores its own copy for consumers to poll later. SNS push notifications and SQS storage/polling serve different roles; configure permissions and subscription links, and operate each consumer’s failures and retries separately.
**Apache Kafka 4.4:** multiple consumer groups can subscribe independently to a topic. Instances sharing the same `group.id` divide partitions, which resembles work sharing; different groups each read the topic records as logical subscribers. Whether a new consumer can read older events depends on its starting offset, retention, and records still present in the topic.
Do not infer exact semantics from a product feature named “topic.” Check how subscriber identity is defined, whether messages are copied or divided, what is retained while a subscriber is absent, what replay is possible, and how duplicates are handled after acknowledgement failures.
Checklist before implementation
**Define logical subscribers.** Decide which services with separate responsibilities or retry policies need independent routes.
**Choose subscription lifecycle.** Distinguish a live-only broadcast from a durable subscription that stores messages during an outage. Record when subscriptions are created and removed.
**Set failure boundaries.** Define retention, backlog limits, ACK/offset timing, retry, and DLQ policies. Isolate subscriber backlogs so one does not stall another.
**Make duplicates safe.** Each subscriber should use a stable event ID or business key to handle repeated delivery. Pub/sub does not provide end-to-end duplicate-free processing.
**Version the contract.** Document event names, field meanings, optional fields, and compatibility rules. Set an evolution policy so consumers can use only the fields they need.
**Observe subscribers separately.** Track per-subscriber lag, oldest message, failures/redeliveries, DLQ, and replay state, not just publish volume. Avoid needless replication of personal or secret data in message logs.
In a test, create two subscribers and stop one. On restart, verify recovery according to that subscriber’s retention and replay policy. Also check that its failure does not block the other subscriber and that redelivery does not duplicate the business outcome. This is a suggested procedure, not a claim that it was run.
Sources and scope
RabbitMQ, “Publish/Subscribe tutorial”: https://www.rabbitmq.com/tutorials/tutorial-three-python
AWS, “Fanout Amazon SNS notifications to Amazon SQS queues for asynchronous processing”: https://docs.aws.amazon.com/sns/latest/dg/sns-sqs-as-subscriber.html
Apache Kafka 4.4 `KafkaConsumer` API, “Consumer Groups and Topic Subscriptions”: https://kafka.apache.org/44/javadoc/org/apache/kafka/clients/consumer/KafkaConsumer.html
Product behavior is limited to the linked documentation. Subscriber isolation and the checklist are design guidance connecting those behaviors to delivery needs.