Redis 发布/订阅机制
学习 Redis 如何实现发布/订阅,包括频道、发布者和订阅者
Redis 发布/订阅机制 是 CoddyKit 上的免费 Redis Caching & Messaging (Pub/Sub, Streams) 课时。 这是第 2 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Redis Caching & Messaging (Pub/Sub, Streams) 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Redis Caching & Messaging (Pub/Sub, Streams) 课程共包含 4 节课。
本课时的部分内容尚未翻译,以英文显示。
Unpacking Redis Pub/Sub
Welcome back! In the previous lesson, we learned what Redis Pub/Sub is. Now, let's dive into the mechanics of how it actually works inside Redis.
We'll explore the main components: channels, publishers, and subscribers. Understanding these roles is key to building real-time features.
Channels: The Message Highways
At the heart of Redis Pub/Sub are channels. Think of a channel as a named topic or a broadcast radio station.
- Named Conduits: Channels are simply names (strings) that messages are sent to and received from.
- No Storage: Redis channels themselves do not store messages. They are purely for real-time message routing.
- Dynamic Creation: Channels are created implicitly when the first client publishes to or subscribes to them.
Publishers: Sending Messages
A publisher is any client that sends a message to a specific channel. Publishers don't need to know who (if anyone) is listening.
- Fire and Forget: Publishers send a message and immediately move on. They don't wait for acknowledgements.
- Decoupled: Publishers are completely decoupled from subscribers. They just need the channel name and the message content.
This decoupling is a major benefit for scalable, real-time applications.
Subscribers: Receiving Messages
A subscriber is a client that expresses interest in receiving messages from one or more specific channels.
- Active Listening: Subscribers actively listen to channels. When a message is published to a subscribed channel, Redis pushes it to the subscriber.
- Client-Side Logic: Subscribers usually have application logic to process the incoming messages.
Subscribers must be connected to Redis to receive messages. If a subscriber disconnects, it misses any messages published during its offline period.
The Message Flow
Let's visualize the simple flow:
- A publisher sends a message to a named channel.
- Redis receives the message and identifies the channel.
- Redis then broadcasts this message to all currently connected subscribers of that specific channel.
- Subscribers receive the message in real-time.
It's like a radio station (channel) broadcasting to many listeners (subscribers) at once.
Publishing via CLI
To publish a message, we use the PUBLISH command. It takes two arguments: the channel name and the message content.
Try running this command in your Redis CLI. It will return the number of clients that received the message.
redis-cli PUBLISH notifications "New user signed up!"Subscribing via CLI
To subscribe to a channel, we use the SUBSCRIBE command. It takes one or more channel names.
When you run this, your CLI will enter a blocking mode, waiting for messages on the notifications channel. You won't see a prompt until you stop it (e.g., Ctrl+C).
redis-cli SUBSCRIBE notificationsPub/Sub's Key Characteristics
Understanding these characteristics is crucial for choosing the right Redis feature:
- Real-time: Messages are delivered instantly to active subscribers.
- Fire-and-Forget: Publishers don't know or care if messages are received.
- No Persistence: If no one is subscribed, the message is lost. Redis Pub/Sub does not store messages for later retrieval.
- Message Ordering: Messages published to a channel are delivered to subscribers in the order they were published.
Pub/Sub vs. Redis Streams
You might wonder how Pub/Sub differs from other messaging patterns. The key difference is persistence.
- Pub/Sub: Ideal for real-time notifications where losing messages for offline subscribers is acceptable (e.g., chat rooms, live updates).
- Redis Streams: Provides message persistence, consumer groups, and replayability. Better for reliable queues, event sourcing, or when messages must not be lost.
We'll cover Streams in a later course!
Quick Check
Which of the following statements accurately describes a Redis Pub/Sub channel?
Recap: Pub/Sub Mechanics
In this lesson, we've broken down the core mechanics of Redis Pub/Sub:
- Channels are the named pathways for messages.
- Publishers send messages to channels in a fire-and-forget manner.
- Subscribers listen to channels and receive messages in real-time.
- Redis Pub/Sub is not persistent; messages are lost if no subscriber is active.
Next, we'll put this into practice by broadcasting simple messages using client libraries!
常见问题解答
「Redis 发布/订阅机制」课时是免费的吗?
是的 — 「Redis 发布/订阅机制」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Redis Caching & Messaging (Pub/Sub, Streams) 课程的其余内容,请升级到 CoddyKit PRO。 Redis Caching & Messaging (Pub/Sub, Streams) 课程共包含 4 节课。
「Redis 发布/订阅机制」这节课中我会学到什么?
学习 Redis 如何实现发布/订阅,包括频道、发布者和订阅者 你通过在浏览器中直接运行的动手代码来练习 Redis Caching & Messaging (Pub/Sub, Streams),全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 Redis Caching & Messaging (Pub/Sub, Streams) 需要有经验吗?
无需任何先前经验。CoddyKit 上的 Redis Caching & Messaging (Pub/Sub, Streams) 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 2 节课,共 4 节。
「Redis 发布/订阅机制」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 Redis Caching & Messaging (Pub/Sub, Streams) 课中编写并运行代码吗?
能。每节 Redis Caching & Messaging (Pub/Sub, Streams) 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。