Pub/sub and fan-out
Deliver one event to many consumers, and many recipients, efficiently.
15 min readOne event, many listeners
With a queue, each message goes to one consumer. With publish/subscribe, every subscriber gets its own copy. That lets independent services react to the same event (a new post, a new score) without the producer knowing about them.
Fan-out on write vs on read
Delivering one item to many recipients is a classic trade-off.
- Fan-out on write: copy the item into each recipient's inbox when it is created. Reads are fast; writes are expensive for very popular senders
- Fan-out on read: store the item once and merge at read time. Writes are cheap; reads do more work
- Hybrids use write fan-out for normal users and read fan-out for celebrities
Real-time delivery
For live updates, clients keep a persistent connection (WebSocket) to a connection server. When an event arrives, a pub/sub channel routes it to whichever server holds the recipient's connection.
Key takeaways
- Pub/sub decouples producers from any number of consumers
- Choose fan-out on write or read based on the follower distribution
- Persistent connections are stateful; route messages to the right server
Sign in to track lessons and earn badges.