This article refers to the Redis interview question

Redis 16 databases

describe

In actual projects Redis is often used for caching, distributed locking, message queuing and so on. But after setting up and configuring the Redis server, many friends should find and have such a question, why Redis has set up 16 databases by default.

Check the Redis database

When we use redis- CLI to connect to redis server, we can use the SELECT command to switch the redis library, for example, we switch to library no. 16, the specific command is as follows:

SELECT 15
Copy the code

After the command is executed, the terminal output is as follows:

We can see that the Redis prompt [15] indicates that we have switched to library 16. Now, we will switch to library 10 again with the following command:

SELECT 11
Copy the code

After the command is executed, the terminal output is as follows:

We see that we have now switched to library 11, we switch to a library that does not exist, and when we are done, the terminal output is as follows:

We see that we are prompted that the database does not exist.

The origin of 16 databases

Redis is a dictionary structured storage server. A single Redis instance provides multiple dictionaries for storing data. Clients can specify which dictionaries to store data in. This is similar to creating multiple databases in a relational database instance, so each dictionary in it can be understood as a separate database.

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

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

Redis’ database concept

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:

#Clear all database data in a Redis instanceRedis 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.

The cluster

Note that all of the above is based on single Redis. The SELECT command is not supported to switch db in cluster mode because Redis cluster mode has only one DB0. To extend some of the differences between clustered and stand-alone Reids:

  • 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.

More and more

Original link: link

Others: Contents

For more articles, you can search the public account: Hi Guest network IT tutorial