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

Channels vs Keyspace Notifications

Understand the difference between explicit Pub/Sub channels and Redis keyspace notifications, and when to reach for each.

Channels vs Keyspace Notifications is a free Redis Caching & Messaging (Pub/Sub, Streams) lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Redis Caching & Messaging (Pub/Sub, Streams) learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

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.

Frequently asked questions

Is the “Channels vs Keyspace Notifications” lesson free?

Yes — the full text of “Channels vs Keyspace Notifications” is free to read here on the web, and the Redis Caching & Messaging (Pub/Sub, Streams) course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Redis Caching & Messaging (Pub/Sub, Streams) course, upgrade to CoddyKit PRO.

What will I learn in “Channels vs Keyspace Notifications”?

Understand the difference between explicit Pub/Sub channels and Redis keyspace notifications, and when to reach for each. You practise Redis Caching & Messaging (Pub/Sub, Streams) with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Redis Caching & Messaging (Pub/Sub, Streams)?

No prior experience is required. Redis Caching & Messaging (Pub/Sub, Streams) on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Channels vs Keyspace Notifications” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Redis Caching & Messaging (Pub/Sub, Streams) lesson?

Yes. Every Redis Caching & Messaging (Pub/Sub, Streams) lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Introduction to Pub/Sub
  2. Redis Pub/Sub Mechanics
  3. Broadcasting Simple Messages
  4. Channels vs Keyspace Notifications
← Back to Redis Caching & Messaging (Pub/Sub, Streams)