The origin of 16 databases
-
Redis is a dictionary structure storage server, provides a Redis instance is used to store the data dictionary, the client can specify which store the data in the dictionary, it has to do with in a relational database instance can create multiple databases are similar, so each of these dictionaries can be understood as a stand-alone database.
-
You can modify databases in the Redis/Redis. Conf file to modify this value, and restart Redis to complete the configuration.
-
The client selects database 0 by default after establishing a connection with Redis, but can change the database at any time using the SELECT command.
1redis> SELECT 1 Select * from db where id = 02OK 3redis [1] > GET username #Copy the code
-
Spring projects can be configured in configuration files
1spring.redis.database=10 Copy the code
Use redis database correctly
-
Since Redis does not support custom database names, each database is named by a number. Developers need to keep their own records of how the stored data corresponds to the database. In addition, Redis does not support different access passwords for each database, so one client can either access all databases or none of them. However, to understand Redis’s concept of “database” properly, one command must be mentioned:
1 #Clear all database data in a Redis instance2 redis 127.0.0.1:6379 > FLUSHALLCopy the code
This command can clean up all database data under the instance, which is different from the relational database we are familiar with. Multiple libraries in a relational database are often used to store data for different applications, and there is no way to flush all library data under an instance at the same time. So for Redis these DBS are more like namespaces and are not suitable for storing data from different applications. For example, database 0 can be used to store the data in the production environment of an application, and database 1 can be used to store the data in the test environment. However, it is not appropriate to use database 0 to store the data of application A and database 1 to store the data of application B. Different applications should use different Redis instances to store data. Redis is very lightweight, with an empty Instance of Redis taking up only about 1M internal memory, so there is no need to worry about multiple instances of Redis taking up a lot of extra memory.
Whether multiple dB is supported for an instance in clustering
- The select command is not supported to switch db in cluster mode because Redis cluster mode has only one DB0.
- Batch operation of keys is limited. For example, Mget and Mset must be in the same slot
- Key transactions and Lua support are limited: the Key of the operation must be on the same node
- Key is the minimum granularity of a data partition: Bigkey partitioning is not supported
- Multiple databases are not supported: there is only one DB0 in clustered mode
- Only one replication layer is supported: the tree replication structure is not supported
conclusion
- Redis instance has 16 DBS by default. It is named dbX because it does not support independent database naming. The default number of databases can be set by modifying the database value in the configuration file. The correct understanding of DB should be “namespace”, multiple applications should not use the same Redis different library, and one application should correspond to one Redis instance, different databases can be used to store the data of different environments. Finally, note that the Redis cluster only has DB0 and does not support multiple dB.