Pub/Sub
How Pub/Sub works
Section titled “How Pub/Sub works”Redis Pub/Sub follows the classic publish-subscribe pattern:
- A client calls
SUBSCRIBE channeland enters listening mode. It can no longer issue regular commands — it only receives push messages from Redis. - Any other client calls
PUBLISH channel message. Redis immediately forwards that message to every currently connected subscriber of that channel. - The message is never stored. If no subscribers are connected, the message is silently discarded. If a subscriber was connected ten seconds ago but disconnected, it gets nothing.
Each channel is just a name — Redis creates and destroys channels implicitly as subscribers come and go. There is no need to create a channel before publishing to it.
SUBSCRIBE and PUBLISH — two-terminal demo
Section titled “SUBSCRIBE and PUBLISH — two-terminal demo”The interaction below requires two separate redis-cli sessions running at the same time. Start Terminal 1 first, then send from Terminal 2.
# Terminal 1 — subscriber127.0.0.1:6379> SUBSCRIBE newsReading messages... (press Ctrl-C to quit or any command to unsubscribe)1) "subscribe"2) "news"3) (integer) 1
# Terminal 2 — publisher (in a second redis-cli session)127.0.0.1:6379> PUBLISH news "Breaking: Redis is fast"(integer) 1
# Back in Terminal 1 — message arrives automatically1) "message"2) "news"3) "Breaking: Redis is fast"The confirmation message Redis sends back to the subscriber on SUBSCRIBE has three parts:
| Part | Value | Meaning |
|---|---|---|
| 1 | "subscribe" | Event type |
| 2 | "news" | Channel name |
| 3 | (integer) 1 | Total channels this client is now subscribed to |
When a published message arrives, it also has three parts: the event type "message", the channel name, and the payload.
The integer returned by PUBLISH is the number of subscribers who received the message. A return value of 0 means nobody was listening.
PUBLISH news "Breaking: Redis is fast"
PUBLISH news "Another update"UNSUBSCRIBE
Section titled “UNSUBSCRIBE”To leave a channel, a subscribed client sends UNSUBSCRIBE. You can name a specific channel or omit the channel name to unsubscribe from all channels at once.
# Unsubscribe from one channel127.0.0.1:6379> UNSUBSCRIBE news1) "unsubscribe"2) "news"3) (integer) 0
# Unsubscribe from all channels (no argument)127.0.0.1:6379> UNSUBSCRIBE1) "unsubscribe"2) (nil)3) (integer) 0Once the subscription count drops to 0, the client exits listening mode and can issue normal commands again.
SUBSCRIBE to multiple channels
Section titled “SUBSCRIBE to multiple channels”A single SUBSCRIBE call accepts any number of channel names. The client receives messages from all of them on the same connection.
127.0.0.1:6379> SUBSCRIBE sports weather finance1) "subscribe"2) "sports"3) (integer) 11) "subscribe"2) "weather"3) (integer) 21) "subscribe"2) "finance"3) (integer) 3Redis sends one confirmation reply per channel. The third field increments as each subscription is added, giving you the running total.