Advertisement
Redis isn’t just a key-value store—it's got tricks up its sleeve, and Pub/Sub is one of the more interesting ones. While it doesn't store messages for later delivery, it shines when you want instant, real-time updates between services. Think of it like a fast-moving bulletin board where anyone can post a message, and whoever's tuned in receives it immediately.
So, if you're curious about setting up real-time messaging with Redis, this guide will walk you through how it works, how to use it, and what you should watch out for—step by step. Without any further ado, let’s begin exploring!
At its core, Pub/Sub in Redis lets a client publish messages to a channel, and other clients can subscribe to those channels. Once subscribed, those clients will receive any message that comes through. There’s no queue, no memory of what was said before. It’s in-the-moment. You’re either tuned in when it happens, or you miss it entirely.
This makes Pub/Sub great for chat systems, server-to-server communication, or pushing live updates—situations that you care about now, not five seconds ago.
Here’s a quick outline of the core commands:
Redis handles the delivery. You don’t need to worry about looping through subscribers or writing a message broker. It’s built in and incredibly fast.
Setting up Redis Pub/Sub is simple, but putting it to good use takes a bit of understanding. Here’s how you can get started:

You'll need a running Redis server. Assuming you've already done that, the first step is to open a terminal window and connect to the Redis CLI.
bash
CopyEdit
redis-cli
Once inside, subscribe to a channel:
bash
CopyEdit
SUBSCRIBE news
This command puts the client into subscriber mode. You won’t get a prompt back. Instead, it will now listen and wait.
In a second terminal, open Redis CLI again.
bash
CopyEdit
redis-cli
Now publish a message:
bash
CopyEdit
PUBLISH news "Breaking news: Redis Pub/Sub is blazing fast!"
Go back to the first window. You’ll see the message appear instantly.
That’s all it takes—no setup, no schema, no fuss.
Sometimes, you don't know the exact channel names you'll want. That's where PSUBSCRIBE comes in.
bash
CopyEdit
PSUBSCRIBE tech.*
Now, any message published to tech.news, tech updates, or tech.redis will get picked up.
On the publisher side:
bash
CopyEdit
PUBLISH tech.redis "New Redis version released"
Subscribers listening with a pattern will receive it.
For anything real, you’ll want more than just Redis CLI. Here’s a basic example in Python using redis-py:
python
CopyEdit
import redis
r = redis.Redis()
p = r.pubsub()
p.subscribe('chatroom')
for message in p.listen():
print(message)
The listen() loop keeps checking for new messages and prints them out as they arrive. The format is a dictionary with keys like type, channel, and data.
To publish from code:
python
CopyEdit
r.publish('chatroom', 'Hello from the app!')
Now, you've got real-time messaging right inside your application.
To really use Redis Pub/Sub well, you need to understand what it does not do.
No persistence: If a subscriber isn't connected when the message is sent, that message is lost. It doesn't hang around waiting. That's by design.
No delivery guarantees: Redis doesn’t track delivery. If a client disconnects mid-message, Redis doesn’t retry. You need to handle reconnect logic on your end.
One-way flow: Publishers and subscribers don’t talk to each other. Messages go out; whoever’s listening hears them. That’s it.
Not ideal for large-scale fan-out: Redis Pub/Sub isn’t built for thousands of subscribers across dozens of channels. It can handle a fair bit, but it’s not a full messaging queue. If you need durability or message history, look elsewhere.
Still, for lightweight, high-speed messaging in a controlled setup, Pub/Sub is ideal.
Once you’ve got the basics, you might want to try a few power features. These aren’t required, but they can make your implementation cleaner and more flexible.
If you're publishing and subscribing to the same application, keep two separate connections. The subscription blocks, so you won't be able to run other Redis commands from that client until you unsubscribe.

Sometimes, you want to subscribe to a broader channel but only act on certain types of messages. Adding message metadata as a prefix (or wrapping it in JSON) helps.
json
CopyEdit
{"type": "user-joined," "user": "Alice"}
Your app can then sort or react based on message content, not just the channel name.
Don’t overuse PSUBSCRIBE with vague patterns like *—you’ll get a flood of messages and likely some you didn’t want. Be intentional with how you design your channel naming scheme.
Redis Pub/Sub isn’t trying to be all things to all people—it’s designed for what it’s meant to do: fast, lightweight, in-the-moment communication between parts of your system. There is no message backlog, no retry logic, and no delivery tracking. But that simplicity is also its strength. It’s ideal for scenarios where freshness matters more than reliability—like live notifications, chat messages, or quick status broadcasts. In these cases, it does exactly what you need and nothing more.
If your app needs a way to shout updates to everyone who’s listening—without ceremony or delay—then Pub/Sub is ready to go right out of the box. Keep it focused, build smart subscribers, and Redis will take care of the rest.
Advertisement
Discover AI’s latest surprises, innovations, and big wins transforming industries and everyday life.
Learn about the top 5 GenAI trends in 2025 that are reshaping technology, fostering innovation, and changing entire industries.
Learn the top 5 strategies to implement AI at scale in 2025 and drive real business growth with more innovative technology.
Discover why technical management continues to play a critical role in today's AI-powered workspaces. Learn how human leadership complements artificial intelligence in modern development teams
Explore the underlying engineering of contextual ASR and how it enables Alexa to understand speech in context, making voice interactions feel more natural and intuitive
OpenAI robotics is no longer speculation. From new hires to industry partnerships, OpenAI is preparing to bring its AI into the physical world. Here's what that could mean
Discover how 9 big tech firms are boldly shaping generative AI trends, innovative tools, and the latest industry news.
Learn how advanced SQL techniques like full-text search, JSON functions, and regex make it possible to handle unstructured data
Microsoft and Nvidia’s AI supercomputer partnership combines Azure and GPUs to speed model training, scale AI, and innovation
Generate millions of transects in seconds with Polars and GeoPandas, enabling fast, scalable geospatial analysis for planning
How AWS S3 buckets and security work to keep your cloud data protected. This guide covers storage, permissions, encryption, and monitoring in simple terms
Discover seven powerful ways AI helps manage uncertainty and improve resilience in today's fast-changing business world.