0Pricing
Redis Caching & Messaging (Pub/Sub, Streams) · 课时

通道与键空间通知

理解显式的发布/订阅通道与 Redis 键空间通知之间的区别,以及何时应选择其中一种

通道与键空间通知 是 CoddyKit 上的免费 Redis Caching & Messaging (Pub/Sub, Streams) 课时。 这是第 4 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Redis Caching & Messaging (Pub/Sub, Streams) 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Redis Caching & Messaging (Pub/Sub, Streams) 课程共包含 4 节课。

本课时的部分内容尚未翻译,以英文显示。

Two Ways to Get Notified

So far you have published to explicit channels with PUBLISH. Redis also offers keyspace notifications, which automatically emit events when keys change. Both deliver messages to subscribers, but the source differs.

Recap: Explicit Channels

With explicit channels, your application decides what to publish and when. The channel name is arbitrary text.

PUBLISH news.sports "Match ended 2-1"
SUBSCRIBE news.sports

What Are Keyspace Notifications?

Keyspace notifications let Redis itself publish an event whenever a key is modified, expired, or evicted. You subscribe to special channels to observe data changes without your app having to publish anything.

Enabling Notifications

They are off by default. Enable them with the notify-keyspace-events config flag, where letters select event classes.

  • K keyspace events
  • E keyevent events
  • A all command/event types
  • x expired, g generic, $ string
CONFIG SET notify-keyspace-events KEA

Keyspace vs Keyevent

There are two channel families:

  • Keyspace: __keyspace@0__:mykey tells you which event happened to a key
  • Keyevent: __keyevent@0__:del tells you which keys had a given event

The @0 is the database number.

PSUBSCRIBE __keyspace@0__:*
PSUBSCRIBE __keyevent@0__:expired

Subscribing to Expirations

A common use case is reacting when a key expires, for example to clean up a session. Enable Ex and subscribe to the expired keyevent channel.

CONFIG SET notify-keyspace-events Ex
SUBSCRIBE __keyevent@0__:expired

A Practical Flow

Set a session key with a TTL. When it expires, Redis publishes to the expired channel, and your worker reacts.

SET session:abc "data" EX 30
# 30s later, subscriber receives:
# __keyevent@0__:expired -> session:abc

Delivery Caveats

Keyspace notifications use Pub/Sub, so they are fire-and-forget. If no client is subscribed when the event fires, the message is lost. They are also not guaranteed for keys that expire while idle until a read or the background expiry cycle touches them.

When to Use Each

Use explicit channels when your app controls the event semantics (chat, notifications, custom events). Use keyspace notifications when you want to react to data mutations you do not directly control, like cache invalidation or expiry cleanup.

Performance Note

Enabling A (all events) on a busy server generates a high volume of messages and adds overhead. Subscribe only to the specific event classes you need.

CONFIG SET notify-keyspace-events Egx

Summary So Far

Explicit channels are app-driven; keyspace notifications are Redis-driven. Both ride on the same lightweight Pub/Sub transport, so neither persists messages.

Quick Check

Pick the correct statement about keyspace notifications.

Recap

You compared explicit Pub/Sub channels with keyspace notifications, learned how to enable notifications via notify-keyspace-events, the keyspace vs keyevent channel families, and the fire-and-forget delivery caveats. Reach for keyspace notifications to react to data changes without publishing them yourself.

常见问题解答

「通道与键空间通知」课时是免费的吗?

是的 — 「通道与键空间通知」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Redis Caching & Messaging (Pub/Sub, Streams) 课程的其余内容,请升级到 CoddyKit PRO。 Redis Caching & Messaging (Pub/Sub, Streams) 课程共包含 4 节课。

「通道与键空间通知」这节课中我会学到什么?

理解显式的发布/订阅通道与 Redis 键空间通知之间的区别,以及何时应选择其中一种 你通过在浏览器中直接运行的动手代码来练习 Redis Caching & Messaging (Pub/Sub, Streams),全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 Redis Caching & Messaging (Pub/Sub, Streams) 需要有经验吗?

无需任何先前经验。CoddyKit 上的 Redis Caching & Messaging (Pub/Sub, Streams) 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 4 节课,共 4 节。

「通道与键空间通知」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 Redis Caching & Messaging (Pub/Sub, Streams) 课中编写并运行代码吗?

能。每节 Redis Caching & Messaging (Pub/Sub, Streams) 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. 发布/订阅简介
  2. Redis 发布/订阅机制
  3. 广播简单消息
  4. 通道与键空间通知
← 返回 Redis Caching & Messaging (Pub/Sub, Streams)