Introduction
Redis is an open-source, in-memory key-value data store. Redis stores data directly in the memory rather than on disk which helps deliver unparallel speed, reliability and performance.
Storing data directly in memory helps applications reduce the latency and throughput which helps improve the performance bottleneck, especially as traffic increases or applications scale.
In this article, we will learn how to list all databases in Redis.
Redis uses a database model in which you can select a database using the SELECT command. By default, Redis has 16 databases numbered from 0 to 15.
How to list all databases in Redis?
To list all the databases in Redis, you can use the INFO command along with keyspace.
$ redis-cli INFO keyspace
This command will return the information about all the databases, the output will be like:
db0:keys=2,expires=0
db1:keys=3,expires=0
db2:keys=1,expires=0
Redis databases are usually identified by numbers, not names. And their numbers are fixed at configuration. By default, Redis typically has 16 databases, numbered from 0 to 15.
To get the number of databases configured for your Redis instance, you can use the CONFIG GET databases command.
CONFIG GET databases
This command will return the configured number of databases. For example, if it returns 16, it means you have databases 0 through 15 available.
How to list all Redis databases In python redis library?
import redis
r = redis.Redis()
dbs = r.info("keyspace")
This will return a dictionary containing the information of all the databases.
Conclusion
Please note that the number of databases in Redis can be set in the configuration file, but the default is 16, and you can switch between them using the SELECT command by specifying the database number.
