Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Why does Redis build 16 databases by default?

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly explains why Redis establishes 16 databases by default. The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn why Redis establishes 16 databases by default.

In practical 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 questions: why does Redis set up 16 databases by default?

In practical projects, Redis is often used for caching, distributed locking, message queuing and so on. However, after setting up and configuring the Redis server, many friends should find and have questions about why Redis has set up 16 databases by default, as shown in the following figure.

I. the origin of 16 databases

Redis is a dictionary-structured storage server, and a Redis instance provides multiple dictionaries for storing data, and the client can specify which dictionary to store the data in. This is similar to creating multiple databases in a relational database instance (as shown in the following figure), so each of these dictionaries can be understood as a separate database.

Take MySQL as an example

Redis supports 16 databases by default. You can modify this value by adjusting the databases in Redis's configuration file redis/redis.conf. After setting it, restart Redis to complete the configuration.

Database 0 is selected by default after the client establishes a connection with Redis, but the database can be replaced at any time using the SELECT command.

# cut library redis > SELECT 1 # default number 0 db, switch to No. 1 db OK redis [1] > GET username # get username (nil) from Library 1

In an actual project, you can specify the database in the form of a Redis configuration file, as shown in the following figure

Second, correctly understand the concept of "database" of Redis.

Because Redis does not support custom database names, each database is named by a number. Developers need to record the corresponding relationship between the stored data and the database. In addition, Redis does not support setting a different access password for each database, so either a client can access all databases, or all databases do not have permission to access. However, to correctly understand Redis's concept of "database", one command has to be mentioned here:

# clear the data in all databases in a Redis instance redis 127.0.0.1 FLUSHALL

This command clears all database data under the instance, which is different from the relational database that we are familiar with. Multiple libraries in a relational database are often used to store data from different applications, and there is no way to empty all library data under an instance at the same time. So for Redis, these db are more like namespaces and are not suitable for storing data from different applications. For example, database 0 can be used to store data in the production environment of an application, and database 1 can be used to store data in the test environment, but it is not suitable to use database 0 to store data in application An and database B in database 1. Different applications should use different Redis instances to store data. Redis is very lightweight, and an empty Redis instance takes up only about 1m of memory, so don't worry about multiple Redis instances taking up a lot of extra memory.

3. Does it support multiple db for one instance in the case of a cluster?

Note that the above is based on a single Redis. Using the select command to switch db is not supported in the cluster case, because there is only one db0 in Redis cluster mode. Further expand some of the differences between cluster and stand-alone Reids, interested friends can consult the relevant information for in-depth understanding, there will be no discussion here.

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

Limited support for Key transactions and Lua: the key of the operation must be on one node

Key is the minimum granularity of data partitions: bigkey partitions are not supported

Multiple databases are not supported: there is only one db0 in cluster mode

Replication supports only one layer: tree replication structure is not supported

Thank you for reading, the above is the content of "Why Redis establishes 16 databases by default". After the study of this article, I believe you have a deeper understanding of why Redis establishes 16 databases by default, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report