Redis

How to count total keys in Redis

Redis is a free and open-source in-memory key-value database that is built for performance and flexibility. It is cross-platform and can run on Unix/Unix-like systems and Windows.

This tutorial describes how you can view and count all the keys in a Redis database using various commands and techniques.

The example commands and illustrations have been tested on a Debian 11 server and a Redis server version 6.0. We recommend using the official Redis CLI utility to get a similar output, as shown in this guide. If you have not setup Redis yet on a Debian system you can do follow our redis on debian installation guide to get started and then come back here.

Get Number of Keys using DBSIZE command.

The first command you can use to get the total number of keys in a Redis database is the DBSIZE command.

This simple command should return the total number of keys in a selected database as an integer value.

First enter the redis-cli in order to start interacting with Redis:

$ redis-cli

The syntax for the DBSIZE command is as shown:

127.0.0.1:6379> DBSIZE
(integer) <>

For example, to get the total number of keys in the database at index 10, we can start by setting the current database to index ten as:

127.0.0.1:6379> SELECT 10
OK

Next, we can get the total number of keys in the database using the command:

127.0.0.1:6379[10]> DBSIZE
(integer) 202

The above example command shows that there are 203 keys in the database at index 10.

Get Number of Keys using a Pattern Match

Another method to get the total number of keys in a Redis database is to use the KEYS command followed by a specific pattern.

Redis will scan all the keys, searching for matches, and print them on the CLI based on the pattern you provide.

To show all the keys without matching a specific desired pattern, use an asterisk to match all keys. (*)

The syntax for the command is as:

127.0.0.1:6379> KEYS *

For example, start switching to that database to see all the keys in the database at index 10.

127.0.0.1:6379[10]> SELECT 10
OK
127.0.0.1:6379[10]> KEYS *
(Output Truncated)

The command will print all the keys in the specified database.

NOTE: The KEYS command contains one major drawback. It can lead to slow performance exceptionally when executed on an extensive database. This is because Redis will scan all the keys in the database to find a pattern. Avoid this in production environments.

Get Redis Key Info using the INFO command.

There is a way you can still view the information about the keys in a Redis database while avoiding the drawbacks of using the KEYS command.

The INFO command provided by Redis is one of the best tools to get detailed and human-readable information about the Redis cluster and the keys stored.

You can specify a specific section in the Redis, such as server, memory, stats, CPU, cluster, keyspace, modules, and more.

Check the Redis official documentation to learn more.

In our example, we are only interested in the keyspace section, which contains information about the keys in a database.

To use the command, use the command followed by the section as shown in the example below:

127.0.0.1:6379> info keyspace
# Keyspace
db0:keys=60,expires=0,avg_ttl=0

The above example returns the keyspace information in the specified database.

Information printed by the command includes the total number of keys, the total number of keys with an expiration duration, and the average time to live for the keys in the datastore.

For example, the following output shows the exact command on a database with an expiring key.

127.0.0.1:6379> info keyspace
# Keyspace
db0:keys=59,expires=1,avg_ttl=98929

The above shows key information about a database with expiring values.

Closing

This guide shows you various ways to get a total number of keys in a Redis database.

Thank you for reading!

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