Publisher–Subscriber

Publish an event once to a topic and let any number of independent services subscribe to it, without the publisher knowing who they are.

Description

A publisher announces that something happened, such as OrderPlaced, by sending one message to a topic on a message broker. Every subscriber gets its own copy, usually through its own queue, and reacts in its own time: send an email, reserve stock, update analytics.

The publisher doesn't know or wait for the subscribers. New reactions are added by subscribing a new service, with no change to the publisher, and a slow or failing subscriber doesn't affect the others. It's the Observer pattern stretched across processes, with durability and retries.

System design

publish OrderPlaced1copy2copy3copy4Order serviceorder-placedtopicEmail serviceInventoryAnalytics

Publisher–Subscriber system design diagram

  1. Order service (service).
  2. order-placed (topic): topic.
  3. Email service (service).
  4. Inventory (service).
  5. Analytics (service).
  6. Step 1: Order service → order-placed: publish OrderPlaced.
  7. Step 2: order-placed → Email service: copy.
  8. Step 3: order-placed → Inventory: copy.
  9. Step 4: order-placed → Analytics: copy.
  1. The order service publishes one OrderPlaced event to the topic and carries on; it doesn't wait for anyone.
  2. The email service gets its own copy and sends the confirmation.
  3. Inventory gets a copy and reserves the stock. If it's down, its queue keeps the message until it's back.
  4. Analytics gets a copy too, added later without touching the order service.
AWS
Amazon SNS with SQS queues per subscriber; Amazon EventBridge; Amazon MSK (Kafka).
Azure
Azure Service Bus topics and subscriptions; Event Grid; Event Hubs.
Google Cloud
Pub/Sub topics and subscriptions; Eventarc.

When to use it

Use it when

  • Several services need to react to the same business event.
  • The publisher shouldn't wait for, or know about, the reactions.
  • You expect more consumers of the event over time.
  • Subscribers must survive restarts and catch up on missed messages.

Avoid it when

  • You need an answer back to continue. That's a request/response call, not an event.
  • There's exactly one consumer and always will be. A plain queue is simpler.
  • Strict global ordering across all events is required. Most brokers only order within a partition or key.

Pitfalls

  • Assuming exactly-once delivery. Brokers deliver at least once, so subscribers must handle duplicates (idempotent handlers).
  • Events that are too thin ("order 42 changed"), forcing every subscriber to call back to the publisher. Include what subscribers need.
  • Event schemas changing without versioning, breaking subscribers you've never heard of. Treat events as a public contract.
  • No dead-letter queue, so poison messages block a subscriber forever.

Alternatives

Queue-Based Load Leveling

Choose it when there's one consumer and the goal is smoothing load, not fanning out.

Competing Consumers

Choose it when each message should be handled by exactly one of many identical workers.

Observer

Choose it when everything runs in one process and messages don't need to survive a crash.

Event Sourcing

Choose it when the events are the system of record, not just notifications.