Introduction to Redis Memory Usage
Redis memory usage command will report the number of bytes of memory required for its key and value to store the data in memory. This command shows the memory in bytes which was required by the specified key. Reported usage will define the total memory allocations for administrative and data overheads that were required by its key and value. Memory usage defines the total memory required for execution commands.
Redis nested data types contain the sample options that we provide, with the count defined as nested sample values of number. By default, we set this option to 5. We use samples as 0 to sample all nested values. As we all know, redis is a data store that stores data in memory. Storing data in memory will improve database performance by storing data in memory.
While storing data in memory it improves the performance, suppose data grows then it becomes very difficult to handle the same. Adding the RAM is more expensive as compared to add the disk space. Size and storage have an impact on database systems, it is defined in memory database.
- Redis memory usage is used to check the usage of memory based on the key that we defined or created in the redis server.
- We can also check the memory usage in the redis server of the specified database and all key which was present in the redis server.
How do I Check my Redis Memory Usage?
In Redis, the memory usage command will report how many bytes are required to store the key and value. The reported memory specifies the amount of memory required to store the data. To check the redis memory usage, we must first login to the redis server using the redis-cli command. The below example shows how we can connect to the redis server.
After login into the redis server now we are executing the info memory command to check the memory usage of the redis server.
While executing the info memory command it will show the details of the used memory and other all the details. We can also define the usage of memory by using the memory stats command as follows.
Redis Memory Command
In the below example, we are executing the redis memory command by using the redis-cli command. To execute the redis memory command we are using redis-cli command as follows.
# redis-cli info memory
Below is the important parameter of the redis memory usage command. It will show all the details of the redis command parameter.
- Used memory – This entry defined the total memory size allocated to the redis server. The value of this entry is specified in bytes.
- Used memory human – This entry in the redis memory command shows the used memory value in a human-readable format.
- Used memory rss – This entry shows the total number of bytes expressed by the specified operating system.
- Used memory peak – It will show the total number of bytes consumed by the server of redis.
- Used memory peak human – It is similar to the used memory peak entry, which we are defining in a human-readable format.
Instead of the above entries, there are other multiple entries defined in the redis memory command. We can also use the memory stats command in redis to find the usage of redis memory.
Redis Memory All Keys
We are using two commands to define the redis memory of all keys or specified keys. The below command shows how we can find the redis memory of all keys.
1. DB size
This command shows the total number of keys contained in a specified database. It will show all the keys present in the database. The below example shows how we can find the number of keys as follows.
# redis-cli -n 0 dbsize
By executing the above command, we can see that there are 49 keys displayed. It will display the total number of keys in the specified database with index zero. So we can say that the above DB contains a total of 50 keys.
2. Info keyspace
This command is used to show the information regarding all the keys present on the database server. It will show details like keys, their expiration, and avg_ttl.
# redis-cli info keyspace
We can also use the memory usage command to see the number of bytes consumed by the specified key as follows. In the below example, we are finding the memory usage of the redis_mem key.
SET redis_mem val MEMORY usage redis_mem
Redis Memory Analysis
Redis memory analysis will help us to analyze the redis database memory which is used to reduce memory use and improves the performance of the application.
In redis memory analysis is done using two ways:
- Online mode
- Offline mode
In online mode, redis downloads the rdb file from our connected instances and analyze the same for creating the temp file which required all the keys and metadata.
The offline mode in Redis will analyze its backup file, which is either present on the s3 bucket or not. Redis insight is accepting rdb files that were provided in order to analyze all of the information required from the files and our Redis instance. We require read-only permission to analyze the backup file that was stored in S3.
Examples of Redis Memory Usage
Below example shows redis memory usage as follows:
In the below example, we are finding the memory usage of the specified key as follows. We are creating the key name as redis_memory.
SET redis_memory val12 MEMORY usage redis_memory
In the below example, we are adding another value in the same key defined in the above example and checking the memory usage.
SET redis_memory val13 MEMORY usage redis_memory
In the below example, we are finding all the usage of all the keys from the redis server by using the following command as follows.
Given below are the FAQs mentioned:
Q1. How can we analyze the memory in redis?
Answer: We can analyze the memory by using two modes. The first mode is online mode and another mode is offline mode.
Q2. What is the use of memory usage command in redis?
Answer: The memory usage command is used to determine how much memory has been used from a specific or all keys.
Q3. What is the use of the info memory command in redis?
Answer: The info memory command is used to display all of the information about redis memory. It will display the used memory of the redis server.
Redis nested data types will contain the sample options that we are providing, with the count defined as nested sample values of number. The Redis memory usage command will report the number of bytes of memory required for its key and value to be stored in memory.
This is a guide to Redis Memory Usage. Here we discuss the introduction, redis memory command, all keys, analysis, and examples. You may also have a look at the following articles to learn more –