Add Redis Keyspace Notifications Support for Redis Cluster#3962
Conversation
|
Hi, I’m Jit, a friendly security platform designed to help developers build secure applications from day zero with an MVS (Minimal viable security) mindset. In case there are security findings, they will be communicated to you as a comment inside the PR. Hope you’ll enjoy using Jit. Questions? Comments? Want to learn more? Get in touch with us. |
There was a problem hiding this comment.
Pull request overview
Adds a new keyspace notifications abstraction to redis-py, including parsing helpers and a cluster-aware subscription manager intended to work across Redis Cluster primary nodes.
Changes:
- Introduces
redis/keyspace_notifications.pywithEventType,NotifyKeyspaceEvents, channel builders,KeyNotificationparsing, andClusterKeyspaceNotifications(+ worker thread). - Exports the new API surface from
redis/__init__.py. - Adds unit/integration tests and a generation/requirements spec document.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 14 comments.
| File | Description |
|---|---|
redis/keyspace_notifications.py |
New module implementing notification parsing, channel helpers, and a cluster-aware subscription manager with topology refresh + background worker. |
redis/__init__.py |
Re-exports the new keyspace notification classes/helpers at the package top-level. |
tests/test_keyspace_notifications.py |
Adds parsing/unit tests plus mocked cluster behavior tests and cluster usability tests. |
specs/keyspace-notifications/SPEC.md |
Adds a spec describing Redis keyspace notifications behavior and testing expectations. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
98e6219 to
ae895e9
Compare
🛡️ Jit Security Scan Results✅ No security findings were detected in this PR
Security scan by Jit
|
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 4 out of 4 changed files in this pull request and generated 10 comments.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
You can also share your feedback on Copilot code review. Take the survey.
ae895e9 to
ebb2b05
Compare
3e578d2 to
665c597
Compare
43063ef to
6b6002f
Compare
765371c to
dc24f12
Compare
601a007 to
3a46ad1
Compare
6bdb0bd to
f8d881c
Compare
…instances or pool instances - the exisitng nodes pools are reused
…subscribed, consistent with the cluster implementations and the core PubSub.listen()
…g it in keyspace notifications
| """ | ||
| self.key_prefix = key_prefix | ||
| self.ignore_subscribe_messages = ignore_subscribe_messages | ||
| self._subscribed_patterns: dict[str, Any] = {} # pattern -> handler |
There was a problem hiding this comment.
Isn't it duplicate data already stored in underlying PubSub object?
There was a problem hiding this comment.
Moved those in cluster implementation - in cluster mode, there are multiple PubSub objects (one per node), and these dicts serve as the single source of truth for re-subscribing new or failed-over nodes.
For Standalone the underlying PubSub object is used to maintain that data.
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes and found 1 potential issue.
There are 2 total unresolved issues (including 1 from previous review).
Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, have a team admin enable autofix in the Cursor dashboard.

Add Redis Keyspace Notifications Support for Redis Cluster
Summary
This PR adds comprehensive support for Redis keyspace notifications in redis-py, including a cluster-aware implementation that automatically subscribes to all primary nodes and handles topology changes.
Please also take a look at the
specs/keyspace-notifications/SPEC.mdfile because it contains detailed instructions for the AI model that was leveraged for code generation purposes.Features
Core Components
EventType- Constants for common Redis keyspace notification event types (SET, DEL, EXPIRE, etc.) with IDE autocomplete support. Uses plain strings so new Redis events work without library updates.NotifyKeyspaceEvents- Enum for configuring Redisnotify-keyspace-eventssetting with common presets (ALL, KEYSPACE_ALL, KEYEVENT_ALL) and individual flags.KeyNotification- Dataclass for parsed notifications with key, event_type, database, channel, and is_keyspace fields. Includesfrom_message()class method for parsing pub/sub messages.KeyspaceChannel- Channel builder for keyspace notifications (__keyspace@<db>__:<key>). Auto-detects patterns for subscribe vs psubscribe.KeyeventChannel- Channel builder for keyevent notifications (__keyevent@<db>__:<event>). Includesall_events()factory method.ClusterKeyspaceNotifications- Cluster-aware notification manager that:notify-keyspace-eventson all nodes (default: "KEA")run_in_thread()for background processingsubscribe_keyspace()andsubscribe_keyevent()convenience methodsUsage Examples
Note
Medium Risk
Adds a new public keyspace-notifications API for both sync and asyncio clients, including cluster-wide subscription management and connection health tracking. Moderate risk due to new concurrency/topology-handling logic and an added
ConnectionInterface.is_connectedrequirement that touches multiple connection implementations and tests.Overview
Adds first-class keyspace notification support to redis-py, including a cluster-aware implementation that subscribes across all primary nodes and can refresh/recreate subscriptions when nodes change or connections break.
Introduces new sync and asyncio APIs (
Redis.keyspace_notifications()/RedisCluster.keyspace_notifications()and async equivalents) plus new public types inredis.keyspace_notificationsandredis.asyncio.keyspace_notificationsfor building channels, parsing messages intoKeyNotification, registering handlers, and running polling loops (threaded for sync, coroutine for async).Extends
ConnectionInterfacewithis_connectedand wires it through core connection classes/adapters to support detecting broken pubsub connections; adds fixtures and extensive unit/integration tests (including enablingnotify-keyspace-eventsduring tests) and updates public exports inredis/__init__.pyandredis/asyncio/__init__.py.Written by Cursor Bugbot for commit 52c613a. This will update automatically on new commits. Configure here.