preface

In practical projects, Redis is often applied to cache, distributed lock, message queue and so on. However, after building and configuring the Redis server, many friends should find and have such a question, why Redis set up 16 databases by default, as shown in the figure below.

One, the origin of 16 databases

Redis is a dictionary structure storage server. A Redis instance provides multiple dictionaries for storing data. Clients can specify which dictionary to store data in. This is similar to how you can create multiple databases in a single relational database instance (as shown in the figure below), so each dictionary in it can be understood as a separate database.

Redis supports 16 databases by default. You can change the value of databases in the Redis /redis.conf configuration file.

The client will SELECT database 0 by default after establishing a connection with Redis, but you can use the SELECT command to change the database at any time.

Redis [1] > SELECT username (nil) from redis [1] > SELECT username (nil)Copy the code

In a real project, you can specify the database in the form of a Redis configuration file, as shown in the figure below!

2. Correctly understand the concept of “database” in Redis

Since Redis does not support custom database names, each database is named with a number. Developers need to keep track of the relationship between the stored data and the database. Redis also does not support different access passwords for each database, so a client can either access all or none of the databases. However, to properly understand the Redis “database” concept there is one command that has to be mentioned:

Redis 127.0.0.1:6379> FLUSHALLCopy the code

This command can clear all database data under the instance, which is different from the relational database we are familiar with. Relational databases Multiple libraries are often used to store data for different applications, and there is no way to wipe out 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 suitable 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 Redis instance occupying about 1 MB of memory internally, so you don’t have to worry about multiple Instances taking up a lot of memory.

3. In the case of cluster, is multiple DB databases supported for one instance?

Note that all of this is based on individual Redis. The select command is not supported to switch db in Redis cluster mode because there is only one DB0. To expand the differences between cluster and stand-alone Reids, interested friends can go to the relevant information for further understanding, which will not be discussed here.

1. Support for batch key operations is limited. For example, mget and mset must be in the same slot

2. Limited support for Key transactions and Lua: The Key for the operation must be on one node

3. Key is the smallest granularity of data partition: BigKey partition is not supported

4. Multiple databases are not supported: In cluster mode, there is only one DB0

5. Only one replication layer is supported: the tree replication structure is not supported

Four,

Redis instance has 16 db by default, so it is named as dbX because it does not support autonomous 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 library, but one application should correspond to one Redis instance. Different databases can be used to store data in different environments. Finally, note that the Redis cluster only supports db0, and does not support multiple DB.

The last

The following figure is a compiled Redis knowledge brain map, which can be analyzed for you.

In addition, also collate a Redis related information, Java systematic information, (including Java core knowledge points, interview topics and 20 years of the latest Internet real questions, e-books, etc.) friends in need can follow the public number [program Yuanxiaowan] can be obtained.