Redis Caching & Messaging (Pub/Sub, Streams) · レッスン

チャネルとKeyspace Notificationsの違い

明示的なPub/SubチャネルとRedisのKeyspace Notificationsの違いと、それぞれを使うべき場面を理解します。

レッスン 4/413 ステップ

「チャネルとKeyspace Notificationsの違い」はCoddyKit上の無料Redis Caching & Messaging (Pub/Sub, Streams)レッスンです。 これはレッスン4/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応の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) — 無料

ブラウザでリアルコードを書いて実行し、24/7 の AI チューターから瞬時にサポートを受け、ウェブまたはアプリで続きから学習できます。

コース
12
レッスン
48

よくある質問

「チャネルとKeyspace Notificationsの違い」レッスンは無料ですか?

はい。「チャネルとKeyspace Notificationsの違い」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Redis Caching & Messaging (Pub/Sub, Streams)コースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Redis Caching & Messaging (Pub/Sub, Streams)コースには全4レッスンが含まれています。

「チャネルとKeyspace Notificationsの違い」で何を学びますか?

明示的なPub/SubチャネルとRedisのKeyspace Notificationsの違いと、それぞれを使うべき場面を理解します。 ブラウザで直接実行するハンズオンコードでRedis Caching & Messaging (Pub/Sub, Streams)を演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。

Redis Caching & Messaging (Pub/Sub, Streams)を始めるのに経験は必要ですか?

事前経験は必要ありません。CoddyKitのRedis Caching & Messaging (Pub/Sub, Streams)は初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン4/4です。

「チャネルとKeyspace Notificationsの違い」レッスンにはどのくらい時間がかかりますか?

ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。

このRedis Caching & Messaging (Pub/Sub, Streams)レッスンでコードを書いて実行できますか?

はい。すべてのRedis Caching & Messaging (Pub/Sub, Streams)レッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。

このコースのすべてのレッスン

  1. Pub/Sub 入門
  2. Redis Pub/Sub の仕組み
  3. シンプルなメッセージのブロードキャスト
  4. チャネルとKeyspace Notificationsの違い
← Redis Caching & Messaging (Pub/Sub, Streams)に戻る