In a real project Redis is often used as cache, distributed lock, message queues, and so on.
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:
# 切库
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
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?
4、总结
Redis实例默认建立了16个db,由于不支持自主进行数据库命名所以以dbX的方式命名。默认数据库数量可以修改配置文件的database值来设定。
对于db正确的理解应为“命名空间”,多个应用程序不应使用同一个Redis不同库,而应一个应用程序对应一个Redis实例,不同的数据库可用于存储不同环境的数据。
最后要注意,Redis集群下只有db0,不支持多db。
来自:www.toutiao.com/i6752317753866060299/
推荐阅读 ↓↓↓
1.不认命,从10年流水线工人,到谷歌上班的程序媛,一位湖南妹子的励志故事
4.“37岁,985毕业,年薪50万,被裁掉只用了10分钟”
5.37岁程序员被裁,120天没找到工作,无奈去小公司,结果懵了...
一个人学习、工作很迷茫?
点击「阅读原文」加入我们的小圈子!