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.

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.

  •  Here, we are fetching the full details of the Hash type key..

  • We have another SET type key for which we are fetching all the members/elements inside as below.

  • For a normal STRING type key, we can use a simple “GET” command.

  • In case of a LIST type key below command can be used.

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.

  • For a SET type, we can use the “SCARDcommand.

  • For a HASH, we can use the “HLEN command.

  • For a LIST, we can use the “LLEN command.

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.

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:

Using SCAN:

Moreover, to find the TOP high usage keys, we can use the native command[–bigkeys].

Output:

5) We can also verify if a KEY has any associated TTL/Expiry or not with the “TTL” command as below.

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.

OR

  • Similarly, to check the non-persisted or the keys having Expiry/TTL set, we can use the below command.

The key will represent the TTL in seconds.

6) With the help of the debugging command [DEBUG OBJECT], we can get low-level data and information about the target Keys.

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 [StringHashListSetsSorted 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!

 

Learn how

Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments