In this blog post we will talk about how to get useful information out of the Valkey OR Redis keys. This would be helpful in many scenarios, like troubleshooting a specific key thing and verifying the resources, especially the “Memory” the data set is holding.
Let’s see a few bunch of operations we can perform on the KEYS to get useful insight.
1) The “TYPE” keyword helps us verify the data type of keys. The structures below are “HASH/SET” types.
|
1 |
127.0.0.1:6379> TYPE user:123<br>hash<br>127.0.0.1:6379> TYPE members<br>set<br> |
2) We can verify a KEY’s specific or all the members data as below.
|
1 |
127.0.0.1:6379> HGET user:123 name<br>"aj"<br> |
|
1 |
127.0.0.1:6379> HGETALL user:123<br>1) "name"<br>2) "aj"<br>3) "email"<br>4) "xxx" |
|
1 |
127.0.0.1:6379> SMEMBERS members<br>1) "mem1"<br>2) "mem2" |
|
1 |
127.0.0.1:6379> get key2<br>"value2"<br> |
|
1 |
127.0.0.1:6379> LRANGE lst 0 -1<br>1) "b"<br>2) "a" |
3) Similarly, to check the count of elements/members inside the Keys we can use the command as per the data type.
|
1 |
127.0.0.1:6379> STRLEN key1<br>(integer) 4 |
|
1 |
127.0.0.1:6379> SCARD members<br>(integer) 2 |
|
1 |
127.0.0.1:6379> HLEN user:123<br>(integer) 2 |
|
1 |
127.0.0.1:6379> LLEN l_key<br>(integer) 2 |
4) In some cases, we also need to check the amount of memory the KEYs hold to assess or fix the memory issues or the overall key size. To do so, we can simply use the “MEMORY USAGE” command, which denotes a value in Bytes.
|
1 |
127.0.0.1:6379> MEMORY USAGE user:123<br>(integer) 88 |
Further, to check the memory usage for matching patterns or all keys, we can use the quick snippets mentioned below, which use options like [KEYS & SCAN]. However, for Big DataSets, directly using KEYS ‘*’ could be impacted and block the workload under high operations. In those scenarios, using SCAN would be more performance-oriented and less impactful. It’s better to use SCAN in a production environment as much as possible.
Note – In case of Redis all we need to do is replace “valkey-cli” with “redis-cli” .
Using KEYS:
|
1 |
valkey-cli keys "*" | while read line; do<br> echo -n "$line: " >> memory.log<br> valkey-cli memory usage "$line" >> memory.log<br>done<br> |
Using SCAN:
|
1 |
valkey-cli scan 0 | while read -r line; do<br> echo -n "$line: " >> memory.log<br> valkey-cli memory usage "$line" >> memory.log<br>done<br> |
Moreover, to find the TOP high usage keys, we can use the native command[–bigkeys].
|
1 |
valkey-cli --bigkeys<br> |
Output:
|
1 |
[00.00%] Biggest string found so far '"key2"' with 6 bytes<br>[00.00%] Biggest hash found so far '"user:123"' with 2 fields<br>[00.00%] Biggest set found so far '"members"' with 2 members<br><br>-------- summary -------<br><br>Sampled 4 keys in the keyspace!<br>Total key length in bytes is 23 (avg len 5.75)<br><br>Biggest hash found '"user:123"' has 2 fields<br>Biggest string found '"key2"' has 6 bytes<br>Biggest set found '"members"' has 2 members<br><br>0 lists with 0 items (00.00% of keys, avg size 0.00)<br>1 hashs with 2 fields (25.00% of keys, avg size 2.00)<br>2 strings with 10 bytes (50.00% of keys, avg size 5.00)<br>0 streams with 0 entries (00.00% of keys, avg size 0.00)<br>1 sets with 2 members (25.00% of keys, avg size 2.00)<br>0 zsets with 0 members (00.00% of keys, avg size 0.00)<br> |
5) We can also verify if a KEY has any associated TTL/Expiry or not with the “TTL” command as below.
|
1 |
127.0.0.1:6379> TTL user:123<br>(integer) -1 |
Note – The command returns -1 which denotes the key exists but has no associated expiration. That means those are persisted.
|
1 |
valkey-cli keys "*" | while read LINE; do<br> TTL=$(valkey-cli ttl "$LINE")<br> if [ "$TTL" -eq -1 ]; then<br> echo "$LINE" <br> fi<br>done<br> |
OR
|
1 |
valkey-cli --scan | while read -r LINE; do<br> TTL=$(valkey-cli ttl "$LINE")<br> if [ "$TTL" -eq -1 ]; then<br> echo "$LINE"<br> fi<br>done<br> |
|
1 |
valkey-cli keys "*" | while read -r LINE; do<br> TTL=$(valkey-cli ttl "$LINE")<br> if [ "$TTL" -gt 0 ]; then<br> echo "$LINE" <br> fi<br>done<br> |
The key will represent the TTL in seconds.
|
1 |
127.0.0.1:6379> ttl m<br>(integer) 27 |
6) With the help of the debugging command [DEBUG OBJECT], we can get low-level data and information about the target Keys.
|
1 |
127.0.0.1:6379> DEBUG OBJECT key1<br>Value at:0xffffb52b99c0 refcount:1 encoding:embstr serializedlength:5 lru:4492663 lru_seconds_idle:7 |
Value at – denotes memory address.
Refcount – tells the counter of object usage.
Encoding – Type of encodings.
Serializedlength – serialized length of the object
Lru – Least Recently Used (LRU) timestamp.
Lru_seconds_idle – How many seconds the object has been idle.
So, in context to the above content, we see how these quick commands could be handy for getting most of the useful information out of the Keys. There are different structures of KEYS [String, Hash, List, Sets, Sorted Sets, etc.], although we can run the specific command/operations without much hassle. These insights are useful for both developers and backend/DBA points of view as well.
“How do I migrate from Redis to Valkey?” We are glad you asked!