Why Redis default 16 libraries? More than 90% of programmers do not know!

In a real project Redis is often used as cache, distributed lock, message queues, and so on. But in the build Redis server configured after many of my friends you should find and have this question, why Redis default established 16 databases, as shown below.

Why default 16 Redis database?

The origin of a, 16 database

Redis storage server is a dictionary structure, a plurality of Redis example provides for storing a data dictionary, the client can specify which data is stored in the dictionary. This can create multiple databases in a relationship similar to database instances (shown below), in which each can be understood to have a separate dictionary database.

Why default 16 Redis database?

For example with the MySQL instance

The default Redis supports 16 database, you can modify this value by adjusting the Redis configuration file redis / redis.conf in databases, Redis will complete restart after configuration is set up.

Why default 16 Redis database?

Client after establishing a connection with the Redis is selected by default 0 database, but you can use the SELECT command to replace the database at any time.

# 切库
redis> SELECT 1 # 默认0号db,切换为1号db
OK
redis [1] > GET username # 从1号库中获取 username
(nil)

You can specify a database, as shown below in the form of Redis profile shown in the actual project

Why default 16 Redis database?

Second, the correct understanding of "database" concept of Redis

Why default 16 Redis database?

As the name Redis does not support custom databases, each database is named after numbers. Developers will need to correspondence between the data and the database of their own record store. In addition Redis does not support setting a different password for each database access, so a client can either access the entire database, or all databases do not have permission to access. However, to properly understand Redis "Database" concept here have to mention one command:

# 清空一个Redis实例中所有数据库中的数据
redis 127.0.0.1:6379> FLUSHALL

This command can clear all database instances in the data, which we know is different relational databases. Relational database for storing a plurality of different libraries used application data, and no ways to erase all data in the repository instance at the same time. So for these Redis db is more of a name space , and is not suitable for storing data of different applications. Such data number 0 may be used an application database stores production environment, using the data stored in the database No. 1 in the test environment, but not suitable for using the data stored in a database application No. 0 A and B using the data # 1 database application, different applications should use different Redis instance store data . Redis is very lightweight, occupying an empty Redis instance intrinsic only about 1M, so do not worry multiple instances extra Redis take up a lot of memory.

Third, whether to support multiple instances of a db clustered cases?

Why default 16 Redis database?

To note that the above mentioned monomers are based on the case of Redis. And in the case of a cluster do not support the use of select command to switch db, since only a db0 under Redis cluster mode. And then expand some differences clusters and stand-alone Reids, and interested friends can go to access relevant information in-depth understanding here do not discuss it.

  • key support for bulk operations limited: for example mget, mset must be in a slot

  • Key Affairs and Lua limited support: key operation must be in a node

  • key is a minimum particle size of the data partition: the partition not supported bigkey

  • It does not support multiple database: clustered mode only a db0

  • Copy supports only one: do not support the replication tree structure

Why default 16 Redis database?

IV Summary

Redis instance of the default established 16 db, because they do not support independent database named so in a way dbX name. The default number of database configuration file can modify database values ​​to set. For correct understanding db should be "namespace", a plurality of application should not use the same library Redis different, but should be an application corresponding to a Redis example, different databases can be used to store data in different environments. Finally, note that, under Redis cluster only db0, it does not support multiple db.

Published 50 original articles · won praise 1706 · Views 2.22 million +

Guess you like

Origin blog.csdn.net/zl1zl2zl3/article/details/105238093