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.
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:
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.
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:
The command should return the number of keys, the average time to live, and the number of expiring keys.
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.