In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)05/31 Report--
Today, I will talk to you about why Redis defaults to 16 databases, which may not be well understood by many people. in order to make you understand better, the editor has summarized the following contents for you. I hope you can get something according to this article.
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
IV. Summary
The Redis instance has 16 db by default, which is named as dbX because it does not support independent database naming. The default number of databases can be set by changing the database value of the configuration file. The correct understanding of db should be "namespace". Multiple applications should not use the same Redis different libraries, but should correspond to one Redis instance, and different databases can be used to store data from different environments. Finally, note that there is only db0 under the Redis cluster, and multiple db is not supported.
After reading the above, do you have any further understanding of why Redis defaults to 16 databases? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.