Note: Redis supports multiple databases, and the data of each database is isolated and cannot be shared, and is only available on a single machine. If it is a cluster, there is no concept of a database.
The reason why redis is divided into so many databases is to distinguish businesses. Different businesses are stored in different libraries. However, one redis is generally used for one project. Different businesses within the project are separated. Use a library so there will be no data crossover.
Redis is a dictionary-structured storage server. In fact, a Redis instance provides multiple dictionaries for storing data. The client can specify which dictionary to store the data in. This is similar to the well-known fact that multiple databases can be created in a relational database instance, so each dictionary can be understood as an independent database.
Each database is named externally with an increasing number starting from 0. Redis supports 16 databases by default (more can be supported through configuration files, no upper limit) , which can be configured by databases to modify this number. After the client establishes a connection with Redis, it will automatically select database No. 0, but you can use the SELECT command to change the database at any time. If you want to select database No. 1:
redis> SELECT 1 OK redis [1] > GET foo (nil)
However, these databases named with numbers are different from ours. Understanding the database is different.
First of all, Redis does not support custom database names. Each database is named after a number. Developers must record which databases store which data.
In addition, Redis does not support setting different access passwords for each database, so a client can either access all databases, or does not have permission to access even one database.
The most important point is that multiple databases are not completely isolated. For example, the FLUSHALL command can clear the data in all databases in a Redis instance.
To sum up, these databases are more like a namespace and are not suitable for storing data from different applications. For example, you can use database No. 0 to store data in the production environment of an application, and use database No. 1 to store data in the test environment. However, it is not appropriate to use database No. 0 to store the data of application A and use database No. 1 to store the data of application B. It is different. Applications should use different Redis instances to store data.
Because Redis is very lightweight, an empty Redis instance only occupies about 1M, so there is no need to worry about multiple Redis instances taking up a lot of additional memory.
The above is the detailed content of Why does redis have 16 libraries?. For more information, please follow other related articles on the PHP Chinese website!