You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Can redis key space notifications be pushed to the redis stream instead of pub/sub channel? #5766
We have a requirement that we need to get a notification on changes to a Redis data structure. Based on my research I found out that I can use Redis key space notifications for doing the same. However, Redis key space notifications send the events to Redis pub/sub channel which is fire and forget i.e once the clients lose the connections all the events till the connection is up again are lost.
Redis streams solve this problem. Also, I want to use consumer group feature of Redis streams. So is there any way that Redis key space notifications can be pushed to Redis streams instead of Redis pub/sub channel?
I would also like to contribute if this feature does not exist already.
Reacted by George C, Aleksandr Litvinov, Robin Cassan, Tugdual Grall, Szilard Stange, Christian Bick, Sascha Desch, Zhongqiu Zhao, KevinWu0904, Leo Antunes and 14 more
@gkorland - Well the use case is that we want to get notifications, if status of a Data structure in redis changes(in our case if something has been pushed to a sorted set). So we thought we can use key space notifications for this purpose.
But key space notification internally use pub/sub channels of Redis which are fire and forget. So in case our client disconnects we will lose some notifications. Also we want to use something like a consumer group where only one of the client gets an event if we have multiple instances for scalability purposes.
So we thought it would be nice if key space notifications pushed the events to a Redis stream instead of a pub/sub channel. As we want the channel to be reliable and scalable.
And that is why I was wondering if this is possible to do in redis. And @itamarhaber said it is not possible to do it without building a module for it.
So we are now exploring blocked pop to solve our use case. But I think it would be a nice add-on to Redis functionality.
I think we'll end having such support soon, because it is trivial and it solves a huge problem with lost messages in keyspace notifications...
Reacted by Chris Bennight, George C, Itamar Haber, Kanthi Vaidya, Aleksandr Litvinov, Roman Alekseev, Tugdual Grall, András Czigány, Patrick Wunderlin, Christian Bick and 8 more
Thanks for validating the usefulness of this. It's quite some time that I think about it. Another "internal" use case I've for streams is to publish (not by default, only if enabled) server metrics every second to some stream. Memory usage, clients, iops-per-sec, ... Of course the stream will be capped but it's simple to retain a few days without much memory usage.
Reacted by Angus Pearson, George C, Bo Wang, Filipe Oliveira (Personal), Kevon Liu, youngiif, jharishabh7, Suyog Kale, Henrique Campos, kkkiio and 1 more
Thanks for validating the usefulness of this. It's quite some time that I think about it. Another "internal" use case I've for streams is to publish (not by default, only if enabled) server metrics every second to some stream. Memory usage, clients, iops-per-sec, ... Of course the stream will be capped but it's simple to retain a few days without much memory usage.
I’m working on a serverless stream consumer dispatcher. It would be very helpful if we could listen to the key space in a reliable way. In my case, that would allow users to create arbitrary streams and got consumer groups attached to it as soon as possible.
My use case here would be to watch a stream for key expiration events then to use gears to listen to that stream and then remove that document from a redis search index and/or redis graph
Just to make you aware. Right now the blocker on this is: what to do with Cluster? Users cannot specify a single key name in the cluster, the local instance may not map with such slot for instance. Also, do we want unified or non unified events in that case (per single instance or a single key in some instance)?
So far I'm thinking about two very different solutions:
Let's specify a single key name for the cluster as well: nodes will route the events to a single master that is responsible for such key, that will do all the XADDs. So we have a single unified view.
Allow Cluster to use database "1" (SELECT 1 basically) as a local, non sharded database. Use this private database for such tasks, that is, to push local events in that case, but in the future for other stuff.
It's a big choice. There is this other possibility:
Don't support that for Cluster for now and bring the feature ASAP for the standalone Redis mode.
WRT 1 - that would potentially create a very hot master shard - smells off.
WRT 2 - a shard-local DB sounds like a good idea for a lot of things, and I prefer that approach for collecting keyspace notifications (and perhaps other types of notifications that may be upcoming). It also gives "atomicity" and as long as the shard is available, so are its events streams.
Another, perhaps half-baked, possibility:
3.5 Have the cluster connect to another, external, Redis database (single or cluster, proxy or not, HA or not, whatevs) for that purpose. In the case of a single instance, it can use 'local' and perhaps some configurable DB number.
Is this still on the table? We would like to get __keyevent@0__:expired notifications for gathering some statistics about some expired keys (expired sessions). Our first thinking was putting this into the related service. Unfortunately relying on pubsub does not play nice with multiple runnig instance of the service. A stream would mean we could use a consumer group so only one instance would get an expiration event.
add meta database in Redis, and use special db name to access meta database, for example select-1.
when a keyspace notification event triggered, add the event to a stream in meta database, it's similar with a special XADD:
key foo expired -> select -1; xadd __keyevent@0__:expired * key foo
Then we can use the streams commands to read the meta key.
for cluster, we allow using select -1 to access meta database, and accessing key in meta database doesn't need to redirect, it means different nodes could have same meta keys, just like notification channel.
The above is easy to implement, but there are still some problem I didn't figure it out(it's about replica and persistance, the key point is if the keys in meta database belong to real data or not? notify pubsub channel doesn't belong to real data) :
does the keys in meta database need to be persistance(both RDB and AOF)?
if not, the history of notification will be lost after restart and load data from disk.
if needs, the AOF file may grow too fast, since every notify XADD needs a SELECT -1 before it, and the next write command also needs a new SELECT.
and if needs, the RDB file would contain meta keys too, that will affect full resync, see below.
does the keys in meta database need to be replicated to replicas?
if not, the history of notification will be lost after failover.
if needs, there are some scenarios may lead to inconsistency:
the write commands replicated to replica would trigger notify on replicas, and then add events to replicas' notify stream, then the meta key in replica is larger than master's(to fix it, we can disable notify when the client is master).
allow or not allow XREADGROUP command on meta database in replica? since it's a write command. Or instead of X like commands, implement other commands to access meta key? maybe too complex.
to share one concern:
i feel it would be a bit hackish to use normal commands (SELECT and XRAD) on some hard coded key name.
i.e. we have some major feature of redis (keyspace notifications), implicitly creating "keys" and expecting the user to access them with normal commands and special key names. (in contrast for example to the SLOWLOG, which has its own command.
On alternative is maybe to put this in the hands of the user in some way, i.e. the user explicitly calls some command like KEYSPACE TRACK <key name>, if we do that, the user the one who redirected it to a specific key.
on the other hand, this poses some issue with cluster.
anyway, just sharing one more concern, maybe someone will be able to come up with a winning idea.
The bottom line is this gets more complex when we consider cluster environment and various scenarios, so very strong arguments about the value of this feature are needed to drive any further work.
Rationale - Why do it?
Redis already supports keyspace notifications to make it possible to track changes and operations on the server side.
There are many use cases that can benefit from this mechanism, including:
Write through cache scenarios
Change Data Capture (where Redis is the source)
Other application patterns that are based on event sourcing
The main limitation of the existing keyspace notifications mechanism is that it offers very few guarantees. Notifications are sent once to connected clients and are not stored anywhere, so a client that drops a connection may lose any arbitrary number of notifications.
Reliable KSN (RKSN)
The goal is to define a keyspace notifications mechanism which is reliable. The properties of this solution are:
Notifications are not lost, even if not immediately delivered
Clients use a client-side cursor mechanism to be able to track new notifications
Notifications are delivered in a total order to corresponds to the order of operations on the server
RKSN storage and access
The RKSN properties are identical to what Streams provide, so we considered the option of simply using stream keys, which can be accessed by clients using existing stream commands.
However, using regular stream keys creates a conflict with Redis Cluster as the key name must match the locally assigned hash slots. Redis could override that in the case of a notifications stream key, but this would also impact cluster-aware clients that would need to access the notifications stream key on all nodes regardless of hash slots topology.
Another alternative we considered was using a dedicated pseudo-database and SELECT to store those keys. However, this has been rejected as SELECT and multiple databases are not supported by Redis Cluster.
The final option was to store reliable notifications as special meta-data that lives outside the keyspace. The interface still resembles streams, but there is a dedicated (e.g., KREAD) command to access this data.
Replication and persistence
RKSN data needs to be persisted into RDB, for several reasons:
A master may restart and still need to serve past notifications to clients
A replica may perform full-sync, get promoted, and need to serve past notifications
The stream of notification includes additional meta-data, such as timestamp or stream entry ID that provide more information about the operation and support a client-side cursor mechanism.
Because of this, notifications must also be propagated locally to AOF and explicitly replicated to replicas. On the replica side, RKSN should be inhibited when processing commands on the replication link.
Redis Cluster
Slot-level ordering
In a cluster environment there is no concept of total order between operations that take place on different nodes. Because hash slots may also migrate between nodes, RKSN can effectively guarantee total ordering of notifications only for a specific hash slot.
Because of this, notifications should include an explicit hash slot identifier so clients can easily distinguish between ordered and unordered events.
RKSN Aggregation
Cluster aware clients need to read RKSN from all cluster nodes, as even for a single key there's no way to guarantee that the full RKSN history is available on a single node.
Clients will be able to aggregate, order, and if necessary de-duplicate notifications received from different nodes.
Migration
The cluster key migration mechanism needs to be enhanced to also support migration of RKSN entries. This operation does not need to be atomic as notifications may live on both source and target nodes during migration, but once a slot has been fully migrated we must guarantee the importing node has the full notification history.
Configuration
RKSN have a more significant impact on resources (memory in particular) and should have several configuration options to control that:
Limit on number of stored notifications / memory used / etc.
Notify-time filtering similar to the existing notify-keyspace-events configuration
Read-time filters in the KREAD command.
Open Issues
FLUSHALL
Should the FLUSHALL command also flush all RKSN, or do we need a dedicated command that explicitly flushes the RKSN history?
Writable Replicas
We currently assume that replicas propagate received notifications and never generate local notifications on commands received on the replication link.
Should writable replicas generate and propagate notifications for commands received from local users?
Reacted by Henrique Campos, Jorge Pereira and Aryeh Klein
@yossigo Thanks for the detailed explanation. I would like to work on this. I see few alternatives mentioned above. Is there any recommended approach already or should I put up a proposal ?
@YourTechBud Could you explain your usecase a bit more?
Currently the notification you would receive on SET operation are the below two. This doesn't have the complete data (value part) to build CDC usecase. How do you plan to tackle it ?
Activity
itamarhaber commentedon Jan 10, 2019
Hello @jharishabh7
Only modules can do this at the time being.
xref: https://stackoverflow.com/questions/54127210/can-redis-key-space-notifications-be-pushed-to-the-redis-stream-instead-of-pub-s
jharishabh7 commentedon Jan 10, 2019
Thank you @itamarhaber for the confirmation.
gkorland commentedon Jan 10, 2019
@jharishabh7 can you describe the use case? which key space notifications would you like to be pushed to streams?
jharishabh7 commentedon Jan 10, 2019
@gkorland - Well the use case is that we want to get notifications, if status of a Data structure in redis changes(in our case if something has been pushed to a sorted set). So we thought we can use key space notifications for this purpose.
But key space notification internally use pub/sub channels of Redis which are fire and forget. So in case our client disconnects we will lose some notifications. Also we want to use something like a consumer group where only one of the client gets an event if we have multiple instances for scalability purposes.
So we thought it would be nice if key space notifications pushed the events to a Redis stream instead of a pub/sub channel. As we want the channel to be reliable and scalable.
And that is why I was wondering if this is possible to do in redis. And @itamarhaber said it is not possible to do it without building a module for it.
So we are now exploring blocked pop to solve our use case. But I think it would be a nice add-on to Redis functionality.
antirez commentedon Jan 10, 2019
I think we'll end having such support soon, because it is trivial and it solves a huge problem with lost messages in keyspace notifications...
jharishabh7 commentedon Jan 10, 2019
Thank you so much @antirez . That will be awesome!
antirez commentedon Jan 10, 2019
Thanks for validating the usefulness of this. It's quite some time that I think about it. Another "internal" use case I've for streams is to publish (not by default, only if enabled) server metrics every second to some stream. Memory usage, clients, iops-per-sec, ... Of course the stream will be capped but it's simple to retain a few days without much memory usage.
jharishabh7 commentedon Jan 10, 2019
Yes, that would be awesome too. Let me know if I can contribute in any way and if you are open for receiving some PR's.
wang2bo2 commentedon Mar 19, 2019
I’m working on a serverless stream consumer dispatcher. It would be very helpful if we could listen to the key space in a reliable way. In my case, that would allow users to create arbitrary streams and got consumer groups attached to it as soon as possible.
maguec commentedon Apr 29, 2019
My use case here would be to watch a stream for key expiration events then to use gears to listen to that stream and then remove that document from a redis search index and/or redis graph
antirez commentedon May 6, 2019
Just to make you aware. Right now the blocker on this is: what to do with Cluster? Users cannot specify a single key name in the cluster, the local instance may not map with such slot for instance. Also, do we want unified or non unified events in that case (per single instance or a single key in some instance)?
So far I'm thinking about two very different solutions:
SELECT 1basically) as a local, non sharded database. Use this private database for such tasks, that is, to push local events in that case, but in the future for other stuff.It's a big choice. There is this other possibility:
itamarhaber commentedon May 6, 2019
WRT 1 - that would potentially create a very hot master shard - smells off.
WRT 2 - a shard-local DB sounds like a good idea for a lot of things, and I prefer that approach for collecting keyspace notifications (and perhaps other types of notifications that may be upcoming). It also gives "atomicity" and as long as the shard is available, so are its events streams.
Another, perhaps half-baked, possibility:
3.5 Have the cluster connect to another, external, Redis database (single or cluster, proxy or not, HA or not, whatevs) for that purpose. In the case of a single instance, it can use 'local' and perhaps some configurable DB number.
andrascz commentedon Apr 28, 2020
Is this still on the table? We would like to get
__keyevent@0__:expirednotifications for gathering some statistics about some expired keys (expired sessions). Our first thinking was putting this into the related service. Unfortunately relying on pubsub does not play nice with multiple runnig instance of the service. A stream would mean we could use a consumer group so only one instance would get an expiration event.4 remaining items
soloestoy commentedon May 11, 2021
Here is a draft design:
select-1.XADD:key
fooexpired ->select -1;xadd __keyevent@0__:expired * key fooThen we can use the streams commands to read the meta key.
select -1to access meta database, and accessing key in meta database doesn't need to redirect, it means different nodes could have same meta keys, just like notification channel.The above is easy to implement, but there are still some problem I didn't figure it out(it's about replica and persistance, the key point is if the keys in meta database belong to real data or not? notify pubsub channel doesn't belong to real data) :
XADDneeds aSELECT -1before it, and the next write command also needs a newSELECT.XREADGROUPcommand on meta database in replica? since it's a write command. Or instead ofXlike commands, implement other commands to access meta key? maybe too complex.FLUSHALLwould flush meta database or not?oranagra commentedon May 11, 2021
to share one concern:
i feel it would be a bit hackish to use normal commands (SELECT and XRAD) on some hard coded key name.
i.e. we have some major feature of redis (keyspace notifications), implicitly creating "keys" and expecting the user to access them with normal commands and special key names. (in contrast for example to the SLOWLOG, which has its own command.
On alternative is maybe to put this in the hands of the user in some way, i.e. the user explicitly calls some command like
KEYSPACE TRACK <key name>, if we do that, the user the one who redirected it to a specific key.on the other hand, this poses some issue with cluster.
anyway, just sharing one more concern, maybe someone will be able to come up with a winning idea.
yossigo commentedon Oct 14, 2021
This has been the subject of long sessions involving @oranagra @YaacovHazan @MeirShpilraien @guybe7 @itamarhaber @inbaryuval and others. I'm posting a brief summary of these discussions here for reference.
The bottom line is this gets more complex when we consider cluster environment and various scenarios, so very strong arguments about the value of this feature are needed to drive any further work.
Rationale - Why do it?
Redis already supports keyspace notifications to make it possible to track changes and operations on the server side.
There are many use cases that can benefit from this mechanism, including:
The main limitation of the existing keyspace notifications mechanism is that it offers very few guarantees. Notifications are sent once to connected clients and are not stored anywhere, so a client that drops a connection may lose any arbitrary number of notifications.
Reliable KSN (RKSN)
The goal is to define a keyspace notifications mechanism which is reliable. The properties of this solution are:
RKSN storage and access
The RKSN properties are identical to what Streams provide, so we considered the option of simply using stream keys, which can be accessed by clients using existing stream commands.
However, using regular stream keys creates a conflict with Redis Cluster as the key name must match the locally assigned hash slots. Redis could override that in the case of a notifications stream key, but this would also impact cluster-aware clients that would need to access the notifications stream key on all nodes regardless of hash slots topology.
Another alternative we considered was using a dedicated pseudo-database and
SELECTto store those keys. However, this has been rejected asSELECTand multiple databases are not supported by Redis Cluster.The final option was to store reliable notifications as special meta-data that lives outside the keyspace. The interface still resembles streams, but there is a dedicated (e.g.,
KREAD) command to access this data.Replication and persistence
RKSN data needs to be persisted into RDB, for several reasons:
The stream of notification includes additional meta-data, such as timestamp or stream entry ID that provide more information about the operation and support a client-side cursor mechanism.
Because of this, notifications must also be propagated locally to AOF and explicitly replicated to replicas. On the replica side, RKSN should be inhibited when processing commands on the replication link.
Redis Cluster
Slot-level ordering
In a cluster environment there is no concept of total order between operations that take place on different nodes. Because hash slots may also migrate between nodes, RKSN can effectively guarantee total ordering of notifications only for a specific hash slot.
Because of this, notifications should include an explicit hash slot identifier so clients can easily distinguish between ordered and unordered events.
RKSN Aggregation
Cluster aware clients need to read RKSN from all cluster nodes, as even for a single key there's no way to guarantee that the full RKSN history is available on a single node.
Clients will be able to aggregate, order, and if necessary de-duplicate notifications received from different nodes.
Migration
The cluster key migration mechanism needs to be enhanced to also support migration of RKSN entries. This operation does not need to be atomic as notifications may live on both source and target nodes during migration, but once a slot has been fully migrated we must guarantee the importing node has the full notification history.
Configuration
RKSN have a more significant impact on resources (memory in particular) and should have several configuration options to control that:
notify-keyspace-eventsconfigurationKREADcommand.Open Issues
FLUSHALL
Should the
FLUSHALLcommand also flush all RKSN, or do we need a dedicated command that explicitly flushes the RKSN history?Writable Replicas
We currently assume that replicas propagate received notifications and never generate local notifications on commands received on the replication link.
Should writable replicas generate and propagate notifications for commands received from local users?
hpatro commentedon Apr 4, 2022
@yossigo Thanks for the detailed explanation. I would like to work on this. I see few alternatives mentioned above. Is there any recommended approach already or should I put up a proposal ?
YourTechBud commentedon Feb 3, 2023
What's the current status of this? I would like to enable CDC like usecases for my current redis cluster
oranagra commentedon Feb 4, 2023
the current status is summed up in the last big comment.
there are no plans to support this in the near future.
hpatro commentedon Feb 9, 2023
@YourTechBud Could you explain your usecase a bit more?
Currently the notification you would receive on SET operation are the below two. This doesn't have the complete data (value part) to build CDC usecase. How do you plan to tackle it ?
christiancarpinelli commentedon Sep 28, 2023
any news on this?