Abhishek Moondra
Abhishek Moondra

Reputation: 95

redis pub sub only for a certain set of keys?

I have a use case in which I want to enable notification only for a certain set of keys, so that when those keys expire I can get a notification from redis.

I have followed this answer to implement this.

I have set parameter notify-keyspace-events to "Ex"

To accomplish this I am adding keys that I want notification for in DB-0 and the other keys in DB-1. But I am recieveing notification for both the DBs. Is there any way to just get notification from a particular DB?

According to redis documentation :

"Redis can notify Pub/Sub clients about events happening in the key space. This feature is documented at http://redis.io/topics/notifications For instance if keyspace events notification is enabled, and a client performs a DEL operation on key "foo" stored in the Database 0, two messages will be published via Pub/Sub:

PUBLISH keyspace@0:foo del

PUBLISH keyevent@0:del foo "

But I am receiving notification from both DB-0 and DB-1.

PS : I know I can filter keys in my application, but I store too many expiring keys in redis and sending notification for all the expiring will increase load on my redis server.

Upvotes: 3

Views: 2533

Answers (1)

for_stack
for_stack

Reputation: 23041

I think you subscribed to a pattern that matches all DBs' notification message, e.g. PSUBSCRIBE __key*__:*.

In fact, you can specify the db index in the subscribed pattern: PSUBSCRIBE __keyspace@0__:* and PSUBSCRIBE __keyevent@0__:*. This way, you'll only received notification of DB0.

Upvotes: 1

Related Questions