Redis

How to Get the Size of Redis Database

Redis is incredibly fast, efficient, and reliable. In addition, it is a powerful tool that provides a caching mechanism for applications. This is because it stores the data in memory that does not require a round trip to the disk.

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.

# redis-cli

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:

127.0.0.1:6379> select 10

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:

cat ip_addresses.txt | redis-cli --pipe

Records in the file are in the format of:

SET key "value"

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:

127.0.0.1:6379> dbsize

(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:

127.0.0.1:6379> info keyspace

# 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.

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