Course contentsShow
← System Design
Lesson 1675 of 1,91940. Real-World System: Social Media FeedFree lesson

Pub-Sub for Real-Time Distribution

Using message brokers like Redis Pub-Sub or Kafka to distribute updates to connected users across server instances.

Pub-Sub for Real-Time Distribution

What you'll learn: How message brokers enable efficient real-time update distribution across multiple server instances in social media systems.

The Core Challenge

Imagine you've built a social feed with WebSocket connections, but your users connect to different server instances behind a load balancer. When Alice posts an update, her followers might be connected to 20 different servers. How does Alice's server notify all the other servers that need to push updates to their connected clients?

The Pub-Sub Solution

Publish-Subscribe (Pub-Sub) messaging solves this by introducing a central message broker. When a server needs to broadcast an update, it publishes a message to the broker. All servers interested in those updates subscribe to relevant channels and receive the message instantly.

Think of it like a radio station: the broadcaster (publisher) transmits on a frequency, and anyone tuned to that channel (subscribers) receives the broadcast simultaneously.

How It Works in Practice

When Alice posts:

  1. Her server publishes the new post event to a channel (e.g., feed:updates)
  2. The message broker (Redis Pub-Sub, Kafka) broadcasts to all subscribed servers
  3. Each server checks if any of its connected WebSocket clients follow Alice
  4. Those clients receive the real-time push

Redis Pub-Sub works well for ephemeral, low-latency messaging where losing occasional messages is acceptable. Kafka provides durability and replay capabilities, essential when you need guaranteed delivery and processing history.

Channel Strategy

You can organize channels by:

  • User IDs: user:12345:feed (each user's personal feed updates)
  • Topics: trending:posts (global trending content)
  • Geographic regions: region:us-west:feeds

Key Takeaway: Pub-Sub message brokers decouple your server instances, enabling any server to broadcast updates that reach all relevant connected users, regardless of which physical server they're connected to.