Channels und Keyspace Notifications im Vergleich
Verstehen Sie den Unterschied zwischen expliziten Pub/Sub-Channels und Redis-Keyspace-Notifications und wann Sie welche Variante einsetzen sollten.
Channels und Keyspace Notifications im Vergleich ist eine kostenlose Redis Caching & Messaging (Pub/Sub, Streams)-Lektion auf CoddyKit. Dies ist Lektion 4 von 4. Du kannst die komplette Lektion unten kostenlos lesen – dann übst du sie direkt im Browser mit einem integrierten Code-Editor und einem KI-Tutor rund um die Uhr. Sie ist Teil des Redis Caching & Messaging (Pub/Sub, Streams)-Lernpfads, und dein Fortschritt wird über Web und CoddyKit-App synchronisiert. Der Redis Caching & Messaging (Pub/Sub, Streams)-Kurs umfasst insgesamt 4 Lektionen.
Teile dieser Lektion wurden noch nicht übersetzt und werden auf Englisch angezeigt.
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.sportsWhat 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.
Kkeyspace eventsEkeyevent eventsAall command/event typesxexpired,ggeneric,$string
CONFIG SET notify-keyspace-events KEAKeyspace vs Keyevent
There are two channel families:
- Keyspace:
__keyspace@0__:mykeytells you which event happened to a key - Keyevent:
__keyevent@0__:deltells you which keys had a given event
The @0 is the database number.
PSUBSCRIBE __keyspace@0__:*
PSUBSCRIBE __keyevent@0__:expiredSubscribing 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__:expiredA 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:abcDelivery 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 EgxSummary 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.
Häufig gestellte Fragen
Ist die Lektion „Channels und Keyspace Notifications im Vergleich“ kostenlos?
Ja — der vollständige Text von „Channels und Keyspace Notifications im Vergleich“ ist hier im Web kostenlos zu lesen. Um sie interaktiv zu üben (integrierter Code-Editor und 24/7 KI-Tutor) und den Rest des Redis Caching & Messaging (Pub/Sub, Streams)-Kurses freizuschalten, upgrade auf CoddyKit PRO. Der Redis Caching & Messaging (Pub/Sub, Streams)-Kurs umfasst insgesamt 4 Lektionen.
Was lerne ich in „Channels und Keyspace Notifications im Vergleich“?
Verstehen Sie den Unterschied zwischen expliziten Pub/Sub-Channels und Redis-Keyspace-Notifications und wann Sie welche Variante einsetzen sollten. Du übst Redis Caching & Messaging (Pub/Sub, Streams) mit praktischem Code, den du direkt im Browser ausführst, und ein 24/7 KI-Tutor beantwortet deine Fragen während du die Lektion bearbeitest.
Brauche ich Erfahrung, um Redis Caching & Messaging (Pub/Sub, Streams) zu starten?
Keine Vorkenntnisse erforderlich. Redis Caching & Messaging (Pub/Sub, Streams) auf CoddyKit ist für Anfänger bis fortgeschrittene Lernende strukturiert, sodass du hier starten oder von Anfang an beginnen und in deinem eigenen Tempo voranschreiten kannst. Dies ist Lektion 4 von 4.
Wie lange dauert die Lektion „Channels und Keyspace Notifications im Vergleich“?
Die meisten CoddyKit-Lektionen dauern etwa 5–10 Minuten. Jede ist kompakt und interaktiv, sodass du stetig Fortschritte machst und genau dort weitermachst, wo du aufgehört hast – im Web und in der App.
Kann ich in dieser Redis Caching & Messaging (Pub/Sub, Streams)-Lektion Code schreiben und ausführen?
Ja. Jede Redis Caching & Messaging (Pub/Sub, Streams)-Lektion enthält einen integrierten Code-Editor, sodass du echten Code direkt in deinem Browser schreibst und ausführst und sofort KI-Feedback erhältst — ohne lokale Einrichtung erforderlich.
Alle Lektionen in diesem Kurs
- Einführung in Pub/Sub
- Funktionsweise von Redis Pub/Sub
- Einfache Nachrichten broadcasten
- Channels und Keyspace Notifications im Vergleich