Persistence top

I just announced the new Learn Spring course, focused on the fundamentals of Spring 5 and Spring Boot 2:



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.

Persistence bottom

I just announced the new Learn Spring course, focused on the fundamentals of Spring 5 and Spring Boot 2:

newest oldest most voted
Notify of
Amerigo Vespussi
Amerigo Vespussi

Can you also write a blog post of Redis clients and their differences?

Loredana Crusoveanu

That’s not currently on our list, but we might consider this in the future.

Thanks for the feedback.

Comments are closed on this article!