Leslie Zobel
Leslie Zobel

Reputation: 51

Redis keyspace notification expiration not firing

I am looking at this page: https://redis.io/topics/notifications

I have the following line set in my config file:

notify-keyspace-events "Kx"

When I do this (and then run my application that eventually expires some keys), I see no events:

redis-cli --csv psubscribe '__keyspace*__:*expire*'

However, when I set my config to this:

notify-keyspace-events "Kg"

And then run the same app and redis-cli command, I do see events:

"pmessage","__keyspace*__:*expire*","__keyspace@0__:spring:session:wca:sessions:expires:9d73fefc-459d-4bf4-83ef-b8fcbf06b997","expire"
"pmessage","__keyspace*__:*expire*","__keyspace@0__:spring:session:wca:sessions:expires:9d73fefc-459d-4bf4-83ef-b8fcbf06b997","expire"
"pmessage","__keyspace*__:*expire*","__keyspace@0__:spring:session:idp:sessions:expires:52dbe449-4616-41ef-bfa6-1d7a16a89f8a","expire"
"pmessage","__keyspace*__:*expire*","__keyspace@0__:spring:session:idp:sessions:expires:52dbe449-4616-41ef-bfa6-1d7a16a89f8a","expire"
"pmessage","__keyspace*__:*expire*","__keyspace@0__:spring:session:idp:sessions:expires:52dbe449-4616-41ef-bfa6-1d7a16a89f8a","expire"
"pmessage","__keyspace*__:*expire*","__keyspace@0__:spring:session:idp:sessions:expires:52dbe449-4616-41ef-bfa6-1d7a16a89f8a","expire"

I understand that the expirations don't necessarily happen right when the TTL has elapsed. But I'm not sure that explains what I'm seeing -- my redis-cli is looking only for "expire" events, and it consistently sees them when (and only when) I notify generic commands. That's too chatty for my app; I just want to see expires.

Any help is appreciated. Thanks!

Upvotes: 4

Views: 6364

Answers (2)

user3024119
user3024119

Reputation: 298

You need to subscribe to "__keyevent@0__:expired" rather than "__keyevent@0__:expire"

127.0.0.1:6379> subscribe __keyevent@0__:expired
Reading messages... (press Ctrl-C to quit)
1) "subscribe"
2) "__keyevent@0__:expired"
3) (integer) 1

1) "message"
2) "__keyevent@0__:expired"
3) "mykey"

Refer this doc. I was also facing same issue. I tried the solution from this link it worked https://github.com/redis/redis/issues/1855

Upvotes: 0

for_stack
for_stack

Reputation: 22906

You subscribed to the wrong channel.

There're two kinds of notification:

  • Key-space notification: the channel is __keyspace@<db>__:<key>
  • Key-event notification: the channel is __keyevent@<db>__:<event>

If you want to get all expired key notification, you have two choices:

use key-space notification

  1. enable key-space notification: config set notify-keyspace-events Kx

  2. subscribe to the channel: psubscribe __keyspace@*__:*

use key-event notification

  1. enable key-event notification: config set notify-keyspace-events Ex

  2. subscribe to the channel: psubscribe __keyevent@*__:expired

Upvotes: 9

Related Questions