Dashboard
Courses
Problems
Badges
Theme
Sign inStart free
Courses/Asynchronous and Event-Driven Systems
Lesson 3 of 20

Pub/sub and fan-out

Deliver one event to many consumers, and many recipients, efficiently.

15 min read

One 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.

Queues and background workStreams, logs and delivery guarantees

Coming up

Course: 0/20 lessons · 0/10 challenges0%

Build it on the canvas, pass the review (70+) and mark it complete to count it toward the course badge.

  1. 2/10Design a Notification SystemFan notifications out to slow providers with idempotent sends, backoff with jitter and a dead-letter queue.Medium
  2. 3/10Design a Newsletter SenderDrain 5 million sends through a rate-limited email provider without overwhelming it or sending twice.
Easy
All 10 challenges →