0Pricing
Redis Caching & Messaging (Pub/Sub, Streams) · Lección

Canales frente a notificaciones de keyspace

Comprenda la diferencia entre los canales explícitos de Pub/Sub y las notificaciones de keyspace de Redis, y cuándo recurrir a cada uno.

Canales frente a notificaciones de keyspace es una lección gratuita de Redis Caching & Messaging (Pub/Sub, Streams) en CoddyKit. Esta es la lección 4 de 4. Puedes leer la lección completa abajo gratuitamente — luego la practicas en el navegador con un editor de código integrado y un tutor de IA 24/7. Forma parte de la ruta de aprendizaje de Redis Caching & Messaging (Pub/Sub, Streams), y tu progreso se sincroniza en la web y la app de CoddyKit. El curso de Redis Caching & Messaging (Pub/Sub, Streams) incluye 4 lecciones en total.

Partes de esta lección aún no han sido traducidas y se muestran en inglés.

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.

Preguntas frecuentes

¿La lección «Canales frente a notificaciones de keyspace» es gratis?

Sí — el texto completo de «Canales frente a notificaciones de keyspace» es gratis para leer aquí en la web. Para practicarla de forma interactiva (editor de código integrado y tutor de IA 24/7) y desbloquear el resto del curso de Redis Caching & Messaging (Pub/Sub, Streams), actualiza a CoddyKit PRO. El curso de Redis Caching & Messaging (Pub/Sub, Streams) incluye 4 lecciones en total.

¿Qué aprenderé en «Canales frente a notificaciones de keyspace»?

Comprenda la diferencia entre los canales explícitos de Pub/Sub y las notificaciones de keyspace de Redis, y cuándo recurrir a cada uno. Practicas Redis Caching & Messaging (Pub/Sub, Streams) con código real que ejecutas directamente en el navegador, y un tutor de IA 24/7 responde tus preguntas mientras trabajas en la lección.

¿Necesito experiencia previa para empezar Redis Caching & Messaging (Pub/Sub, Streams)?

No se requiere experiencia previa. Redis Caching & Messaging (Pub/Sub, Streams) en CoddyKit está estructurado para principiantes hasta estudiantes avanzados, así que puedes empezar aquí o desde el inicio y avanzar a tu ritmo. Esta es la lección 4 de 4.

¿Cuánto tiempo toma la lección «Canales frente a notificaciones de keyspace»?

La mayoría de las lecciones de CoddyKit toman alrededor de 5–10 minutos. Cada una es compacta e interactiva, así que avanzas constantemente y retomas exactamente por donde dejaste en la web y la app.

¿Puedo escribir y ejecutar código en esta lección de Redis Caching & Messaging (Pub/Sub, Streams)?

Sí. Cada lección de Redis Caching & Messaging (Pub/Sub, Streams) incluye un editor de código integrado, así que escribes y ejecutas código real directamente en tu navegador y obtienes retroalimentación instantánea de IA — sin configuración local necesaria.

Todas las lecciones de este curso

  1. Introducción a Pub/Sub
  2. Mecánica de Redis Pub/Sub
  3. Difusión de mensajes sencillos
  4. Canales frente a notificaciones de keyspace
← Volver a Redis Caching & Messaging (Pub/Sub, Streams)