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 2 3 4 |
127.0.0.1:6379> TYPE user:123 hash 127.0.0.1:6379> TYPE members set |
2) We can verify a KEY’s specific or all the members data as below.
- Here we are fetching the specific HASH type key [user:123] “name” member information only.
1 2 |
127.0.0.1:6379> HGET user:123 name "aj" |
- Here, we are fetching the full details of the Hash type key..
1 2 3 4 5 |
127.0.0.1:6379> HGETALL user:123 1) "name" 2) "aj" 3) "email" 4) "xxx" |
- We have another SET type key for which we are fetching all the members/elements inside as below.
1 2 3 |
127.0.0.1:6379> SMEMBERS members 1) "mem1" 2) "mem2" |
- For a normal STRING type key, we can use a simple “GET” command.
1 2 |
127.0.0.1:6379> get key2 "value2" |
- In case of a LIST type key below command can be used.
1 2 3 |
127.0.0.1:6379> LRANGE lst 0 -1 1) "b" 2) "a" |
3) Similarly, to check the count of elements/members inside the Keys we can use the command as per the data type.
- For a normal String we can use the below command.
1 2 |
127.0.0.1:6379> STRLEN key1 (integer) 4 |
- For a SET type, we can use the “SCARD” command.
1 2 |
127.0.0.1:6379> SCARD members (integer) 2 |
- For a HASH, we can use the “HLEN“ command.
1 2 |
127.0.0.1:6379> HLEN user:123 (integer) 2 |
- For a LIST, we can use the “LLEN“ command.
1 2 |
127.0.0.1:6379> LLEN l_key (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 2 |
127.0.0.1:6379> MEMORY USAGE user:123 (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 2 3 4 |
valkey-cli keys "*" | while read line; do echo -n "$line: " >> memory.log valkey-cli memory usage "$line" >> memory.log done |
Using SCAN:
1 2 3 4 |
valkey-cli scan 0 | while read -r line; do echo -n "$line: " >> memory.log valkey-cli memory usage "$line" >> memory.log done |
Moreover, to find the TOP high usage keys, we can use the native command[–bigkeys].
1 |
valkey-cli --bigkeys |
Output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
[00.00%] Biggest string found so far '"key2"' with 6 bytes [00.00%] Biggest hash found so far '"user:123"' with 2 fields [00.00%] Biggest set found so far '"members"' with 2 members -------- summary ------- Sampled 4 keys in the keyspace! Total key length in bytes is 23 (avg len 5.75) Biggest hash found '"user:123"' has 2 fields Biggest string found '"key2"' has 6 bytes Biggest set found '"members"' has 2 members 0 lists with 0 items (00.00% of keys, avg size 0.00) 1 hashs with 2 fields (25.00% of keys, avg size 2.00) 2 strings with 10 bytes (50.00% of keys, avg size 5.00) 0 streams with 0 entries (00.00% of keys, avg size 0.00) 1 sets with 2 members (25.00% of keys, avg size 2.00) 0 zsets with 0 members (00.00% of keys, avg size 0.00) |
5) We can also verify if a KEY has any associated TTL/Expiry or not with the “TTL” command as below.
1 2 |
127.0.0.1:6379> TTL user:123 (integer) -1 |
Note – The command returns -1 which denotes the key exists but has no associated expiration. That means those are persisted.
- To check all those persisted keys further we can use the keys/scan command as mentioned below.
1 2 3 4 5 6 |
valkey-cli keys "*" | while read LINE; do TTL=$(valkey-cli ttl "$LINE") if [ "$TTL" -eq -1 ]; then echo "$LINE" fi done |
OR
1 2 3 4 5 6 |
valkey-cli --scan | while read -r LINE; do TTL=$(valkey-cli ttl "$LINE") if [ "$TTL" -eq -1 ]; then echo "$LINE" fi done |
- Similarly, to check the non-persisted or the keys having Expiry/TTL set, we can use the below command.
1 2 3 4 5 6 |
valkey-cli keys "*" | while read -r LINE; do TTL=$(valkey-cli ttl "$LINE") if [ "$TTL" -gt 0 ]; then echo "$LINE" fi done |
The key will represent the TTL in seconds.
1 2 |
127.0.0.1:6379> ttl m (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 2 |
127.0.0.1:6379> DEBUG OBJECT key1 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.
Summary
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!