Maintenance

Site is under maintenance — quizzes are still available.

Go to quizzes
Sponsored Reserved space — layout preview until AdSense is connected
Python

Redis Pub/Sub Explained: Real-Time Messaging Made Simple

Learn how Redis Pub/Sub enables real-time messaging for live chat, notifications, and more. This guide explains the core concepts, provides practical Python examples, and covers when to use (or avoid) this pattern.

July 2026 10 min read 1 views 0 hearts

Ever wondered how live chat apps, real-time notifications, or stock tickers work behind the scenes? The secret sauce is often a messaging pattern called publish/subscribe, and Redis makes it surprisingly easy to implement. Let me walk you through how this works in practice.

What Exactly Is Pub/Sub?

Think of it like a radio station. The station broadcasts music (publishes), and anyone with a radio tuned to the right frequency receives it (subscribes). The station doesn't care who's listening, and listeners don't need to know where the music comes from. That's the beauty of pub/sub - publishers and subscribers are completely decoupled.

In Redis, this pattern works the same way. A publisher sends a message to a channel, and any number of subscribers listening to that channel receive it instantly. No polling, no database queries, just pure real-time messaging.

The Core Concept

Redis Pub/Sub has three main components:

  • Publishers - send messages to channels
  • Channels - named pathways for messages
  • Subscribers - listen to channels for new messages

The magic happens because publishers don't need to know who's listening, and subscribers don't need to know who's sending. This makes your system incredibly flexible and scalable.

A Real-World Example

Let's say you're building a live notification system for PythonSkillset.com. When a new article gets published, you want to notify all logged-in users instantly. Here's how you'd set it up:

import redis

# Connect to Redis
r = redis.Redis(host='localhost', port=6379, db=0)

# Publisher - sends notification when new article goes live
def publish_article_notification(article_title):
    message = f"New article published: {article_title}"
    r.publish('article_notifications', message)
    print(f"Published: {message}")

# Subscriber - listens for new articles
def listen_for_articles():
    pubsub = r.pubsub()
    pubsub.subscribe('article_notifications')
    print("Listening for new articles...")

    for message in pubsub.listen():
        if message['type'] == 'message':
            print(f"Received: {message['data'].decode('utf-8')}")

How It Actually Works

When you call publish(), Redis sends the message to all active subscribers on that channel. The key thing to understand is that Redis doesn't store these messages - if nobody's listening, the message disappears forever. This makes Pub/Sub perfect for real-time notifications but not for guaranteed delivery.

Real-World Use Cases at PythonSkillset

At PythonSkillset.com, we use Redis Pub/Sub for several things:

Live comment updates - When someone posts a comment on an article, we publish it to a channel. All users viewing that article get the comment instantly without refreshing.

Server status alerts - Our monitoring system publishes alerts when CPU usage spikes. The operations team's dashboard subscribes and shows real-time warnings.

Cache invalidation - When content gets updated, we publish a message telling all servers to clear their cache for that specific article.

Setting Up a Simple Example

Let's build a basic chat system to see how this works in practice:

import redis
import threading
import time

# Connect to Redis
r = redis.Redis(host='localhost', port=6379, db=0)

def chat_publisher():
    """Simulates a user sending messages"""
    messages = ["Hello everyone!", "How's it going?", "Anyone here?"]
    for msg in messages:
        r.publish('chat_room', msg)
        time.sleep(2)

def chat_subscriber(user_name):
    """Simulates a user receiving messages"""
    pubsub = r.pubsub()
    pubsub.subscribe('chat_room')
    print(f"{user_name} joined the chat")

    for message in pubsub.listen():
        if message['type'] == 'message':
            print(f"{user_name} received: {message['data'].decode()}")

# Start subscriber in background
import threading
sub_thread = threading.Thread(target=chat_subscriber, args=("Alice",))
sub_thread.daemon = True
sub_thread.start()

# Give subscriber time to connect
time.sleep(1)

# Publish messages
chat_publisher()

When to Use Redis Pub/Sub

This pattern shines in specific scenarios:

Real-time notifications - When users need instant updates without refreshing their browser. Think live comments, friend requests, or system alerts.

Broadcast messages - Sending the same information to multiple consumers simultaneously. Perfect for live sports scores or market data.

Simple event systems - When you need to trigger actions across different parts of your application without tight coupling.

When Not to Use It

Pub/Sub has limitations you should know about:

No message persistence - If a subscriber disconnects, it misses all messages sent during that time. For guaranteed delivery, consider Redis Streams or a message queue like RabbitMQ.

No acknowledgment - Publishers never know if subscribers received the message. This is fine for notifications but not for critical transactions.

No message ordering guarantees - While Redis generally delivers messages in order, network issues can cause delays. For strict ordering, use Redis Streams.

