Pub/sub fan-out
Pub/sub fan-out
Gold
A publisher sends each message once to a topic, and the topic hands a copy to every subscription — unlike a queue, where each message goes to one consumer. The publisher doesn't know or wait for its subscribers, so adding one needs no change to it. Each subscription usually has its own queue, so a slow or failing subscriber falls behind alone. The cost is volume — N subscribers turn each message into N deliveries.
- Use when
- One event that several independent services react to — push, email, an in-app inbox · Adding new consumers later without touching the producer · Subscribers that work at different speeds or are sometimes offline
- Avoid when
- Work meant for exactly one worker — use a queue · The sender needs a reply straight away · Strict ordering, or one transaction across the sender and all its consumers · A few consumers that each need quite different data — separate queues or direct calls are simpler
- Trade-offs
- Loose coupling, but a message is harder to trace end to end · N subscribers multiply deliveries, storage and downstream load by N · Each subscriber needs its own buffer, workers and monitoring
- Requires
- Competing consumers
3 objectives · 11 misconceptions
- explain Explain how a topic gives every subscriber its own copy of each message where a queue gives each message to one consumer, and why adding a subscriber needs no change to the publisher.
- predict Predict the message volume and each subscriber's backlog when one publish rate fans out to several subscribers that work at different speeds.
- choose Choose a topic or a queue from whether a message is a job for one worker or news for anyone interested — and combine them, with a queue per subscriber and competing workers on it.
- Not true: Subscribers to a topic split its messages between them, like workers on a queue.
- Not true: Every service that takes from a queue gets its own copy of each message on it.
- Not true: A job meant for one worker can go on a topic, and it will still be done only once.
- Not true: Adding a subscriber means changing the publisher.
- Not true: One slow subscriber holds up delivery to all the others.
- Not true: Fanning out to more subscribers costs nothing extra, because the publisher still sends one message.
- Not true: The publisher waits until every subscriber has handled the message.
- Not true: Building a feed when it is opened is one quick query, however many accounts the reader follows.
- Not true: Each follower's feed should hold full copies of the posts.
- Not true: Copying each post into every follower's feed costs about the same for any author, however many followers they have.
- Not true: A post with millions of followers is handled by adding enough feed workers to copy it quickly.
Learn
Pub/sub fan-outMembers~12 minPractice
Notify’s big nightMembersmediumCheck
Station test-out80% to passNeighborhood
contrast withCompeting consumers