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.
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.
The syntax for the DBSIZE command is as shown:
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:
Next, we can get the total number of keys in the database using the command:
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:
For example, start switching to that database to see all the keys in the database at index 10.
127.0.0.1:6379> KEYS *
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:
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.
The above shows key information about a database with expiring values.
This guide shows you various ways to get a total number of keys in a Redis database.
Thank you for reading!