Pub/Sub
Redis Pub/Sub (Publish/Subscribe) is a messaging paradigm where:
- Senders (called publishers) send messages to channels.
- Receivers (called subscribers) listen to channels and receive messages in real time.
This is asynchronous, fire-and-forget messaging. Messages are not stored in Redis — they are only delivered to currently active subscribers.
Key Concepts
| Term | Description |
|---|---|
PUBLISH | Sends a message to a channel. |
SUBSCRIBE | Listens for messages on a channel. |
UNSUBSCRIBE | Stops listening to a channel. |
PSUBSCRIBE | Subscribe to channel(s) using pattern matching. |
PUBSUB | Inspect the state of Pub/Sub (like list of channels, etc.). |
Example
Let's simulate a basic chat system using Redis Pub/Sub. Step-by-step in Redis CLI
Terminal 1: Subscriber
SUBSCRIBE chatroom
Output
Reading messages... (blocking)
1) "subscribe"
2) "chatroom"
3) "Hello, world!"
Terminal 1 is now listening for all messages on the chatroom channel.
| Index | What it represents | Meaning |
|---|---|---|
1) | Event type | "subscribe" = you subscribed to a channel |
2) | Channel name | "chatroom" |
3) | Number of channels you’re subscribed to now | 1 |
Terminal 2: Publisher
PUBLISH chatroom "Hello, world!"
Output:
(integer) 1
It means 1 subscriber received the message.
Explanation
- The subscriber was already listening on chatroom.
- When the publisher sent "Hello, world!" to chatroom, Redis delivered it instantly.
- If no subscribers are listening, the message is discarded.
Pattern Subscription
Redis provides two kinds of subscriptions:
- Direct subscriptions → using
SUBSCRIBE channel(exact channel name). - Pattern-based subscriptions → using
PSUBSCRIBE pattern(wildcard matching).
With PSUBSCRIBE, a client can subscribe to multiple channels at once using glob-style patterns (*, ?, []).
How It Works
- A pattern is matched against channel names.
- When a
PUBLISHcommand sends a message to a channel, Redis checks if the channel name matches any active patterns. - If it matches, all pattern-subscribed clients receive the message, in addition to any direct subscribers.
Supported Wildcards in Patterns
*→ matches zero or more characters.- Example: news.* matches news.sports, news.politics.
?→ matches exactly one character.- Example: user.? matches user.1, user.a, but not user.10.
[]→ matches one character from the set.- Example: room[123] matches room1, room2, room3.
Example Walkthrough
-
Subscribe with a Pattern
In one terminal:
redis-cli
127.0.0.1:6379> PSUBSCRIBE news.*Output:
Reading messages... (press Ctrl-C to quit)
1) "psubscribe"
2) "news.*"
3) (integer) 1 -
Publish to Matching Channels
In another terminal:
127.0.0.1:6379> PUBLISH news.sports "Sports update: Redis is awesome!"
(integer) 1
127.0.0.1:6379> PUBLISH news.weather "Weather update: Sunny day!"
(integer) 1 -
Subscriber Receives Messages
Back in the subscriber terminal:
1) "pmessage"
2) "news.*"
3) "news.sports"
4) "Sports update: Redis is awesome!"
1) "pmessage"
2) "news.*"
3) "news.weather"
4) "Weather update: Sunny day!"pmessage→ type of message (pattern-based).news.*→ the pattern that matched.news.sports→ actual channel."Sports update: Redis is awesome!"→ message content.