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
Memory card and where the analogy breaks
3 min read+10 XP
The memory card (tear this page out)
| # | Question | Memory code |
|---|---|---|
| 23.1 | Queue vs pub/sub? | A job goes to one plumber; the news goes to every subscriber |
| 23.2 | Topics and subscribers? | Name the topic after what happened; let anyone who cares subscribe |
| 23.3 | Consumer groups? | Every team gets the paper; inside a team, the boys split the street |
| 23.4 | Fan-out (write, read, hybrid)? | Doorstep for most, the stall for the giants |
| 23.5 | Event-driven architecture? | Announce what happened; let whoever cares react |
| 23.6 | Duplicates and ordering? | Skip the second copy, and don't let yesterday's paper beat today's |
| 23.7 | Outbox? | Write it in the order book with the order; the relay posts it later |
The whole chapter in one line: Publish facts once to a topic, let each interested service subscribe as a group, push to most and pull for the giants, make subscribers shrug off duplicates and stale events, and publish through an outbox.
Where the analogy breaks
- A newspaper is the same for everyone. A feed is personal: fan-out builds a different list for every reader.
- A reader can ignore yesterday's paper by looking at the date. A service needs explicit versions or sequence numbers to know which event is newer.
- A paper agency doesn't keep last month's papers. A log like Kafka does, so a new subscriber can start from the past and replay history.
Quick check
Five questions on this chapter. Pick an answer, then check it.
quiz