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

Canali e notifiche keyspace a confronto

Comprenda la differenza tra i canali Pub/Sub espliciti e le notifiche keyspace di Redis, e sappia quando scegliere l'uno o le altre.

Canali e notifiche keyspace a confronto è una lezione Redis Caching & Messaging (Pub/Sub, Streams) gratuita su CoddyKit. Questa è la lezione 4 di 4. Puoi leggere la lezione completa qui gratuitamente — poi esercitati direttamente nel browser con un editor di codice integrato e un tutor IA disponibile 24/7. Fa parte del percorso di apprendimento Redis Caching & Messaging (Pub/Sub, Streams), e i tuoi progressi si sincronizzano tra il web e l'app CoddyKit. Il corso Redis Caching & Messaging (Pub/Sub, Streams) include 4 lezioni in totale.

Parti di questa lezione non sono ancora state tradotte e vengono mostrate in inglese.

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.

Domande Frequenti

La lezione «Canali e notifiche keyspace a confronto» è gratuita?

Sì — il testo completo di «Canali e notifiche keyspace a confronto» è gratuito qui sul web. Per esercitarvi in modo interattivo (un editor di codice integrato e un tutor IA 24/7) e sbloccare il resto del corso Redis Caching & Messaging (Pub/Sub, Streams), passa a CoddyKit PRO. Il corso Redis Caching & Messaging (Pub/Sub, Streams) include 4 lezioni in totale.

Cosa imparerò in «Canali e notifiche keyspace a confronto»?

Comprenda la differenza tra i canali Pub/Sub espliciti e le notifiche keyspace di Redis, e sappia quando scegliere l'uno o le altre. Eserciti Redis Caching & Messaging (Pub/Sub, Streams) con codice pratico che esegui direttamente nel browser, e un tutor IA 24/7 risponde alle tue domande mentre lavori sulla lezione.

Ho bisogno di esperienza per iniziare Redis Caching & Messaging (Pub/Sub, Streams)?

Non è richiesta alcuna esperienza precedente. Redis Caching & Messaging (Pub/Sub, Streams) su CoddyKit è strutturato per principianti e studenti avanzati, quindi puoi iniziare da qui o dall'inizio e procedere al tuo ritmo. Questa è la lezione 4 di 4.

Quanto tempo richiede la lezione «Canali e notifiche keyspace a confronto»?

La maggior parte delle lezioni CoddyKit richiede circa 5–10 minuti. Ogni lezione è breve e interattiva, quindi fai progressi costanti e riprendi esattamente da dove hai lasciato su web e app.

Posso scrivere ed eseguire codice in questa lezione Redis Caching & Messaging (Pub/Sub, Streams)?

Sì. Ogni lezione Redis Caching & Messaging (Pub/Sub, Streams) include un editor di codice integrato, quindi scrivi ed esegui codice reale direttamente nel tuo browser e ricevi feedback istantaneo dall'IA — nessuna configurazione locale necessaria.

Tutte le lezioni di questo corso

  1. Introduzione a Pub/Sub
  2. Meccanismi di Redis Pub/Sub
  3. Trasmettere messaggi semplici
  4. Canali e notifiche keyspace a confronto
← Torna a Redis Caching & Messaging (Pub/Sub, Streams)