1. Introduction

In this short tutorial, we’ll take a look at different ways to list all the databases available in Redis.

2. Listing All Databases

In the first place, the number of databases in Redis is fixed. Therefore, we can extract this information from the configuration file with a simple grep command:

$ cat redis.conf | grep databases
databases 16

But what if we don’t have access to the configuration file? In this case, we can get the information we need by reading the configuration at runtime via the redis-cli:> CONFIG GET databases
1) "databases"
2) "16"

Lastly, even though it’s more suitable for low-level applications, we can use the Redis Serialization Protocol (RESP) through a telnet connection:

$ telnet 6379
Connected to
Escape character is '^]'.

3. Listing All Databases With Entries

Sometimes we’ll want to get more information about the databases that contain keys. In order to do that, we can take advantage of the Redis INFO command, used to get information and statistics about the server. Here, we specifically want to focus our attention in the keyspace section, which contains database-related data:> INFO keyspace
# Keyspace

The output lists the databases containing at least one key, along with a few statistics:

  • number of keys contained
  • number of keys with expiration
  • keys’ average time-to-live

4. Conclusion

To sum up, this article went through different ways of listing databases in Redis. As we’ve seen, there are different solutions, and which one we choose really depends on what we’re trying to achieve.

A grep is generally the best option if we have access to the config file. Otherwise, we can use the redis-cli. RESP is not usually a good choice unless we’re building an application that needs a low-level protocol. Finally, the INFO command is useful if we want to retrieve only databases that contain keys.

Course – LSD (cat=Persistence)

Get started with Spring Data JPA through the reference Learn Spring Data JPA course:

res – Persistence (eBook) (cat=Persistence)
Inline Feedbacks
View all comments
Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.