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.

As noted in the survey found that: 90% of programmers do not even know why  Redis  default 16 database!


The origin of a database of 1,16

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.

To MySQL instance as an example:


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.

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


2, the correct understanding Redis "Database" concept

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 different for each database access password , so a client can either access the entire database, or all databases do not have permission to access.

However, to properly understand the Redis " database " here have to mention the concept of a 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.

所以对于Redis来说这些db更像是一种命名空间,且不适宜存储不同应用程序的数据。比如可以使用0号数据库存储某个应用生产环境中的数据,使用1号数据库存储测试环境中的数据,但不适宜使用0号数据库存储A应用的数据而使用1号数据库B应用的数据,不同的应用应该使用不同的Redis实例存储数据

Redis非常轻量级,一个空Redis实例占用的内在只有1M左右,所以不用担心多个Redis实例会额外占用很多内存。推荐大家关注微信公众号:互联网架构师,在后台回复:8,可以获取我整理的 N 篇最新 Redis 教程,都是干货。


3、集群情况下是否支持一个实例多个db?

要注意以上所说的都是基于单体Redis的情况。而在集群的情况下不支持使用select命令来切换db,因为Redis集群模式下只有一个db0。扩展一些集群与单机Reids的区别,感兴趣的朋友可以去查阅相关的资料深入理解,这里就不做讨论了。


4、总结

Redis实例默认建立了16个db,由于不支持自主进行数据库命名所以以dbX的方式命名。默认数据库数量可以修改配置文件的database值来设定。

对于db正确的理解应为“命名空间”,多个应用程序不应使用同一个Redis不同库,而应一个应用程序对应一个Redis实例,不同的数据库可用于存储不同环境的数据。

最后要注意,Redis集群下只有db0,不支持多db。

来自:www.toutiao.com/i6752317753866060299/

推荐阅读 ↓↓↓

1.不认命,从10年流水线工人,到谷歌上班的程序媛,一位湖南妹子的励志故事

2.如何才能成为优秀的架构师?

3.从零开始搭建创业公司后台技术栈

4.“37岁,985毕业,年薪50万,被裁掉只用了10分钟”

5.37岁程序员被裁,120天没找到工作,无奈去小公司,结果懵了...

6.副业&接私活必备的 10 个开源项目!

7.你知道哪10大算法统治着全球吗?

8.15张图看懂瞎忙和高效的区别!

一个人学习、工作很迷茫?

点击「阅读原文」加入我们的小圈子!

发布了8 篇原创文章 · 获赞 17 · 访问量 6万+

Guess you like

Origin blog.csdn.net/emprere/article/details/104305969