Course contentsShow
← System Design
Lesson 357 of 1,91910. NoSQL: Key-Value StoresFree lesson

Redis Pub/Sub for Real-Time Messaging

Using Redis publish/subscribe for real-time features like chat, notifications, and event broadcasting.

Redis Pub/Sub for Real-Time Messaging

You'll learn: How Redis publish/subscribe enables real-time communication patterns like chat, notifications, and event broadcasting in distributed systems.

The Core Idea

Redis Pub/Sub (publish/subscribe) implements a messaging pattern where senders (publishers) broadcast messages to named channels, and receivers (subscribers) listen to those channels. Unlike traditional request-response, publishers don't know who's listening, and subscribers receive messages immediately as they arrive.

Think of it like a radio station: the broadcaster (publisher) sends content on specific frequencies (channels), and anyone tuned in (subscribers) receives it instantly. Multiple listeners can tune to the same station, and the broadcaster doesn't need to know who's listening.

How It Works

A publisher sends a message to a channel using PUBLISH channel message. Redis immediately delivers that message to all active subscribers listening to that channel via SUBSCRIBE channel.

Redis supports pattern matching too: PSUBSCRIBE news.* subscribes to all channels starting with "news." This is powerful for hierarchical event systems.

Real-Time Use Cases

  • Chat applications: Each room is a channel; users subscribe to rooms and publish messages
  • Live notifications: Publish user-specific events to channels like user:123:notifications
  • Event broadcasting: Microservices publish domain events that other services consume
  • Real-time dashboards: Backend publishes metrics updates; dashboards subscribe for live data

Critical Limitation

Pub/Sub is fire-and-forget. Messages aren't persisted—if no subscribers are listening when you publish, the message disappears. Subscribers only receive messages sent after they subscribe. For guaranteed delivery, you'd need Redis Streams or a message queue.

Key Takeaway: Redis Pub/Sub provides ultra-low-latency, many-to-many real-time messaging through channels, perfect for ephemeral notifications and live updates where guaranteed delivery isn't required.