How to discovers keys having a TTL vs those NOT having a TTL?

2,250 views
Skip to first unread message

ge...@rxmg.com

unread,
Aug 21, 2018, 8:26:06 PM8/21/18
to Redis DB
Is there any way to find keys in my Redis server that have a TTL vs those that do not?

I am thinking it would be cool if I could say 

$> redis-cli -h redis-server.tld KEYS * WITHTTL > ~/expiring-keys.log

$> redis-cli -h redis-server.tld KEYS * WITHOUTTTL > ~/permanent-keys.log

Otherwise I don't think I can know, without a KEYS * and then iterating on the result set of keys with TTL $keyname === -1, or spot checking some with SCAN, etc., or LUA.

The reason is (maybe a bad one) for my use case because we have a number of applications writing to the same Redis cluster, and some keys are permanent, and some have expiry, and I don't know which keys are expiring; in order to debug, I'd like to focus on keys that have non zero expiry versus those that are just fixtures. 

Cheers

Sripathi Krishnan

unread,
Aug 22, 2018, 2:25:41 AM8/22/18
to redi...@googlegroups.com
Using Open Source Dump Analysis
redis-rdb-tools can parse your rdb backup files and generate a csv. One of the several columns is the expiry of the key. You would use it like 
rdb --command memory /path/to/dump.rdb > keys.csv. The last column in the CSV should be the ttl / expiry.

Only caveat - the version on pip doesn't have this, but the source code is in github. So you just have to install it from source. Github has the instructions.

Using Scan Command
Here is a bash one liner to find keys and their expiry in a CSV format:

redis-cli --scan --pattern "*" | awk '{printf "echo " $1 "\r\nttl " $1 "\r\n"}' | redis-cli --csv | awk '!(NR%2){if($0 > 0) {print p "," $0}}{p=$0}'


The highlighted $0 > 0 in the awk script is for ttl > 0. Modify that to suit your needs. The script will print keys as it find them, you can kill it anytime. The script will run faster if you provide a pattern.

Tip: you can use the above template script to run all kinds of analysis. For example, replace ttl with object idletime to find keys that haven't been used in a while.

Using Commercial RDBTools
If you prefer - RDBTools GUI for Redis (https://rdbtools.com/) can run the analysis and show you a UI with keys in various expiry buckets. 
Disclaimer - I'm the founder.

--Sri


--
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.
Reply all
Reply to author
Forward
0 new messages