23. Pub/Sub and Fan-Out
1
The picture to keep in your head2
23.1 What's the difference between a queue and pub/sub?3
23.2 What are topics and subscribers?4
23.3 What are consumer groups?5
23.4 What are the fan-out strategies?6
23.5 What is event-driven architecture?7
23.6 How do you handle duplicates and ordering?8
23.7 How do you publish events reliably (the outbox pattern)?9
Memory card and where the analogy breaks10
Practice: five levels11
Interview questions12
Solutions13
Sources14
Code lab23. Pub/Sub and Fan-Out
23.5 What is event-driven architecture?
3 min read+10 XP
On the paper round: The press doesn't call each reader to tell them the news. It prints the paper. Whoever subscribes reads it and does what they like: the stockbroker trades, the student cuts out the crossword. The press never needs to know.
In the system: In event-driven architecture, services announce facts ("order placed", "payment captured", "book released") instead of calling each other with instructions:
Figure: with events, a new feature is a new subscriber; the publisher doesn't change.
What you gain:
- Loose coupling. Checkout doesn't know about loyalty points. Adding a "recommend similar books" service means subscribing to
order_placed, not editing checkout. - Resilience. If the loyalty service is down, orders still go through; it catches up from the topic later.
- A history. With a retained log, events are a record you can replay to rebuild a view or feed a new service (Chapter 22).
What it costs:
- Harder to follow. "What happens when an order is placed?" is now spread across many services. You need tracing with a correlation ID (Chapter 30) and a catalogue of who publishes and subscribes to what.
- Eventual consistency. Points appear a moment after the order (Chapter 16).
- Coordination across steps needs care: a multi-step business process driven purely by events is choreography; with a coordinator it's orchestration (Chapter 17's sagas).
Two styles of event payload worth naming:
- Notification events: "order 881 placed"; consumers call back for details.
- Event-carried state: "order 881 placed, total ₹1,248, items [...]"; consumers don't need to call back, at the cost of bigger events.
Memory code: "Announce what happened; let whoever cares react."