How does pub/sub messaging work in Redis?
Learn how Redis pub/sub delivers real-time messages to channel subscribers, why delivery is fire-and-forget, and when to choose Streams for durability.
Expected Interview Answer
Redis pub/sub is a messaging pattern where publishers send messages to named channels and subscribers listening on those channels receive them in real time, with the Redis server acting as the message broker in between.
Publishers use PUBLISH to send a message to a channel, and clients use SUBSCRIBE (or PSUBSCRIBE for glob-style patterns) to register interest in channels. Delivery is fire-and-forget: messages are pushed only to clients connected at that moment and are never stored, so a subscriber that is offline or slow simply misses them. There is no acknowledgement, persistence, or replay, which makes it fast and simple but unsuitable when guaranteed delivery is required — for that, Redis Streams is the durable alternative.
- Real-time, low-latency message fan-out to many subscribers
- Decouples publishers from subscribers so neither knows about the other
- Simple API with SUBSCRIBE, PSUBSCRIBE, and PUBLISH
- Pattern matching lets one subscriber follow many channels at once
- No polling — the server pushes messages as they arrive
AI Mentor Explanation
Think of a stadium PA announcer calling out live score updates over the tannoy. Anyone in their seat right then hears every ball described the instant it happens, but a fan who stepped out for tea hears nothing about those deliveries — the words vanish into the air with no recording kept for latecomers.
Step-by-Step Explanation
Step 1
Subscribers register
Clients issue SUBSCRIBE channel or PSUBSCRIBE pattern and enter subscribe mode, telling Redis which channels they care about.
Step 2
Publisher sends
A producer runs PUBLISH channel message; Redis returns the number of clients that received it.
Step 3
Server fans out
Redis pushes the message to every client currently subscribed to that channel or a matching pattern.
Step 4
Delivery is instant and transient
Only online subscribers receive it; the message is not stored, queued, or retried for anyone offline.
Step 5
Choose Streams if durability matters
When you need persistence, consumer groups, or replay, use Redis Streams (XADD/XREADGROUP) instead of pub/sub.
What Interviewer Expects
- Clear grasp of publishers, subscribers, and channels
- Understanding that delivery is fire-and-forget with no persistence
- Knowing PSUBSCRIBE enables pattern-based subscriptions
- Awareness of when to use Streams instead of pub/sub
- Recognition that a subscribed connection cannot run normal commands
Common Mistakes
- Assuming messages are stored and replayed for offline subscribers
- Confusing pub/sub with durable Redis Streams
- Expecting acknowledgements or delivery guarantees
- Forgetting that a subscribed client is limited to subscribe-mode commands
- Believing PUBLISH blocks until subscribers process the message
Best Answer (HR Friendly)
“Redis pub/sub lets one part of a system broadcast messages to a named channel while other parts listen on that channel and get the message instantly. It is like a live radio broadcast — whoever is tuned in hears it, but anyone not listening at that moment simply misses it.”
Code Example
# Terminal 1: subscribe to a channel
redis-cli SUBSCRIBE news
# Reading messages... (waits for messages)
# Terminal 2: publish a message
redis-cli PUBLISH news "Breaking: Redis 7 released"
# (integer) 1 <- number of subscribers that received it
# Pattern subscribe to all news.* channels
redis-cli PSUBSCRIBE 'news.*'Follow-up Questions
- How does Redis pub/sub differ from Redis Streams?
- What happens to messages when a subscriber is disconnected?
- How does PSUBSCRIBE pattern matching work?
- Can a client publish while it is subscribed to a channel?
- How would you achieve at-least-once delivery in Redis?
MCQ Practice
1. What happens to a Redis pub/sub message if no client is subscribed to the channel?
Pub/sub is fire-and-forget; a message published to a channel with no current subscribers is simply dropped and never stored.
2. Which command subscribes to channels using glob-style patterns?
PSUBSCRIBE registers interest in channels matching a pattern such as news.*, while SUBSCRIBE takes exact channel names.
3. For guaranteed delivery with replay and consumer groups, which Redis feature should you use instead of pub/sub?
Redis Streams persist messages and support consumer groups and replay, unlike transient pub/sub.
Flash Cards
What are the three roles in Redis pub/sub? — Publishers send to channels, subscribers listen on channels, and the Redis server brokers the delivery.
Is a pub/sub message stored? — No. It is pushed only to currently connected subscribers and is never persisted or replayed.
What does PSUBSCRIBE do? — Subscribes to all channels matching a glob-style pattern, e.g. news.* matches news.sports and news.tech.
When should you use Streams instead of pub/sub? — When you need durability, acknowledgements, consumer groups, or the ability to replay missed messages.