Back to Blog
Lesson 23 of the Redis: Redis Essentials & Data Types course
DatabasesAugust 10, 20264 min read

Introduction to Pub/Sub: Real-Time Messaging with Redis

Master Redis Pub/Sub to build real-time, event-driven applications. Learn how to publish messages and subscribe to channels to decouple your system architecture.

RedisPub/SubNode.jsReal-timeMessagingBackend
Flat lay of white tiles spelling 'WELCOME' on a red background

Previously in this course, we explored Introduction to Sorted Sets, where we learned to manage ranked data. This lesson shifts our focus from data storage to data distribution: we are moving from static structures to dynamic, real-time messaging using the Publish/Subscribe (Pub/Sub) pattern.

Understanding Pub/Sub from First Principles

In traditional database operations, you "push" data to a table or "pull" it using a query. Pub/Sub is different. It is a communication pattern where senders (publishers) do not send messages directly to specific receivers (subscribers). Instead, messages are categorized into "channels."

When a publisher sends a message to a channel, Redis instantly broadcasts that message to every client currently listening to that channel. Key characteristics include:

  • Fire-and-forget: If a subscriber isn't connected at the exact moment a message is published, it misses that message. Redis Pub/Sub does not persist messages.
  • Decoupling: The publisher doesn't need to know who the subscribers are, or even if any exist.
  • High Performance: Because it happens entirely in memory, it is ideal for real-time notifications, chat systems, or triggering background tasks.

Implementing Pub/Sub in Node.js

To work with Pub/Sub, you need two separate client instances: one to publish and one to subscribe. In Redis, a connection that enters "subscriber mode" cannot execute regular commands (like GET or SET), which is why your application must maintain distinct connections.

Here is a concrete example using the node-redis library.

1. The Subscriber

The subscriber creates a dedicated connection, listens to a specific channel, and defines a handler for incoming messages.

JAVASCRIPT
const redis = require(CE9178">'redis');

async function startSubscriber() {
  const subscriber = redis.createClient();
  await subscriber.connect();

  // Subscribe to the CE9178">'notifications' channel
  await subscriber.subscribe(CE9178">'notifications', (message) => {
    console.log(CE9178">`Received message: ${message}`);
  });

  console.log(CE9178">'Subscribed to "notifications" channel...');
}

startSubscriber();

2. The Publisher

The publisher uses a standard connection to push data into the channel.

JAVASCRIPT
const redis = require(CE9178">'redis');

async function publishMessage() {
  const publisher = redis.createClient();
  await publisher.connect();

  const channel = CE9178">'notifications';
  const message = CE9178">'Hello, real-time world!';

  await publisher.publish(channel, message);
  console.log(CE9178">`Published: "${message}" to ${channel}`);
  
  await publisher.quit();
}

publishMessage();

Hands-on Exercise

  1. Open two terminal windows. In the first, run the Subscriber script. In the second, run the Publisher script.
  2. Observe the output in the subscriber terminal.
  3. Challenge: Modify the subscriber to listen to two channels simultaneously (e.g., notifications and alerts) and print which channel the message arrived on. Hint: Use psubscribe if you want to use pattern matching (like alerts.*), but for simple channels, you can call subscribe multiple times.

Common Pitfalls

  • Blocking Connections: As mentioned, once a client calls SUBSCRIBE, it is locked into "subscriber mode." Don't try to reuse a subscriber client to run SET or GET commands; you will receive an error. Always use a dedicated client instance for subscriptions.
  • Message Loss: Because Redis doesn't store these messages, Pub/Sub is not a durable message queue. If your subscriber crashes, it will not receive messages sent while it was offline. If you need guaranteed delivery, look into Introduction to Message Brokers for more robust alternatives.
  • Fire-and-forget Nature: If you need to ensure a message is processed even if the consumer is temporarily down, Pub/Sub is not the right tool. Use it for "nice-to-have" real-time updates, not for critical state changes that require persistence.

Frequently Asked Questions

Q: Can I use Pub/Sub to store messages for later? A: No. Redis Pub/Sub is volatile. If no one is listening, the message effectively disappears.

Q: How many channels can one client subscribe to? A: A single client can subscribe to many channels at once.

Q: Is there a way to verify if a message was delivered? A: No. The publisher receives an integer representing the number of clients that received the message, but it doesn't confirm that those clients successfully processed the logic.

Recap

We’ve learned that Pub/Sub is a powerful, low-latency mechanism for broadcasting information. By using dedicated connections, we can separate our messaging logic from our standard CRUD operations. We’ve touched on the ephemeral nature of these messages—a crucial distinction for system design.

Up next: We will leverage this knowledge to build a full Real-Time Notification System that bridges our existing API logic with live user feedback.

Similar Posts