However, there is one drawback. Redis stores the data in memory which can have significant performance issues for large datasets.
Therefore, it is vital to keep track of the size of the database stored in the Redis instance and take the necessary actions.
In this article, we will learn how to determine the size of a database in Redis.
Managing and Switching Redis Databases
Redis allows you to store data in 16 logical databases. Each database corresponds to a specific index starting at index 0. Hence, the first database is at index 0, and the last database is at index 15.
Each database is isolated, and changes in one database do not affect the other databases in your instance.
By default, Redis will automatically connect to the database 0 upon login.
127.0.0.1:6379>
To switch to the database at a specific index, use the SELECT command followed by the database index you wish to access.
For example, to switch to database 10, we can do the following:
OK
127.0.0.1:6379[10]>
Note that the number of your currently selected database is indicated at the terminal prompt. This is only applicable on databases above index 0.
Redis Bulk Insert Data
You can bulk insert data using the Redis pipe mode. For example, to insert ip_addresses.txt file, use the following command:
Records in the file are in the format of:
Redis Get Database Size
To get the size of a database in Redis, use the DBSIZE command. This returns the total number of keys stored in the currently selected database.
For example:
(integer) 103
The previous command returns the number of keys in the database at index 0.
Another command you can use to get the database size is the info command. The info command returns the information about your Redis instance.
To filter for the database size, we can query the keyspace section. The following example is provided:
# Keyspace
db0:keys=103,expires=0,avg_ttl=0
The command should return the number of keys, the average time to live, and the number of expiring keys.
Conclusion
This article details how to manage and switch databases in Redis. We also cover how to insert bulk data into a Redis database and fetch the database size. We hope you found this article helpful. Check the other Linux Hint articles for more tips and information.