CONFIG SET notify-keyspace-events 'KEA'
SUBSCRIBE __keyevent@0__:expired
Topics
40
Caching Patterns
Cluster Sharding & Hash Slots
Clustering
Connection Pooling & Client Libraries
Data Types
Distributed Locks with Redis
Expiration & Eviction
Geospatial Data in Redis
Hashes in Redis
HyperLogLog in Redis
Keyspace Notifications
Leaderboards with Sorted Sets
Lists in Redis
Lua Scripting
Persistence (RDB/AOF)
Pub/Sub
Rate Limiting with Redis
Redis Architecture & Installation
Redis as a Message Queue
Redis Backup & Disaster Recovery
Redis CLI & Basic Commands
Redis Memory Optimization
Redis Modules Overview
Redis Monitoring & Observability
Redis Performance Tuning & Benchmarking
Redis Replication
Redis Security & ACL
Redis Sentinel & High Availability
Redis vs Memcached
Redis with Docker & Kubernetes
RedisBloom (Probabilistic Data Structures)
RedisJSON
RedisSearch (Full Text Search)
RedisTimeSeries
Session Management with Redis
Sets & Sorted Sets in Redis
Streams
Strings & Bitmaps in Redis
TLS & Encryption in Redis
Transactions
Keyspace Notifications
7 questions found
Keyspace notifications let Redis publish events whenever certain operations happen to keys, such as a key being set, deleted, or expiring, letting other parts of your application subscribe to and react to these changes in real time, rather than needing to constantly poll Redis to check if something has changed.
Real-world example
A shopping cart application listens for expired key events, automatically cleaning up related order data whenever a customer's abandoned cart session expires, without needing a separate polling process.
Pub/Sub;Expiration & Eviction
How do you enable keyspace notifications in Redis, and what do the different configuration character options represent?
BeginnerYou enable keyspace notifications using the CONFIG SET command on the notify-keyspace-events setting, combining character flags where K enables keyspace events, E enables keyevent events, and additional letters like g, s, or x specify which specific types of commands or events, such as expired keys, should actually trigger a notification.
CONFIG SET notify-keyspace-events 'Ex'
-- E enables keyevent notifications
-- x specifically enables expired event notifications
Real-world example
A session management system enables notifications specifically for expired events using the Ex flag combination, keeping notification overhead minimal by not subscribing to every possible type of keyspace change.
Pub/Sub;Redis Performance Tuning & Benchmarking
How do you subscribe to and handle a specific type of keyspace notification, such as being alerted whenever a key expires?
IntermediateYou subscribe to the special channel named __keyevent@0__:expired, where zero represents the database number, using a regular Pub/Sub SUBSCRIBE command, and your application then receives a message containing the name of the key each time one expires in that particular database.
SUBSCRIBE __keyevent@0__:expired
-- Your application receives a message
-- containing the key name each time a key expires
Real-world example
A cache warming system subscribes to expired key events, automatically triggering a background job to refresh a piece of cached data the moment it actually expires, rather than waiting for the next request to trigger a slow regeneration.
Pub/Sub;Caching Patterns
What are the reliability limitations of keyspace notifications that developers should understand before relying on them for critical application logic?
IntermediateKeyspace notifications are delivered using Redis's Pub/Sub mechanism, which does not guarantee delivery, meaning if a subscribing client is disconnected or too slow to keep up when a notification is published, that specific notification is simply lost forever, making keyspace notifications unsuitable for critical logic that absolutely must never miss an event.
-- If no subscriber is actively listening when a key expires,
-- that specific expiration event is lost forever
SUBSCRIBE __keyevent@0__:expired
Real-world example
A team initially relied on keyspace notifications to trigger a critical billing calculation, then redesigned their system after realizing a brief subscriber disconnection had caused several important events to be silently missed.
Streams;Pub/Sub
How would you build a reliable cache invalidation system using keyspace notifications, while accounting for their delivery limitations?
AdvancedYou would use keyspace notifications as a fast, best effort mechanism to quickly invalidate related cache entries, but combine it with a periodic reconciliation process that independently verifies cached data against the source of truth, ensuring that even if a notification is occasionally missed, stale data does not persist indefinitely.
SUBSCRIBE __keyevent@0__:set
-- On notification, invalidate related cache entries
-- A separate periodic job also double checks cache freshness
Real-world example
An e-commerce platform uses keyspace notifications for fast, immediate cache invalidation when product data changes, while also running an hourly reconciliation job that catches any rare cases where a notification might have been missed.
Streams;Caching Patterns
What performance considerations should you keep in mind when enabling keyspace notifications on a high throughput Redis instance?
AdvancedEnabling keyspace notifications adds a small amount of overhead to every single operation on keys matching the enabled notification types, since Redis must publish a message for each matching event, so on a very high throughput system, you should enable only the specific notification types you actually need rather than broadly enabling all possible events, minimizing unnecessary overhead.
-- Enable only the specific events actually needed
CONFIG SET notify-keyspace-events 'Ex' -- only expired events, not everything
Real-world example
A high throughput application carefully enables keyspace notifications only for expired events specifically, avoiding the additional overhead that would come from broadly notifying on every single write operation across the entire dataset.
Redis Performance Tuning & Benchmarking;Redis Monitoring & Observability
What practical use cases in a real application are well suited to keyspace notifications despite their delivery limitations?
IntermediateKeyspace notifications work well for non critical, best effort scenarios such as triggering a cache refresh, logging activity for monitoring dashboards, sending a real time update to a connected user interface, or triggering cleanup tasks, where occasionally missing a notification would not cause serious harm to the application's correctness.
SUBSCRIBE __keyevent@0__:expired
-- Trigger a non critical UI update or cleanup task
-- when a temporary key expires
Real-world example
A live dashboard displaying currently active user sessions subscribes to expired key notifications, updating the displayed count in near real time, accepting that an occasional missed notification would only cause a brief, harmless display inaccuracy.
Pub/Sub;Redis Monitoring & Observability