Building a Real-Time Notification System

Here's a more practical example from PythonSkillset.com's notification system:

import redis
import json
from datetime import datetime

class NotificationSystem:
    def __init__(self):
        self.redis = redis.Redis(host='localhost', port=6379, db=0)
        self.pubsub = self.redis.pubsub()

    def send_notification(self, user_id, message):
        """Publish a notification to a user's channel"""
        notification = {
            'user_id': user_id,
            'message': message,
            'timestamp': datetime.now().isoformat()
        }
        self.redis.publish(f'user:{user_id}:notifications', json.dumps(notification))

    def listen_for_notifications(self, user_id):
        """Subscribe to a user's notification channel"""
        self.pubsub.subscribe(f'user:{user_id}:notifications')
        print(f"Listening for notifications for user {user_id}")

        for message in self.pubsub.listen():
            if message['type'] == 'message':
                notification = json.loads(message['data'])
                print(f"New notification: {notification['message']}")

# Usage
notifier = NotificationSystem()

# Start listening in background
import threading
listener = threading.Thread(target=notifier.listen_for_notifications, args=("user123",))
listener.daemon = True
listener.start()

# Send a notification
notifier.send_notification("user123", "Your article has been published!")

The Practical Benefits

What makes Redis Pub/Sub so useful in real applications?

Speed - Messages travel through Redis's in-memory engine, so delivery happens in microseconds. For PythonSkillset.com's live comment system, users see new comments appear within 50 milliseconds.

Simplicity - You don't need complex message brokers or queue systems. A few lines of Python code and you have a working pub/sub system.

Scalability - You can have hundreds of subscribers on a single channel without performance degradation. Redis handles the fan-out efficiently.

Common Pitfalls to Avoid

After implementing this at PythonSkillset.com, we learned a few lessons:

Don't use it for critical data - If a subscriber crashes, it loses messages. For important tasks like order processing, use Redis Streams or a proper message queue.

Watch your connection count - Each subscriber opens a connection. With thousands of users, you'll need connection pooling or a different approach.

Pattern subscriptions are powerful - Instead of subscribing to individual channels, you can use patterns like user:*:notifications to catch all user notifications with one subscription.

A More Advanced Example

Here's how we handle multiple channels at PythonSkillset.com:

import redis
import json

class MultiChannelNotifier:
    def __init__(self):
        self.redis = redis.Redis(host='localhost', port=6379, db=0)
        self.pubsub = self.redis.pubsub()

    def subscribe_to_patterns(self):
        """Subscribe to multiple channel patterns"""
        self.pubsub.psubscribe('article:*', 'comment:*', 'user:*:alerts')
        print("Subscribed to all article, comment, and user alert channels")

        for message in self.pubsub.listen():
            if message['type'] == 'pmessage':
                channel = message['channel'].decode()
                data = message['data'].decode()
                print(f"Channel: {channel} - Data: {data}")

    def publish_article_update(self, article_id, update_type):
        """Publish an update to an article channel"""
        channel = f'article:{article_id}'
        message = json.dumps({
            'type': update_type,
            'article_id': article_id,
            'timestamp': time.time()
        })
        self.redis.publish(channel, message)

# Start the listener
notifier = NotificationSystem()
listener_thread = threading.Thread(target=notifier.subscribe_to_patterns)
listener_thread.daemon = True
listener_thread.start()

# Publish some updates
notifier.publish_article_update(42, 'new_comment')
notifier.publish_article_update(42, 'status_change')

Performance Considerations

Redis Pub/Sub is incredibly fast, but there are limits. A single Redis instance can handle millions of messages per second, but each subscriber requires a separate connection. For PythonSkillset.com's production system, we use connection pooling and limit the number of simultaneous subscribers per channel to 1000.

When You Should Look Elsewhere

Pub/Sub isn't the right tool for every job. Consider alternatives when:

  • You need message persistence (use Redis Streams or RabbitMQ)
  • You require exactly-once delivery (use Kafka)
  • You need message ordering across multiple consumers (use Redis Streams)
  • Your subscribers need to process messages at their own pace (use a queue)

The Bottom Line

Redis Pub/Sub is one of those tools that's simple enough to understand in five minutes but powerful enough to build real-time features that users love. At PythonSkillset.com, it powers our live commenting system, notification alerts, and cache invalidation - all with minimal code and impressive performance.

The best part? You can start using it today with just a few lines of Python. Give it a try in your next project, and you'll see why real-time messaging doesn't have to be complicated.

Comments

Questions, corrections, and tips stay visible for everyone reading this page.

0 in thread

Join the discussion

Shown next to your comment.

Up to 4,000 characters

No comments yet

Be the first to leave a note — it helps the next reader.