Redis

How do I check my Redis Memory Usage

Redis is an in-memory data store. This means that it stores the data in the system’s memory. The ability to store the data in the RAM is very crucial to improve performance as it does not require a complete round trip to the disk to fetch the data.

However, storing the data in memory comes with a significant drawback. When the data size grows exponentially, more RAM size is required to handle the data. Adding more RAM as the data grows can be expensive and prone to hardware compatibility and failures.

Although storage and size affect all database systems, it is more announced in in-memory databases such as Redis or Memcached. It is good to keep in mind that Redis is not developed as the central database for massive applications.

To keep track of what’s going on in your Redis database, we need a way to monitor memory usage.

In the following subsections of the tutorial, we will learn various commands to explore memory usage in our Redis cluster.

Redis Memory Command.

Redis provides you with a CLI command to view detailed information about the cluster’s memory usage.

To run the command, open the terminal and enter:

redis-cli info memory

The command above should give an example output as shown in the screenshot below:

Let us discuss what each of the entries from the above command entails. It is good to note that we will only cover the most important ones. Check the documentation for more information.

  1. Used_memory – This entry shows the total memory size allocated to the Redis cluster. Simply put, it represents the maximum size the Redis cluster can store. The value is expressed in Bytes.
  2. Used_memory_human – This entry shows the used_memory value expressed in a human-readable format.
  3. Used_memory_rss – shows the total number of bytes expressed by the operating system.
  4. Used_memory_peak – shows the total number of bytes consumed by Redis.
  5. used_memory_peak_human – similar to the above entry in a human-readable format.

The above are some essential entries from the INFO MEMORY command. You can learn more in the Redis official documentation.

https://redis.io/commands/INFO

Redis Key Memory Info

You can use two main commands if you want to view the keys in a Redis datastore.

  1. Dbsize – The Redis dbsize command shows the total number of valid keys in a specific database.
  2. Info keyspace – This command shows the keys in each database available in the Redis cluster.

Examples:

redis-cli -n 0 dbsize
(integer) 59

The above command shows the number of valid keys in the database at index 0.

redis-cli info keyspace
# Keyspace
db0:keys=59,expires=0,avg_ttl=0

The above command provides more details about the keys.

You can use the MEMORY USAGE command to see the number of bytes consumed by a specific key and value.

The syntax is as:

MEMORY USAGE <key>

For example, the following shows the number of bytes required to store the key “AZ.”

MEMORY USAGE AZ
(integer) 57

The output shows the number of bytes occupied by the specified key and value.

Keep in mind that the above command only shows the information about a targeted key. Hence, it requires you to know which key to monitor.

Closing

In this guide, we discussed monitoring memory usage in the Redis cluster. We also covered how to view memory usage for a specific key in Redis.

Thank you for reading!

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list