Skip to content
System Design Lab
Microservice Patterns

Publish/Subscribe

Publish an event once and let any number of independent subscribers receive their own copy — new consumers can be added without touching the producer.

Beginner 3 min read· Updated 2026-11-18

What you will learn

  • Distinguish publish/subscribe (topic) from point-to-point (queue).
  • Add a consumer without changing or redeploying the producer.
  • Understand per-subscriber delivery, lag and isolation.
  • Model subscriptions with Kafka consumer groups or broker topics.

Before you start

Overview

A newsletter is publish/subscribe: the author writes one issue, and everyone who subscribed gets a copy. The author does not know or care who the readers are, and a new reader can subscribe at any time without the author changing anything.

In publish/subscribe a producer publishes an event to a topic. Each subscription receives every event independently, keeps its own position and processes at its own speed. The producer only knows the topic, never the consumers.

Run it

A shows fan-out: three subscribers, three copies. B adds a Fraud subscriber without touching Orders. C makes Analytics very slow — notice Email and Loyalty are unaffected. D wires the same three services to a queue instead: each event goes to only one of them.

Loading simulation…

Topic vs queue

Topic (pub/sub)Queue (point-to-point)
Each message goes toevery subscriptionexactly one consumer
Used fornotifying many parts of the system about a factdistributing work among identical workers
Adding a consumernew subscription gets all future eventsshares the existing work
Kafka equivalenta different consumer group per subscriberconsumers within one consumer group

Implementation with Spring Kafka

Two independent subscribers to the same topic
@Component
class EmailSubscriber {
    @KafkaListener(topics = "order-events", groupId = "email")      // its own consumer group
    void on(OrderPlaced event) { mail.sendConfirmation(event); }
}

@Component
class AnalyticsSubscriber {
    @KafkaListener(topics = "order-events", groupId = "analytics")  // another group: also gets every event
    void on(OrderPlaced event) { warehouse.record(event); }
}

Advanced Each consumer group tracks its own offsets, so a slow or crashed subscriber builds lag only for itself. Keep the event schema stable (versioned, additive changes only): once several teams subscribe, the event is a public contract.

Trade-offs

BenefitsCosts
CouplingProducers and consumers evolve and deploy independently.Hard to see who depends on an event; removing a field may break unknown consumers.
ScalabilityAdd subscribers without load on the producer.Every subscription adds broker storage and throughput.
DebuggingEvents form an audit trail.Flows are implicit; you need tracing and a schema registry.

Production considerations

  • Document events in a catalogue (owner, schema, consumers) and validate schemas in CI.
  • Monitor lag per subscription; a stuck subscriber is invisible to the producer.
  • Make subscribers idempotent and tolerant of out-of-order events (see Idempotent Consumer).

Further reading

Related topics