Home >Database >Redis >What is the database concept of Redis

What is the database concept of Redis

WBOY
WBOYforward
2023-05-28 12:13:06805browse

1. The origin of 16 databases

Redis is a dictionary-structured storage server. A Redis instance provides multiple dictionaries for storing data. The client can specify which dictionary to store the data in. This is similar to how multiple databases can be created in a relational database instance (as shown in the figure below), so each dictionary can be understood as an independent database.

What is the database concept of Redis

Take MySQL instance as an example

Redis supports 16 databases by default, which can be modified by adjusting the databases in the Redis configuration file redis/redis.conf After setting this value, restart Redis to complete the configuration.

What is the database concept of Redis

After the client establishes a connection with Redis, database No. 0 will be selected by default, but you can use the SELECT command to change the database at any time.

In actual projects, you can specify the database in the form of a Redis configuration file, as shown in the figure below

What is the database concept of Redis

2. Correctly understand Redis "Database" concept

Since Redis does not support custom database names, each database is named with a number. Developers need to record the correspondence between the stored data and the database. In addition, Redis does not support setting different access passwords for each database, so a client can either access all databases, or all databases have no permission to access. However, to correctly understand the "database" concept of Redis, we have to mention a command:

What is the database concept of Redis

This command can clear all database data under the instance, which is different from what we are talking about. It is different from the well-known relational database. Multiple libraries of relational databases are often used to store data for different applications, and there is no way to clear all library data under an instance at the same time. So for Redis, these dbs 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. Redis is very lightweight. An empty Redis instance only takes up about 1M, so you don’t have to worry about multiple Redis instances taking up a lot of extra memory.

3. Does one instance support multiple DBs in a cluster?

It should be noted that the above are all based on single Redis. In the case of a cluster, the use of the select command to switch db is not supported because there is only one db0 in Redis cluster mode. To expand on the differences between clusters and stand-alone Reids, interested friends can check out relevant information for a deeper understanding, but we will not discuss them here.

  • Key batch operation support is limited: For example, mget and mset must be in a slot

  • Key transaction and Lua support are limited: The key of the operation must be in A node

  • key is the minimum granularity of data partition: Bigkey partition is not supported

  • Does not support multiple databases: There is only one db0 in cluster mode

  • Copy only supports one level: tree copy structure is not supported

The above is the detailed content of What is the database concept of Redis. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete