# Publish–Subscribe

A messaging pattern in which publishers send to a topic and each interested subscription receives its own copy.

- Type: framework
- Domain: Engineering & architecture
- Level: Intermediate
- Last reviewed: 2026-09-24
- Original: [Soroush’s Product Wiki](<https://wiki.soroush.uk/topic/publish-subscribe/>)

## What it means

Publish–subscribe, or pub/sub, is a messaging pattern that separates publishers from subscribers through named topics or channels. A publisher emits a message without addressing individual receivers; the messaging system distributes it to each matching subscription. Unlike competing consumers on one queue, separate subscriptions can each receive the same publication.

## Why it matters

One product event can drive independent capabilities such as notifications, analytics, fulfillment, and auditing without the producer calling each system. New subscribers can be added with less producer change, although every subscriber still depends on the event's meaning and delivery behavior.

## When to use it

Use pub/sub when multiple independent consumers should react to the same event or data stream. Use a work queue when one of several workers should perform each task, and use a direct request when the publisher needs an immediate response from a known receiver.

## How it works

1. Define a stable topic and message contract representing a meaningful event or update.
2. Publishers send messages to the topic without selecting individual subscribers.
3. Each consumer creates or uses a subscription with any supported filters and retention policy.
4. The messaging service copies or exposes matching messages to every subscription.
5. Subscribers acknowledge and process deliveries independently, handling duplicates and failures.
6. Teams monitor subscriber lag, failed delivery, schema compatibility, and business completion.

## A practical example

After an order is accepted, the order service publishes one event. Separate subscriptions trigger fulfillment, customer messaging, fraud review, and analytics. A temporary analytics failure builds lag only on its subscription and does not block fulfillment.

## Where it breaks

Pub/sub introduces asynchronous timing, duplicate or out-of-order delivery, and harder end-to-end tracing. Slow subscribers may accumulate costly backlogs, and a loosely governed topic can become an unstable integration contract shared by many teams.

## Common mistakes

- Using one shared queue when every consumer actually needs its own copy.
- Publishing vague events whose meaning changes with the producer's internal model.
- Assuming publication means all subscribers completed their work.
- Ignoring slow, inactive, or repeatedly failing subscriptions.

## Visualization

[View the visualization on the website](<https://wiki.soroush.uk/topic/publish-subscribe/#visualization-publish-subscribe>)

## Sources

1. [Publish-Subscribe Pattern](<https://docs.aws.amazon.com/prescriptive-guidance/latest/cloud-design-patterns/publish-subscribe.html>) — AWS Prescriptive Guidance · Institutional architecture guide
2. [Pub/Sub Overview](<https://cloud.google.com/pubsub/docs/overview>) — Google Cloud · Official technical documentation

## Related topics

- [Message Queue](<https://wiki.soroush.uk/topic/message-queue/>)
- [Message Broker](<https://wiki.soroush.uk/topic/message-broker/>)
- [Event-Driven Architecture](<https://wiki.soroush.uk/topic/event-driven-architecture/>)
