Count Keys matching pattern in redis

2,557 views
Skip to first unread message

Anjna Bhati

unread,
Jun 26, 2016, 4:45:04 AM6/26/16
to Redis DB
Hi,
I am trying to keep around 4 million keys in redis with a specific initial pattern and then want to get those keys' count from redis. Each key has an expiry so the count returns only those keys that have not expired. Using Keys and scan is too expensive an operation and slows down the application. Is there an alternative in redis for doing this without affecting the application's performance or any collection that I can use that will serve my purpose. Please guide.

Regards,
Anjna Bhati

Itamar Haber

unread,
Jun 26, 2016, 9:26:37 AM6/26/16
to Redis DB
Hi Anjna,

There are two alternatives to online ad-hoc scanning (with `SCAN` and never with `KEYS`) for key patterns.

Alternative 1 - keyspace notifications: read the doc at http://redis.io/topics/notifications and then set up a process to listen to expiration events. This will have minimal implication on your application and you can maintain any counters needed with that listener. The caveat of this approach is that events (PubSub) are delivered at most once, so the listener could potentially miss some, which would lead to counters going out of sync.

Alternative 2 - "manual" expiration: instead of using Redis' expiration, keep a Sorted Set with your keys' names and their expiration timestamp as score. You'll have to change the app slightly to stop setting the TTL and instead ZADD to that zset. Then, either with every read request or periodically (or both), check that zset to find any expired keys and deal with them as needed (i.e. update the relevant pattern counter) before removing them from the zset and DELeting the actual key.

Cheers,

--
You received this message because you are subscribed to the Google Groups "Redis DB" group.
To unsubscribe from this group and stop receiving emails from it, send an email to redis-db+u...@googlegroups.com.
To post to this group, send email to redi...@googlegroups.com.
Visit this group at https://groups.google.com/group/redis-db.
For more options, visit https://groups.google.com/d/optout.



--

Itamar Haber | Chief Developer Advocate
Redis Watch Newsletter | &&(curat||edit||janit||)
Redis Labs ~/redis

Mobile: +972 (54) 567 9692
Email: ita...@redislabs.com
Twitter: @itamarhaber
Skype: itamar.haber

Blog  |  Twitter  |  LinkedIn


Reply all
Reply to author
Forward
0 new messages