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.
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.
Topic vs queue
| Topic (pub/sub) | Queue (point-to-point) | |
|---|---|---|
| Each message goes to | every subscription | exactly one consumer |
| Used for | notifying many parts of the system about a fact | distributing work among identical workers |
| Adding a consumer | new subscription gets all future events | shares the existing work |
| Kafka equivalent | a different consumer group per subscriber | consumers within one consumer group |
Implementation with Spring Kafka
@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
| Benefits | Costs | |
|---|---|---|
| Coupling | Producers and consumers evolve and deploy independently. | Hard to see who depends on an event; removing a field may break unknown consumers. |
| Scalability | Add subscribers without load on the producer. | Every subscription adds broker storage and throughput. |
| Debugging | Events 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).