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:
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.
- 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.
- Used_memory_human – This entry shows the used_memory value expressed in a human-readable format.
- Used_memory_rss – shows the total number of bytes expressed by the operating system.
- Used_memory_peak – shows the total number of bytes consumed by Redis.
- 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.
Redis Key Memory Info
You can use two main commands if you want to view the keys in a Redis datastore.
- Dbsize – The Redis dbsize command shows the total number of valid keys in a specific database.
- Info keyspace – This command shows the keys in each database available in the Redis cluster.
The above command shows the number of valid keys in the database at index 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:
For example, the following shows the number of bytes required to store the key “AZ.”
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.
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!