NoSQL数据库之redis持久化存储(二)

第3章 Redis数据类型详解

 

3.1 Redis键/值介绍

Redis key值是二进制安全的,这意味着可以用任何二进制序列作为key值,从形如“foo”的简单字符串到一个JPG文件的内容都可以。空字符串也是有效key值。

关于key的几条规则:

  • 太长的键值不是个好主意,例如1024字节的键值就不是个好主意,不仅因为消耗内存,而且在数据中查找这类键值的计算成本很高。
  • 太短的键值通常也不是好主意,如果你要用“u:1000:pwd”来代替

user:1000:password,这没有什么问题,但后者更易阅读,并且由此增加的空间消耗相对于key object和value object本身来说很小。当然,没人阻止你一定要用更短的键值节省一丁点儿空间。

  • 最好坚持一种模式。例如:"object-type🆔field"就是个不错的注意,像这样“user:1000:password”。或者一个键值对的字段名中加上一个点,就想这样“comment🔢reply.to”
 
  1. 127.0.0.1:6379> set user:01:passwd 99999
  2. OK
  3. 127.0.0.1:6379> get user:01:passwd
  4. "99999"

image_1dghd02eb4vl1ln84sr2dq3qh9.png-29.7kB

 

3.2 数据类型

 

3.2.1 String字符串类型

这是Redis最简单的数据类型之一。如果只使用这种数据类型,那么redis就是一个持久化的memcached服务器(注:memcache的数据仅保存在内存中,服务器重启后,数据将丢失)。当然redis对string类型的功能比memcached还是多很多的,我们来玩儿一下字符串类型:

(1)常规的String字符串类型

 
  1. [root@redis01 scripts]# redis-cli -a yunjisuan set work ">9000"
  2. OK
  3. [root@redis01 scripts]# redis-cli -a yunjisuan get work
  4. ">9000"
  • 在redis中,我们通常用set设置一对key/value键值,然后用get来获取字符串的值。
  • value值可以是任何类型的字符串(包括二进制数据),例如你可以在一个键下保存一个jpg图片。但值的长度不能超过1GB
  • 虽然字符串是Redis的基本值类型,redis还支持更多对字符串的操作功能。

(2)String类型也可以用来存储数字,并支持对数字的加减操作。

 
  1. [root@redis01 scripts]# redis-cli -a yunjisuan set counter 1
  2. OK
  3. [root@redis01 scripts]# redis-cli -a yunjisuan incr counter #自增1
  4. (integer) 2
  5. [root@redis01 scripts]# redis-cli -a yunjisuan incr counter
  6. (integer) 3
  7. [root@redis01 scripts]# redis-cli -a yunjisuan get counter
  8. "3"
  9. [root@redis01 scripts]# redis-cli -a yunjisuan incrby counter 2 #自增指定数值
  10. (integer) 5
  11. [root@redis01 scripts]# redis-cli -a yunjisuan incrby counter 2
  12. (integer) 7
  13. [root@redis01 scripts]# redis-cli -a yunjisuan get counter
  14. "7"
  15. [root@redis01 scripts]# redis-cli -a yunjisuan decr counter #自减1
  16. (integer) 6
  17. [root@redis01 scripts]# redis-cli -a yunjisuan decr counter
  18. (integer) 5
  19. [root@redis01 scripts]# redis-cli -a yunjisuan get counter
  20. "5"
  21. [root@redis01 scripts]# redis-cli -a yunjisuan decrby counter 2 #自减指定数值
  22. (integer) 3
  23. [root@redis01 scripts]# redis-cli -a yunjisuan decrby counter 2
  24. (integer) 1
  25. [root@redis01 scripts]# redis-cli -a yunjisuan get counter
  26. "1"

INCR命令将字符串值解析成整型,将其加1,最后将结果保存为新的字符串值,类似的命令如下列表: 
image_1crp5hp031mn41n571v378b41tj8m.png-7.6kB

(3)为key设置新值并且返回原值

对字符串,另一个操作是GETSET命令,行如其名:他为key设置新值并且返回原值。这有什么用处呢?例如:你的系统每当有新用户访问时就用INCR命令操作一个Redis key。 
你希望每小时对这个信息收集一次。你就可以GETSET这个key并给其赋值0并读取原值。

 
  1. [root@redis01 scripts]# redis-cli -a yunjisuan
  2. 127.0.0.1:6379> set user01 zhangsan #设置新key-value
  3. OK
  4. 127.0.0.1:6379> get user01
  5. "zhangsan"
  6. 127.0.0.1:6379> getset user01 wangwu #设置新数据并返回旧数据
  7. "zhangsan"
  8. 127.0.0.1:6379> getset user01 liliu #设置新数据并返回旧数据
  9. "wangwu"
  10. 127.0.0.1:6379> getset user01 gongli #设置新数据并返回旧数据
  11. "liliu"
  12. 127.0.0.1:6379> get user01
  13. "gongli"

(4)String类型还支持批量读写操作

 
  1. 127.0.0.1:6379> mset name zhangsan age 44
  2. OK
  3. 127.0.0.1:6379> mget name age
  4. 1) "zhangsan"
  5. 2) "44"

(5)string类型还支持对其部分的修改和获取操作

 
  1. 127.0.0.1:6379> set images flower
  2. OK
  3. 127.0.0.1:6379> get images
  4. "flower"
  5. 127.0.0.1:6379> append images .jpg #追加字符串
  6. (integer) 10
  7. 127.0.0.1:6379> get images
  8. "flower.jpg"
  9. 127.0.0.1:6379> strlen images
  10. (integer) 10
  11. 127.0.0.1:6379> substr images 0 6
  12. "flower."
  13. 127.0.0.1:6379> substr images 0 5
  14. "flower"

命令使用帮助:

 
  1. #查看单个命令help+命令名
  2. 127.0.0.1:6379> help set
  3. SET key value [EX seconds] [PX milliseconds] [NX|XX]
  4. summary: Set the string value of a key
  5. since: 1.0.0
  6. group: string
  7. 127.0.0.1:6379> help mset
  8. MSET key value [key value ...]
  9. summary: Set multiple keys to multiple values
  10. since: 1.0.1
  11. group: string
 

3.2.2 List类型

  • 要说清楚列表数据类型,最好先讲一点理论背景,在信息技术界List这个词常常被使用不当。例如“Python Lists”就名不副实(名为Linked Lists),但它们实际上是数组(同样的数据类型在Ruby中叫数组)
  • 一般意义上讲,列表就是有序元素的序列:10,20,1,2,3就是一个列表。但用数组实现的List和用Linked List实现的List,在属性方面大不相同。
  • redis lists基于Linked Lists实现。这意味着即使在一个list中有数百万个元素,在头部或尾部添加一个元素的操作,其时间复杂度也是常数级别的。用LPUSH命令在十个元素的list头部添加新元素,和在千万元素list头部添加新元素的速度相同。
  • 那么,坏消息是什么?在数组实现的list中利用索引访问元素的速度极快,而同样的操作在linked list实现的list上没有那么快。
  • Redis Lists用linked list实现的原因是:对于数据库系统来说,至关重要的特性是:能非常快的在很大的列表上添加元素。另一个重要因素是,正如你将要看到的:Redis lists能在常数时间取得常数长度。

Redis lists入门:

LPUSH命令可向list的左边(头部)添加一个新元素,而RPUSH命令可向list的右边(尾部)添加一个新元素。最后LRANGE命令可从list中取出一定范围的元素。

Redis能够将数据存储成一个列表,并能对这个列表进行丰富的操作:

 

```4127.0.0.1:6379> lpush students "zhangsan" #将元素“zhangsan”放在students列表的最左边 
(integer) 1 
127.0.0.1:6379> lpush students "wangwu" #将元素“wangwu”插入列表的最左边 
(integer) 2 
127.0.0.1:6379> lpush students "liliu" #将元素“liliu”插入列表的最左边 
(integer) 3 
127.0.0.1:6379> lrange students 0 2 #查看序列是0到2的元素 
1) "liliu" 
2) "wangwu" 
3) "zhangsan" 
127.0.0.1:6379> rpush students "wangyue" #将元素wangyue插入列表的最右边 
(integer) 4 
127.0.0.1:6379> lrange students 0 3 #查看序列是0到3的元素 
1) "liliu" 
2) "wangwu" 
3) "zhangsan" 
4) "wangyue" 
127.0.0.1:6379> llen students #查看列表元素的个数 
(integer) 4 
127.0.0.1:6379> lpop students #移除最左边的元素值 
"liliu" 
127.0.0.1:6379> rpop students #移除最右边的元素值 
"wangyue" 
127.0.0.1:6379> lrange students 0 3 #列表里只剩下两个元素了 
1) "wangwu" 
2) "zhangsan" 
127.0.0.1:6379> rpush students zhangsan 
(integer) 3 
127.0.0.1:6379> rpush students zhangsan 
(integer) 4 
127.0.0.1:6379> lrange students 0 3 
1) "wangwu" 
2) "zhangsan" 
3) "zhangsan" 
4) "zhangsan" 
127.0.0.1:6379> lrem students 2 "zhangsan" #删除列表里是“zhangsan”的元素,删除两次(从左向右删) 
127.0.0.1:6379> lrange students 0 3 
1) "wangwu" 
2) "zhangsan" 
127.0.0.1:6379> rpush students zhangsan 
(integer) 2 
127.0.0.1:6379> rpush students zhangsan 
(integer) 3 
127.0.0.1:6379> lrem students 1 "zhangsan" #删除列表里的元素zhangsan一次 
127.0.0.1:6379> lrange students 0 3 
1) "wangwu" 
2) "zhangsan" 
3) "zhangsan" 
127.0.0.1:6379> lrem students 0 "zhangsan" #清空列表所有的zhangsan元素 
127.0.0.1:6379> lrange students 0 3 
1) "wangwu"

  1. **Redis也支持很多修改操作**
  2. <div class="md-section-divider"></div>

linsert 在列表里的某个值的前后插入元素

  1. 127.0.0.1:6379> lrange students 0 5
  2. 1) "wangwu"
  3. 127.0.0.1:6379> lpush students a b c d #左插入元素abcd
  4. (integer) 5
  5. 127.0.0.1:6379> lrange students 0 5
  6. 1) "d"
  7. 2) "c"
  8. 3) "b"
  9. 4) "a"
  10. 5) "wangwu"
  11. 127.0.0.1:6379> linsert students before b xxxx #在元素b的前边插入元素xxxx
  12. (integer) 6
  13. 127.0.0.1:6379> lrange students 0 9
  14. 1) "d"
  15. 2) "c"
  16. 3) "xxxx"
  17. 4) "b"
  18. 5) "a"
  19. 6) "wangwu"
  20. 127.0.0.1:6379> linsert students after b xxxx #在元素b的后边插入元素xxxx
  21. (integer) 7
  22. 127.0.0.1:6379> lrange students 0 9
  23. 1) "d"
  24. 2) "c"
  25. 3) "xxxx"
  26. 4) "b"
  27. 5) "xxxx"
  28. 6) "a"
  29. 7) "wangwu"
  30. <div class="md-section-divider"></div>

更多操作,请查询help @list

列表list的用途:

  • 正如你可以从上面的例子中猜到的,list可被用来实现聊天系统。还可以作为不同进程间传递消息的队列。关键是,你可以每次都以原先添加的顺序访问数据。这不需要任何SQLORDER操作,将会非常快,也会很容易扩展到百万级别的规模。
  • 例如在评级系统中,比如社会化新闻网站reddit.com,你可以把每个新提交的链接添加到一个list,用LRANGE可简单的对结果分页。
  • 在博客引擎实现中,你可为每篇日志设置一个list,在该list中推入进博客评论,等等。
  • 向Redis list压入ID而不是实际的数据。
  • 在上面的例子里,我们将“对象”(此例中是简单消息)直接压入Redis list,但通常不应这么做,由于对象可能被多次引用:例如在一个list中维护其时间顺序,在一个集合中保存它的类别,只要有必要,它还会出现在其他list中,等等。

3.2.3 集合(Sets)类型

  • Redis集合是未排序的集合,其元素是二进制安全的字符串。SADD命令可以向集合添加一个新元素。和sets相关的操作也有许多,比如检测某个元素是否存在,以及实现交集,并集,差集等等。一例胜千言:
  • Redis能够将一系列不重复的值存储成一个集合
  1. 127.0.0.1:6379> sadd users laoda #向集合users里添加一个元素“laoda”
  2. (integer) 1
  3. 127.0.0.1:6379> sadd users laoer laosan #向结合users里添加两个元素laoer,laosan
  4. (integer) 2
  5. 127.0.0.1:6379> smembers users #查看集合里的所有元素
  6. 1) "laosan" #可以看到集合里的元素是无序的
  7. 2) "laoda"
  8. 3) "laoer"
  9. <div class="md-section-divider"></div>
  10. #我们向集合中添加了三个元素,并让Redis返回所有元素。现在让我们看一下某个元素是否存在于集合中
  11. 127.0.0.1:6379> sismember users laoda #查看元素laoda是否存在于集合users中
  12. (integer) 1 #存在
  13. 127.0.0.1:6379> sismember users laoer #查看元素laoer是否存在于集合users中
  14. (integer) 1 #存在
  15. 127.0.0.1:6379> sismember users laosan #查看元素laosan是否存在于集合users中
  16. (integer) 1 #存在
  17. 127.0.0.1:6379> sismember users laosi ##查看元素laosi是否存在于集合users中
  18. (integer) 0 #不存在
  19. <div class="md-section-divider"></div>
  • “laoda”是这个集合的成员,而“laosi”不是。集合特别适合表现对象之间的关系。例如用Redis集合可以很容易实现标签功能。
  • 下面是一个简单的方案:对每个想加标签的对象,用一个标签ID集合与之关联,并且对每个已有的标签,一组对象ID与之关联。
  • 例如,假设我们的新闻ID1000被加了三个标签tag1,2,5和77,就可以设置下面两个集合:
  1. root@redis-master ~]# redis-cli -a yunjisuan sadd news:1000:tags 1
  2. (integer) 1
  3. [root@redis-master ~]# redis-cli -a yunjisuan sadd news:1000:tags 2
  4. (integer) 1
  5. [root@redis-master ~]# redis-cli -a yunjisuan sadd news:1000:tags 5
  6. (integer) 1
  7. [root@redis-master ~]# redis-cli -a yunjisuan sadd news:1000:tags 77
  8. (integer) 1
  9. [root@redis-master ~]# redis-cli -a yunjisuan sadd tag:1:objects 1000
  10. (integer) 1
  11. [root@redis-master ~]# redis-cli -a yunjisuan sadd tag:2:objects 1000
  12. (integer) 1
  13. [root@redis-master ~]# redis-cli -a yunjisuan sadd tag:5:objects 1000
  14. (integer) 1
  15. [root@redis-master ~]# redis-cli -a yunjisuan sadd tag:27:objects 1000
  16. (integer) 1
  17. <div class="md-section-divider"></div>
  18. #要获取一个对象的所有标签,我们只需要:
  19. <div class="md-section-divider"></div>
  20. #获取ID号为1000的所有新闻的题目
  21. [root@redis-master ~]# redis-cli -a yunjisuan smembers news:1000:tags #获取集合为news:1000:tags的所有元素
  22. 1) "1" #新闻标题
  23. 2) "2" #新闻标题
  24. 3) "5" #新闻标题
  25. 4) "77" #新闻标题
  26. <div class="md-section-divider"></div>
  27. #查询某个标签的具体内容,我们只需要:
  28. <div class="md-section-divider"></div>
  29. #获取某个新闻标题的具体内容
  30. [root@redis-master ~]# redis-cli -a yunjisuan smembers tag:5:objects #获取集合为tag:5:objects的所有元素
  31. 1) "1000" #新闻内容
  32. <div class="md-section-divider"></div>

而有些看上去并不简单的操作仍然能使用相应的Redis命令轻松实现。例如我们也许想获得一份同时拥有标签1,2,10和27的对象列表。则可以用SINTER命令来做,他可以在不同集合之间取出交集。因此为达目的我们只需:

[root@redis-master ~]# redis-cli -a yunjisuan sadd tag:1:objects 500 #向集合tag:1:objects里添加元素“500” 
(integer) 1 
[root@redis-master ~]# redis-cli -a yunjisuan smembers tag:1:objects #查看集合tag:1:objects里的所有元素 
1) "500" 
2) "1000" 
[root@redis-master ~]# redis-cli -a yunjisuan smembers tag:2:objects #查看集合tag:2:objects里的所有元素 
1) "1000" 
[root@redis-master ~]# redis-cli -a yunjisuan sinter tag:1:objects tag:2:objects tag:5:objects tag:27:objects #求集合tag:1:objects ...tag:27:objects里的所有元素的交集 
1) "1000"

如何为字符串获取唯一标识:

  • 在标签的例子里,我们用到了标签ID,却没有提到ID从何而来。基本上你得为每个加入系统的标签分配一个唯一标识。你也希望在多个客户端同时试着添加同样的标签时不要出现竞争的情况。此外,如果标签已经存在,你希望返回他的ID,否则创建一个新的唯一标识并将其与此标签关联。
  • Redis 1.4增加了Hash类型。有了它,字符串和唯一ID关联的事儿将不值一提,但如今我们如何用现有Redis命令可靠的解决它呢?
  • 我们首先的尝试(以失败告终)可能如下:

假设我们想为标签“redis”获取一个唯一ID:

  • 为了让算法是二进制安全的(只是标签而不考虑utf8,空格等等)我们对用户的注册名做SHA1签名。SHA1(redis)=b840fc02d524045429941cc15f59e41cb7be6c52
  • 检查这个标签是否已与一个唯一ID关联(验证用户名是否重复) 
    用命令GET tag:b840fc02d524045429941cc15f59e41cb7be6c52:id
  • 如果上面的GET操作返回一个ID,则将其返回给用户。表示用户名已经被注册了。
  • 否则...用INCR next.tag.id(自增加1)命令生成一个新的唯一ID(假定它返回123456)
  • 最后关联标签和新的ID 
    SET tag:b840fc02d524045429941cc15f59e41cb7be6c52:id 123456 
    并将新ID返回给调用者(表示注册成功)。

但是这里会出现一个问题,假如两个客户端同时使用这组指令尝试为标签“redis”获取唯一ID时会发生什么呢?如果时间凑巧,他们俩都会从GET操作获得nil,都将对next.tag.id key做自增操作,这个key会被自增两次。其中一个客户端会将错误的ID返回给调用者。

幸运的是修复这个算法并不难,这是明智的版本:

  • 为了让算法是二进制安全的(只是标签而不考虑utf8,空格等等)我们对用户的注册名做SHA1签名。SHA1(redis)=b840fc02d524045429941cc15f59e41cb7be6c52
  • 检查这个标签是否已与一个唯一ID关联(验证用户名是否重复) 
    用命令GET tag:b840fc02d524045429941cc15f59e41cb7be6c52:id
  • 如果上面的GET操作返回一个ID,则将其返回给用户。表示用户名已经被注册了。
  • 否则...用INCR next.tag.id(自增加1)命令生成一个新的唯一ID(假定它返回123456)
  • 最后关联标签和新的ID 
    SETNX tag:b840fc02d524045429941cc15f59e41cb7be6c52:id 123456 (如果另一个客户端比当前客户端更快,SETNX将不会设置key。而且,当key被成功设置时SETNX返回1,否则返回0.那么...让我们再做最后一步运算)
  • 如果SETNX返回1(key设置成功)则将123456返回给调用者,这就是我们的用户名ID,否则执行GET tag:b840fc02d524045429941cc15f59e41cb7be6c52:id 并将结果返回给调用者(表示注册成功)。

3.2.4 有序集合(Sorted Sets)类型

Sorted Sets和Sets结构相似,不同的是存在Sorted Sets中的数据会有一个score属性,并会在写入时就按这个score拍好序。

  1. <div class="md-section-divider"></div>
  2. #向一个有序集合里添加元素
  3. 127.0.0.1:6379> ZADD days 0 mon #days是有序集合名,0是序号,mon是值
  4. (integer) 1
  5. 127.0.0.1:6379> ZADD days 1 tue
  6. (integer) 1
  7. 127.0.0.1:6379> ZADD days 2 web
  8. (integer) 1
  9. 127.0.0.1:6379> ZADD days 3 thu
  10. (integer) 1
  11. 127.0.0.1:6379> ZADD days 4 fri
  12. (integer) 1
  13. 127.0.0.1:6379> ZADD days 5 sat
  14. (integer) 1
  15. 127.0.0.1:6379> ZADD days 6 sun
  16. (integer) 1
  17. 127.0.0.1:6379> zrange days 0 6 #查看集合索引0到6的元素
  18. 1) "mon"
  19. 2) "tue"
  20. 3) "web"
  21. 4) "thu"
  22. 5) "fri"
  23. 6) "sat"
  24. 7) "sun"
  25. <div class="md-section-divider"></div>
  26. #从上面我们可以看出,ZADD创建的集合是有序集合。
  27. <div class="md-section-divider"></div>
  28. #查看有序集合days的具体值的排序
  29. 127.0.0.1:6379> zscore days mon
  30. "0"
  31. 127.0.0.1:6379> zscore days web
  32. "2"
  33. 127.0.0.1:6379> zscore days fri
  34. "4"
  35. root@redis-master ~]# redis-cli -a yunjisuan
  36. 127.0.0.1:6379> zscore days mon
  37. "0"
  38. 127.0.0.1:6379> zscore days web
  39. "2"
  40. 127.0.0.1:6379> zscore days fri
  41. "4"
  42. 127.0.0.1:6379> zcount days 3 6
  43. (integer) 4
  44. 127.0.0.1:6379> ZRANGEBYSCORE days 3 6
  45. 1) "thu"
  46. 2) "fri"
  47. 3) "sat"
  48. 4) "sun"
  49. <div class="md-section-divider"></div>
  • 集合是使用频率很高的数据类型,但是...对许多问题来说他们也有点太不讲顺序了;因此Redis1.2引入了有序集合。它和集合非常相似,也是二进制安全的字符串集合,但是这次带有关联的score,以及一个类似LRANGE的操作可以返回有序元素,此操作只能作用于有序集合,它就是,ZRANGE命令。
  • 基本上有序集合从某种程度上说是SQL世界的索引在Redis中的等价物。例如在上面提到的reddit.com例子中,并没有提到如何根据用户投票和时间因素将新闻组合生成首页。我们将看到有序集合如何解决这个问题,但最好先从更简单的事情开始,阐明这个高级数据类型是如何工作的。让我们添加几个黑客,并将他们的生日作为“score”。
  1. 127.0.0.1:6379> zadd hackers 1940 "1940-Alan Kay"
  2. (integer) 1
  3. 127.0.0.1:6379> zadd hackers 1953 "1953-Richard Stallman"
  4. (integer) 1
  5. 127.0.0.1:6379> zadd hackers 1965 "1965-Yukihiro Matsumoto"
  6. (integer) 1
  7. 127.0.0.1:6379> zadd hackers 1916 "1916-Claude Shannon"
  8. (integer) 1
  9. 127.0.0.1:6379> zadd hackers 1969 "1969-Linus Torvalds"
  10. (integer) 1
  11. 127.0.0.1:6379> zadd hackers 1912 "1912-Alan Turing"
  12. (integer) 1
  13. <div class="md-section-divider"></div>

对有序集合来说,按生日排序返回这些黑客易如反掌,因为他们已经是有序的。有序集合是通过一个dual-ported数据结构实现的,它包含一个精简的有序列表和一个hash table,因此添加一个元素的时间复杂度是O(log(N))。这还行,但当我们需要访问有序的元素时,Redis不必再做任何事情,它已经是有序的了:

  1. 127.0.0.1:6379> zadd hackers 1940 "1940-Alan Kay"
  2. (integer) 1
  3. 127.0.0.1:6379> zadd hackers 1953 "1953-Richard Stallman"
  4. (integer) 1
  5. 127.0.0.1:6379> zadd hackers 1965 "1965-Yukihiro Matsumoto"
  6. (integer) 1
  7. 127.0.0.1:6379> zadd hackers 1916 "1916-Claude Shannon"
  8. (integer) 1
  9. 127.0.0.1:6379> zadd hackers 1969 "1969-Linus Torvalds"
  10. (integer) 1
  11. 127.0.0.1:6379> zadd hackers 1912 "1912-Alan Turing"
  12. (integer) 1
  13. <div class="md-section-divider"></div>
  14. #利用zrange进行排序查询
  15. 127.0.0.1:6379> zrange hackers 0 6
  16. 1) "1912-Alan Turing"
  17. 2) "1916-Claude Shannon"
  18. 3) "1940-Alan Kay"
  19. 4) "1953-Richard Stallman"
  20. 5) "1965-Yukihiro Matsumoto"
  21. 6) "1969-Linus Torvalds"
  22. <div class="md-section-divider"></div>
  23. #利用zrevrange进行反向查询
  24. 127.0.0.1:6379> zrevrange hackers 0 -1
  25. 1) "1969-Linus Torvalds"
  26. 2) "1965-Yukihiro Matsumoto"
  27. 3) "1953-Richard Stallman"
  28. 4) "1940-Alan Kay"
  29. 5) "1916-Claude Shannon"
  30. 6) "1912-Alan Turing"
  31. <div class="md-section-divider"></div>

3.2.5 Hash类型

Redis能够存储key对多个属性的数据(比如user1,uname user1.passwd)

  1. <div class="md-section-divider"></div>
  2. #存储一个hash类型test,他的属性是name,属性数据是yunjisuan
  3. 127.0.0.1:6379> hset test name yunjisuan
  4. (integer) 1
  5. <div class="md-section-divider"></div>
  6. #存储一个hash类型test,他的属性是age,属性数据是35
  7. 127.0.0.1:6379> hset test age 35
  8. (integer) 1
  9. <div class="md-section-divider"></div>
  10. #存储一个hash类型test,他的属性是sex,属性数据是non
  11. 127.0.0.1:6379> hset test sex nan
  12. (integer) 1
  13. <div class="md-section-divider"></div>
  14. #查看hash类型test的所有属性的值
  15. 127.0.0.1:6379> hvals test
  16. 1) "yunjisuan"
  17. 2) "35"
  18. 3) "nan"
  19. <div class="md-section-divider"></div>
  20. #查看hash类型test的所有属性及属性所对应的值
  21. 127.0.0.1:6379> hgetall test
  22. 1) "name"
  23. 2) "yunjisuan"
  24. 3) "age"
  25. 4) "35"
  26. 5) "sex"
  27. 6) "nan"
  28. <div class="md-section-divider"></div>

第4章 redis多实例实战

4.1 创建redis的存储目录

  1. <div class="md-section-divider"></div>
  2. #创建redis存储目录
  3. [root@redis-master redis]# cat -n /usr/local/redis/conf/redis.conf | sed -n '187p'
  4. 187 dir ./ #修改本行的存储路径配置路径
  5. [root@redis-master redis]# cat -n /usr/local/redis/conf/redis.conf | sed -n '187p'
  6. 187 dir /usr/local/redis/data/ #改成这个
  7. [root@redis-master redis]# redis-cli -a yunjisuan shutdown #关闭redis服务
  8. [root@redis-master redis]# mkdir /usr/local/redis/data #创建redis存储目录
  9. [root@redis-master redis]# redis-server /usr/local/redis/conf/redis.conf & #后台启动redis进程
  10. <div class="md-section-divider"></div>
  11. #向redis里写入数据,并保存
  12. [root@redis-master redis]# redis-cli -a yunjisuan
  13. 127.0.0.1:6379> set name2 yunjisuan
  14. OK
  15. 127.0.0.1:6379> save #保存数据
  16. [3456] 08 Oct 04:39:05.169 * DB saved on disk
  17. OK
  18. 127.0.0.1:6379> quit
  19. [root@redis-master redis]# ll /usr/local/redis/
  20. total 12
  21. drwxr-xr-x. 2 root root 4096 Oct 7 16:53 bin
  22. drwxr-xr-x. 2 root root 4096 Oct 8 04:33 conf
  23. drwxr-xr-x. 2 root root 4096 Oct 8 04:39 data
  24. [root@redis-master redis]# ll /usr/local/redis/data/
  25. total 4
  26. -rw-r--r--. 1 root root 49 Oct 8 04:39 dump.rdb #保存的rdb文件
  27. <div class="md-section-divider"></div>

4.2 创建redis多实例的存储目录及文件

  1. <div class="md-section-divider"></div>
  2. #创建redis多实例存储目录
  3. [root@redis-master redis]# mkdir -p /data/6380/data
  4. [root@redis-master redis]# mkdir -p /data/6381/data
  5. <div class="md-section-divider"></div>
  6. #创建redis多实例配置文件
  7. [root@redis-master redis]# cp /usr/local/redis/conf/redis.conf /data/6380/
  8. [root@redis-master redis]# cp /usr/local/redis/conf/redis.conf /data/6381/
  9. <div class="md-section-divider"></div>
  10. #修改多实例配置文件的数据存储路径
  11. [root@redis-master redis]# sed -n '187p' /data/6380/redis.conf
  12. dir /data/6380/data #照此修改存储路径
  13. [root@redis-master redis]# sed -n '187p' /data/6381/redis.conf
  14. dir /data/6381/data #照此修改存储路径
  15. <div class="md-section-divider"></div>
  16. #修改多实例配置文件的占用端口
  17. [root@redis-master redis]# sed -n '45p' /data/6380/redis.conf
  18. port 6380 #照此修改启动端口
  19. [root@redis-master redis]# sed -n '45p' /data/6381/redis.conf
  20. port 6381 #照此修改启动端口
  21. <div class="md-section-divider"></div>
  22. #修改多实例配置文件的pid文件位置
  23. [root@redis-master redis]# sed -n '41p' /data/6380/redis.conf
  24. pidfile /data/6380/redis.pid #照此修改
  25. [root@redis-master redis]# sed -n '41p' /data/6381/redis.conf
  26. pidfile /data/6381/redis.pid #照此修改
  27. <div class="md-section-divider"></div>
  28. #开启多实例配置文件的持久化日志
  29. [root@redis-master redis]# sed -n '449p' /data/6380/redis.conf
  30. appendonly yes #照此修改
  31. [root@redis-master redis]# sed -n '449p' /data/6381/redis.conf
  32. appendonly yes #照此修改
  33. <div class="md-section-divider"></div>

4.3 启动redis多实例进程

  1. [root@redis-master redis]# redis-server /data/6380/redis.conf &
  2. [root@redis-master redis]# redis-server /data/6381/redis.conf &
  3. <div class="md-section-divider"></div>

4.4 查看redis多实例的进程启动情况

  1. [root@redis-master redis]# netstat -antup | grep redis
  2. tcp 0 0 0.0.0.0:6379 0.0.0.0:* LISTEN 3456/redis-server *
  3. tcp 0 0 0.0.0.0:6380 0.0.0.0:* LISTEN 3493/redis-server *
  4. tcp 0 0 0.0.0.0:6381 0.0.0.0:* LISTEN 3496/redis-server *
  5. tcp 0 0 :::6379 :::* LISTEN 3456/redis-server *
  6. tcp 0 0 :::6380 :::* LISTEN 3493/redis-server *
  7. tcp 0 0 :::6381 :::* LISTEN 3496/redis-server *
  8. <div class="md-section-divider"></div>

4.5 查看多实例文件夹目录树一览

  1. [root@redis-master data]# tree /data
  2. /data
  3. ├── 6380 #redis实例6380启动目录
  4. ├── data #redis实例6380数据目录
  5. ├── appendonly.aof #redis实例6380的数据持久化日志(记录了数据库的修改,类似binlog)
  6. └── dump.rdb #redis实例6380数据存储文件
  7. └── redis.conf #redis实例6380配置文件
  8. └── 6381 #redis实例6381启动目录
  9. ├── data #redis实例6381数据目录
  10. ├── appendonly.aof #redis实例6381的数据持久化日志(记录了数据库的修改,类似binlog)
  11. └── dump.rdb #redis实例6381数据存储文件
  12. └── redis.conf #redis实例6381配置文件
  13. 4 directories, 6 files
  14. <div class="md-section-divider"></div>

或许会迷糊,appendonly.aof是做什么用的呢? 
我们打开文件的内容查看如下:

  1. [root@redis-master data]# cat /data/6380/data/appendonly.aof
  2. *2
  3. $6
  4. SELECT
  5. $1
  6. 0
  7. *3
  8. $3
  9. set
  10. $4
  11. name
  12. $9
  13. yunjisuan
  14. *1
  15. $4
  16. save
  17. <div class="md-section-divider"></div>

我们发现appendonly.aof实际上里面记录的是我们对redis数据库的修改记录,这点类似于MySQL的binlog日志。

第5章 Redis主从同步

5.1 Redis主从同步特点

  • 一个master可以拥有多个slave
  • 多个slave可以连接同一个master,还可以连接到其他slave
  • 主从复制不会阻塞master,在同步数据时,master可以继续处理client请求。
  • 提高系统的伸缩性

5.2 Redis主从同步的过程

  1. 配置好slave服务器连接master后,slave会建立和master的连接,然后发送sync命令。
  2. 无论是第一次同步建立的连接还是连接断开后的重新连接,master都会启动一个后台进程,将数据库快照保存到磁盘文件中,同时master主进程会开始收集新的写命令并缓存起来。
  3. 当后台进程完成写磁盘文件后,master就将快照文件发送给slave,slave将文件保存到磁盘上,然后加载到内存将数据库快照恢复到slave上。
  4. slave完成快照文件的恢复后,master就会把缓存的命令都转发给slave,slave更新内存数据库。
  5. 后续master收到的写命令都会通过开始建立的连接发送给slave。从master到slave的同步数据的命令和从client到master发送的命令使用相同的协议格式。当master和slave的连接断开时,slave可以自动重新建立连接。如果master同时收到多个slave发来的同步连接命令,只会使用启动一个进程来写数据库镜像,然后发送给所有slave。 
    image_1crp7iaic18pdnp1v6iou96vc1j.png-30.1kB

(1)Slave服务器连接到Master 
服务器 
(2)Slave服务器发送SYNC命令 
(3)Master服务器备份数据库到.rdb文件 
(4)Master服务器把.rdb文件传输给Slave服务器 
(5)Slave服务器把.rdb文件数据导入到数据库中。

上面的这5步是同步的第一阶段,接下来在Master服务器上调用每一个命令都使用replicationFeedSlaves()来同步到Slave服务器。

Redis的主从同步具有明显的分布式缓存特点:

(1)一个master可以有多个slave,一个slave下面还可以有多个slave 
(2)slave不仅可以连接到master,slave也可以连接其他slave形成树状。 
(3)主从同步不会阻塞master,但是会阻塞slave。也就是说当一个或多个slave与master进行初次同步数据时,master可以继续处理client发来的请求。相反slave在初次同步数据时则会阻塞不能处理client的请求。 
(4)主从同步可以同来提高系统的可伸缩性,我们可以用多个slave专门处理client端的读请求,也可以用来做简单的数据冗余或者只在slave上进行持久化从而提升集群的整体性能。 
(5)对于老版本的redis,每次重连都会重新发送所有数据。

5.3 Redis主动同步设置方法

有两种方式可以用来完成进行主从Redis服务器的同步设置。都需要在slave服务器上进行,指定slave需要连接的Redis服务器(可能是master,也可能是slave)。

5.3.1 在redis.conf配置文件中设置

通过简单的配置slave(master端无需配置),用户就能使用redis的主从复制 
我们让端口6379的redis做master;端口6380的redis做slave

  1. <div class="md-section-divider"></div>
  2. #我们修改/data/6380/redis.conf的配置文件
  3. [root@redis-master ~]# cat -n /data/6380/redis.conf | sed -n '189,215p'
  4. 189 ################################# REPLICATION #################################
  5. 190
  6. 191 # Master-Slave replication. Use slaveof to make a Redis instance a copy of
  7. 192 # another Redis server. Note that the configuration is local to the slave
  8. 193 # so for example it is possible to configure the slave to save the DB with a
  9. 194 # different interval, or to listen to another port, and so on.
  10. 195 #
  11. 196 # slaveof <masterip> <masterport>
  12. 197 slaveof 192.168.0.135 6379 在此处添加本行内容,指定主master的IP和端口
  13. 198 # If the master is password protected (using the "requirepass" configuration
  14. 199 # directive below) it is possible to tell the slave to authenticate before
  15. 200 # starting the replication synchronization process, otherwise the master will
  16. 201 # refuse the slave request.
  17. 202 #
  18. 203 # masterauth <master-password>
  19. 204 masterauth yunjisuan 在此处添加本行内容,指定验证的密码
  20. 205 # When a slave loses its connection with the master, or when the replication
  21. 206 # is still in progress, the slave can act in two different ways:
  22. 207 #
  23. 208 # 1) if slave-serve-stale-data is set to 'yes' (the default) the slave will
  24. 209 # still reply to client requests, possibly with out of date data, or the
  25. 210 # data set may just be empty if this is the first synchronization.
  26. 211 #
  27. 212 # 2) if slave-serve-stale-data is set to 'no' the slave will reply with
  28. 213 # an error "SYNC with master in progress" to all the kind of commands
  29. 214 # but to INFO and SLAVEOF.
  30. 215 #
  31. <div class="md-section-divider"></div>

接下来我们重启redis的服务进程

  1. [root@redis-master ~]# redis-cli -p 6380 -a yunjisuan shutdown #关闭6380redis进程
  2. [3558] 08 Oct 09:03:10.218 # User requested shutdown...
  3. [3558] 08 Oct 09:03:10.218 * Calling fsync() on the AOF file.
  4. [3558] 08 Oct 09:03:10.218 * Saving the final RDB snapshot before exiting.
  5. [3558] 08 Oct 09:03:10.220 * DB saved on disk
  6. [3558] 08 Oct 09:03:10.220 # Redis is now ready to exit, bye bye...
  7. [3]+ Done redis-server /data/6380/redis.conf (wd: /data)
  8. (wd now: ~)
  9. [root@redis-master ~]# redis-server /data/6380/redis.conf & #后台启动
  10. <div class="md-section-divider"></div>

当再次启动从库时出现如下信息:

  1. [3616] 08 Oct 09:07:50.955 # Server started, Redis version 2.8.9
  2. [3616] 08 Oct 09:07:50.965 * DB saved on disk
  3. [3616] 08 Oct 09:07:50.965 * DB loaded from append only file: 0.010 seconds
  4. [3616] 08 Oct 09:07:50.965 * The server is now ready to accept connections on port 6380
  5. [3616] 08 Oct 09:07:51.958 * Connecting to MASTER 192.168.0.135:6379 #连接master
  6. [3616] 08 Oct 09:07:51.958 * MASTER <-> SLAVE sync started #开始发送sync
  7. [3616] 08 Oct 09:07:51.958 * Non blocking connect for SYNC fired the event. #这是一个不阻塞事件
  8. [3616] 08 Oct 09:07:51.958 * Master replied to PING, replication can continue... #master应答了ping,同步开始
  9. [3616] 08 Oct 09:07:51.959 * Partial resynchronization not possible (no cached master) #重新进行同步不可能(master没有缓存内容)
  10. [3616] 08 Oct 09:07:51.961 * Full resync from master: #从master同步全部数据 933d3b0123f2d72cf106d901434898aab24d2a6e:1
  11. [3616] 08 Oct 09:07:52.052 * MASTER <-> SLAVE sync: receiving 49 bytes from master #从master接收到49字节数据
  12. [3616] 08 Oct 09:07:52.052 * MASTER <-> SLAVE sync: Flushing old data #刷新旧数据
  13. [3616] 08 Oct 09:07:52.053 * MASTER <-> SLAVE sync: Loading DB in memory #数据放到内存
  14. [3616] 08 Oct 09:07:52.053 * MASTER <-> SLAVE sync: Finished with success #同步完成
  15. [3616] 08 Oct 09:07:52.054 * Background append only file rewriting started by pid 3620 #AOF重写
  16. [3620] 08 Oct 09:07:52.060 * SYNC append only file rewrite performed
  17. [3620] 08 Oct 09:07:52.060 * AOF rewrite: 6 MB of memory used by copy-on-write
  18. [3616] 08 Oct 09:07:52.159 * Background AOF rewrite terminated with success #AOF重写成功
  19. [3616] 08 Oct 09:07:52.159 * Parent diff successfully flushed to the rewritten AOF (0 bytes)
  20. [3616] 08 Oct 09:07:52.159 * Background AOF rewrite finished successfully #AOF重写完毕
  21. <div class="md-section-divider"></div>

5.3.2 进行redis主从同步测试

  1. [root@redis-master ~]# redis-cli -a yunjisuan -p 6380 get name #获取redis6380的键name的值
  2. "benet"
  3. [root@redis-master ~]# redis-cli -a yunjisuan -p 6379 set name xxxxx #向redis6379里存一个key=name,value=xxxxx的数据
  4. OK
  5. [root@redis-master ~]# redis-cli -a yunjisuan -p 6380 get name #获取redis6380的键name的值
  6. "xxxxx"
  7. <div class="md-section-divider"></div>

综上所示:redis主从同步成功

5.3.3 redis主从同步相关配置参数解释

  1. [root@redis-master ~]# cat -n /data/6380/redis.conf | sed -n "189,324p"
  2. 189 ################################# REPLICATION #################################
  3. 190
  4. 191 # Master-Slave replication. Use slaveof to make a Redis instance a copy of
  5. 192 # another Redis server. Note that the configuration is local to the slave
  6. 193 # so for example it is possible to configure the slave to save the DB with a
  7. 194 # different interval, or to listen to another port, and so on.
  8. 195 #
  9. 196 # slaveof <masterip> <masterport>
  10. 197 slaveof 192.168.0.135 6379 用于标识master的连接IP及端口号
  11. 198 # If the master is password protected (using the "requirepass" configuration
  12. 199 # directive below) it is possible to tell the slave to authenticate before
  13. 200 # starting the replication synchronization process, otherwise the master will
  14. 201 # refuse the slave request.
  15. 202 #
  16. 203 # masterauth <master-password>
  17. 204 masterauth yunjisuan 如果master设置了连接密码,这里要写上
  18. 205 # When a slave loses its connection with the master, or when the replication
  19. 206 # is still in progress, the slave can act in two different ways:
  20. 207 #
  21. 208 # 1) if slave-serve-stale-data is set to 'yes' (the default) the slave will
  22. 209 # still reply to client requests, possibly with out of date data, or the
  23. 210 # data set may just be empty if this is the first synchronization.
  24. 211 #
  25. 212 # 2) if slave-serve-stale-data is set to 'no' the slave will reply with
  26. 213 # an error "SYNC with master in progress" to all the kind of commands
  27. 214 # but to INFO and SLAVEOF.
  28. 215 #
  29. 216 slave-serve-stale-data yes 如果设置yes,那么一旦从库连接不上主库,从库继续响应客户端发来的请求并回复,但是回复的内容有可能是过期的。如果no,那么slave会应答一个错误提示,就不提供访问了。
  30. 217
  31. 218 # You can configure a slave instance to accept writes or not. Writing against
  32. 219 # a slave instance may be useful to store some ephemeral data (because data
  33. 220 # written on a slave will be easily deleted after resync with the master) but
  34. 221 # may also cause problems if clients are writing to it because of a
  35. 222 # misconfiguration.
  36. 223 #
  37. 224 # Since Redis 2.6 by default slaves are read-only.
  38. 225 #
  39. 226 # Note: read only slaves are not designed to be exposed to untrusted clients
  40. 227 # on the internet. It's just a protection layer against misuse of the instance.
  41. 228 # Still a read only slave exports by default all the administrative commands
  42. 229 # such as CONFIG, DEBUG, and so forth. To a limited extent you can improve
  43. 230 # security of read only slaves using 'rename-command' to shadow all the
  44. 231 # administrative / dangerous commands.
  45. 232 slave-read-only yes yes:从库被设置为只能读
  46. 233
  47. 234 # Slaves send PINGs to server in a predefined interval. It's possible to change
  48. 235 # this interval with the repl_ping_slave_period option. The default value is 10
  49. 236 # seconds.
  50. 237 #
  51. 238 # repl-ping-slave-period 10
  52. 239
  53. 240 # The following option sets the replication timeout for:
  54. 241 #
  55. 242 # 1) Bulk transfer I/O during SYNC, from the point of view of slave.
  56. 243 # 2) Master timeout from the point of view of slaves (data, pings).
  57. 244 # 3) Slave timeout from the point of view of masters (REPLCONF ACK pings).
  58. 245 #
  59. 246 # It is important to make sure that this value is greater than the value
  60. 247 # specified for repl-ping-slave-period otherwise a timeout will be detected
  61. 248 # every time there is low traffic between the master and the slave.
  62. 249 #
  63. 250 # repl-timeout 60
  64. 251
  65. 252 # Disable TCP_NODELAY on the slave socket after SYNC?
  66. 253 #
  67. 254 # If you select "yes" Redis will use a smaller number of TCP packets and
  68. 255 # less bandwidth to send data to slaves. But this can add a delay for
  69. 256 # the data to appear on the slave side, up to 40 milliseconds with
  70. 257 # Linux kernels using a default configuration.
  71. 258 #
  72. 259 # If you select "no" the delay for data to appear on the slave side will
  73. 260 # be reduced but more bandwidth will be used for replication.
  74. 261 #
  75. 262 # By default we optimize for low latency, but in very high traffic conditions
  76. 263 # or when the master and slaves are many hops away, turning this to "yes" may
  77. 264 # be a good idea.
  78. 265 repl-disable-tcp-nodelay no
  79. 266
  80. 267 # Set the replication backlog size. The backlog is a buffer that accumulates
  81. 268 # slave data when slaves are disconnected for some time, so that when a slave
  82. 269 # wants to reconnect again, often a full resync is not needed, but a partial
  83. 270 # resync is enough, just passing the portion of data the slave missed while
  84. 271 # disconnected.
  85. 272 #
  86. 273 # The biggest the replication backlog, the longer the time the slave can be
  87. 274 # disconnected and later be able to perform a partial resynchronization.
  88. 275 #
  89. 276 # The backlog is only allocated once there is at least a slave connected.
  90. 277 #
  91. 278 \# repl-backlog-size 1mb 用于同步的backlog大小,用于从库增量同步
  92. 279
  93. 280 # After a master has no longer connected slaves for some time, the backlog
  94. 281 # will be freed. The following option configures the amount of seconds that
  95. 282 # need to elapse, starting from the time the last slave disconnected, for
  96. 283 # the backlog buffer to be freed.
  97. 284 #
  98. 285 # A value of 0 means to never release the backlog.
  99. 286 #
  100. 287 \# repl-backlog-ttl 3600 当主从连接断开,backlog的生存周期
  101. 288
  102. 289 # The slave priority is an integer number published by Redis in the INFO output.
  103. 290 # It is used by Redis Sentinel in order to select a slave to promote into a
  104. 291 # master if the master is no longer working correctly.
  105. 292 #
  106. 293 # A slave with a low priority number is considered better for promotion, so
  107. 294 # for instance if there are three slaves with priority 10, 100, 25 Sentinel will
  108. 295 # pick the one with priority 10, that is the lowest.
  109. 296 #
  110. 297 # However a special priority of 0 marks the slave as not able to perform the
  111. 298 # role of master, so a slave with priority of 0 will never be selected by
  112. 299 # Redis Sentinel for promotion.
  113. 300 #
  114. 301 # By default the priority is 100.
  115. 302 slave-priority 100 slave的优先级
  116. 303
  117. 304 # It is possible for a master to stop accepting writes if there are less than
  118. 305 # N slaves connected, having a lag less or equal than M seconds.
  119. 306 #
  120. 307 # The N slaves need to be in "online" state.
  121. 308 #
  122. 309 # The lag in seconds, that must be <= the specified value, is calculated from
  123. 310 # the last ping received from the slave, that is usually sent every second.
  124. 311 #
  125. 312 # This option does not GUARANTEES that N replicas will accept the write, but
  126. 313 # will limit the window of exposure for lost writes in case not enough slaves
  127. 314 # are available, to the specified number of seconds.
  128. 315 #
  129. 316 # For example to require at least 3 slaves with a lag <= 10 seconds use:
  130. 317 #
  131. 318 # min-slaves-to-write 3
  132. 319 # min-slaves-max-lag 10
  133. 320 #
  134. 321 # Setting one or the other to 0 disables the feature.
  135. 322 #
  136. 323 # By default min-slaves-to-write is set to 0 (feature disabled) and
  137. 324 # min-slaves-max-lag is set to 10.
  138. <div class="md-section-divider"></div>

5.4 查看redis各项参数的方法

  1. <div class="md-section-divider"></div>
  2. #我们登陆redis-master
  3. [root@redis-master ~]# redis-cli -a yunjisuan -p 6379 #登陆
  4. 127.0.0.1:6379> info #查看各项信息
  5. <div class="md-section-divider"></div>
  6. # Server
  7. redis_version:2.8.9
  8. redis_git_sha1:00000000
  9. redis_git_dirty:0
  10. redis_build_id:125c8b01feaf5fd0
  11. redis_mode:standalone
  12. os:Linux 2.6.32-431.el6.x86_64 x86_64
  13. arch_bits:64
  14. multiplexing_api:epoll
  15. gcc_version:4.4.7
  16. process_id:3456
  17. run_id:933d3b0123f2d72cf106d901434898aab24d2a6e
  18. tcp_port:6379
  19. uptime_in_seconds:23790
  20. uptime_in_days:0
  21. hz:10
  22. lru_clock:14303250
  23. config_file:/usr/local/redis/conf/redis.conf
  24. <div class="md-section-divider"></div>
  25. # Clients
  26. connected_clients:1
  27. client_longest_output_list:0
  28. client_biggest_input_buf:0
  29. blocked_clients:0
  30. <div class="md-section-divider"></div>
  31. # Memory
  32. used_memory:1879144
  33. used_memory_human:1.79M
  34. used_memory_rss:10010624
  35. used_memory_peak:1915072
  36. used_memory_peak_human:1.83M
  37. used_memory_lua:33792
  38. mem_fragmentation_ratio:5.33
  39. mem_allocator:jemalloc-3.2.0
  40. <div class="md-section-divider"></div>
  41. # Persistence
  42. loading:0
  43. rdb_changes_since_last_save:0
  44. rdb_bgsave_in_progress:0
  45. rdb_last_save_time:1507468973
  46. rdb_last_bgsave_status:ok
  47. rdb_last_bgsave_time_sec:0
  48. rdb_current_bgsave_time_sec:-1
  49. aof_enabled:0
  50. aof_rewrite_in_progress:0
  51. aof_rewrite_scheduled:0
  52. aof_last_rewrite_time_sec:-1
  53. aof_current_rewrite_time_sec:-1
  54. aof_last_bgrewrite_status:ok
  55. aof_last_write_status:ok
  56. <div class="md-section-divider"></div>
  57. # Stats
  58. total_connections_received:5
  59. total_commands_processed:7362
  60. instantaneous_ops_per_sec:1
  61. rejected_connections:0
  62. sync_full:1
  63. sync_partial_ok:0
  64. sync_partial_err:0
  65. expired_keys:0
  66. evicted_keys:0
  67. keyspace_hits:0
  68. keyspace_misses:0
  69. pubsub_channels:0
  70. pubsub_patterns:0
  71. latest_fork_usec:242
  72. <div class="md-section-divider"></div>
  73. # Replication
  74. role:master
  75. connected_slaves:1
  76. slave0:ip=192.168.0.135,port=6380,state=online,offset=10348,lag=1
  77. master_repl_offset:10348
  78. repl_backlog_active:1
  79. repl_backlog_size:1048576
  80. repl_backlog_first_byte_offset:2
  81. repl_backlog_histlen:10347
  82. <div class="md-section-divider"></div>
  83. # CPU
  84. used_cpu_sys:9.79
  85. used_cpu_user:7.03
  86. used_cpu_sys_children:0.01
  87. used_cpu_user_children:0.00
  88. <div class="md-section-divider"></div>
  89. # Keyspace
  90. db0:keys=2,expires=0,avg_ttl=0
  91. <div class="md-section-divider"></div>

如果我们只想单独查看某些信息,那么操作如下:

  1. 127.0.0.1:6379> info cpu #查看CPU信息
  2. <div class="md-section-divider"></div>
  3. # CPU
  4. used_cpu_sys:10.11
  5. used_cpu_user:7.46
  6. used_cpu_sys_children:0.01
  7. used_cpu_user_children:0.00
  8. 127.0.0.1:6379> info memory #查看内存信息
  9. <div class="md-section-divider"></div>
  10. # Memory
  11. used_memory:1878304
  12. used_memory_human:1.79M
  13. used_memory_rss:10027008
  14. used_memory_peak:1915072
  15. used_memory_peak_human:1.83M
  16. used_memory_lua:33792
  17. mem_fragmentation_ratio:5.34
  18. mem_allocator:jemalloc-3.2.0
  19. 127.0.0.1:6379> info clients #查看客户端信息
  20. <div class="md-section-divider"></div>
  21. # Clients
  22. connected_clients:1
  23. client_longest_output_list:0
  24. client_biggest_input_buf:0
  25. blocked_clients:0
  26. 127.0.0.1:6379> info replication #查看同步信息
  27. <div class="md-section-divider"></div>
  28. # Replication
  29. role:master #本redis是主
  30. connected_slaves:1
  31. slave0:ip=192.168.0.135,port=6380,state=online,offset=11972,lag=1 #主库ip,端口,状态,偏移量等
  32. master_repl_offset:11972
  33. repl_backlog_active:1
  34. repl_backlog_size:1048576
  35. repl_backlog_first_byte_offset:2
  36. repl_backlog_histlen:11971
  37. <div class="md-section-divider"></div>

第六章 redis的高级特性

6.1 redis数据过期设置及过期机制

Redis支持按key设置过期时间,过期后值将被删除(在客户端看来是被删除了的) 
用TTL命令可以获取某个key值的过期时间(-1表示永久不过期)

  1. 127.0.0.1:6379> keys *
  2. 1) "name"
  3. 2) "name2"
  4. 127.0.0.1:6379> TTL name 查看key过期时间(-1为永久不过期,-2为已经过期)
  5. (integer) -1
  6. 127.0.0.1:6379> TTL name2
  7. (integer) -1
  8. <div class="md-section-divider"></div>

下面通过命令先用EXISTS查看key值是否存在,然后设置5秒过期时间

  1. 127.0.0.1:6379> expire name 5 #给key name设置5秒过期时间
  2. (integer) 1
  3. 127.0.0.1:6379> TTL name #查看key过期时间
  4. (integer) 3 #3秒后过期
  5. 127.0.0.1:6379> TTL name
  6. (integer) 2 #2秒后过期
  7. 127.0.0.1:6379> TTL name
  8. (integer) 1 #1秒后过期
  9. 127.0.0.1:6379> TTL name
  10. (integer) -2 #key已经过期
  11. 127.0.0.1:6379> TTL name
  12. (integer) -2
  13. 127.0.0.1:6379> get name #过期了的key是无法通过key获取value的
  14. (nil)
  15. <div class="md-section-divider"></div>

6.2 redis持久化

  • Redis的所有数据都存储在内存中,但是他也提供对这些数据的持久化。
  • redis是一个支持持久化的内存数据库,也就是说redis需要经常将内存中的数据同步到磁盘来保证持久化。redis支持两种持久化方式,一种是Snapshotting(快照)也是默认方式,另一种是Append-only file(缩写aof)的方式。

6.2.1 数据快照

快照是redis默认的持久化方式。这种方式就是将内存中数据以快照的方式写入到二进制文件中,默认的文件名为dump.rdb。可以通过配置设置自动做快照持久化。例如,可以配置redis在n秒内如果超过m个key被修改就自动做快照,下面是redis默认的快照保存设置参数:

  1. save 900 1 #900 秒内如果超过1个key被修改,则发起快照保存
  2. save 300 10 #300 秒内如果超过10个key被修改,则发起快照保存
  3. save 60 10000
  4. <div class="md-section-divider"></div>

下面介绍详细的快照保存过程:

1)redis调用fork,现在有了子进程和父进程. 
2)父进程继续处理client请求,子进程负责将内存内容写入到临时文件。由于Linux的写时复制机制(copy on write)父子进程会共享相同的物理页面,当父进程处理写请求时Linux会为父进程要修改的页面创建副本,而不是写共享的页面。所以子进程地址空间内的数据是fork时的整个数据库的一个快照。 
3)当子进程将快照写入临时文件完毕后,用临时文件替换原来的快照文件,然后子进程退出。client也可以使用save或者bgsave命令通知redis做一次快照持久化。save操作是在主线程中保存快照的,由于redis是用一个主线程来处理所有client的请求,这种方式会阻塞所有client请求。所以不推荐使用。另一点需要注意的是,每次快照持久化都是将内存数据完整写入到磁盘一次,并不是增量的只同步变更数据。如果数据量大的话,而且写操作比较多,必然会引起大量的磁盘io操作,可能会严重影响性能。 
数据快照的原理是将整个Redis中存的所有数据遍历一遍存到一个扩展名为rdb的数据文件中。通过SAVE命令可以调用这个过程。

进行有rdb文件的数据还原测试

  1. <div class="md-section-divider"></div>
  2. #进行有rdb文件的数据还原测试
  3. [root@redis-master redis]# redis-cli -a yunjisuan set names john #向redis里写入一个键值对
  4. OK
  5. [root@redis-master redis]# redis-cli -a yunjisuan get names #查看键的值
  6. "john"
  7. [root@redis-master redis]# ll /usr/local/redis/data/ #此时/usr/local/redis/data目录下没有任何东西
  8. total 0
  9. [root@redis-master redis]# redis-cli -a yunjisuan shutdown #关闭redis进程
  10. [3940] 08 Oct 19:09:08.932 # User requested shutdown...
  11. [3940] 08 Oct 19:09:08.932 * Saving the final RDB snapshot before exiting. #关闭时,redis自动进行RDB文件的保存
  12. [3940] 08 Oct 19:09:08.943 * DB saved on disk #RDB文件已经保存到了磁盘上
  13. [3940] 08 Oct 19:09:08.943 # Redis is now ready to exit, bye bye...
  14. [1]+ Done redis-server /usr/local/redis/conf/redis.conf #进程Done
  15. [root@redis-master redis]# ll /usr/local/redis/data/ #此时目录下已经生成了RDB快照
  16. total 4
  17. -rw-r--r--. 1 root root 32 Oct 8 19:09 dump.rdb
  18. [root@redis-master redis]# redis-server /usr/local/redis/conf/redis.conf & #后台启动redis
  19. [root@redis-master redis]# redis-cli -a yunjisuan get names #查询redis中的键值对
  20. "john" #数据恢复
  21. <div class="md-section-divider"></div>

进行无rdb文件的数据丢失测试

  1. <div class="md-section-divider"></div>
  2. #登陆redis
  3. [root@redis-master redis]# redis-cli -a yunjisuan
  4. 127.0.0.1:6379> keys * #有数据
  5. 1) "names"
  6. 127.0.0.1:6379> quit #退出
  7. [root@redis-master redis]# redis-cli -a yunjisuan shutdown #关闭服务
  8. [root@redis-master redis]# netstat -antup | grep redis #默认端口6379已经消失
  9. tcp 0 0 0.0.0.0:6381 0.0.0.0:* LISTEN 3519/redis-server *
  10. tcp 0 0 :::6381 :::* LISTEN 3519/redis-server *
  11. [root@redis-master redis]# pwd #当前路径位置
  12. /usr/local/redis
  13. [root@redis-master redis]# ll data/ #redis数据目录下存在.rdb文件
  14. total 4
  15. -rw-r--r--. 1 root root 32 Oct 8 21:20 dump.rdb
  16. [root@redis-master redis]# rm -rf data/dump.rdb #删除.rdb文件
  17. [root@redis-master redis]# ll data/ ##查看目录为空
  18. total 0
  19. [root@redis-master redis]# redis-server /usr/local/redis/conf/redis.conf & #后台启动redis
  20. [root@redis-master redis]# netstat -antup | grep redis
  21. tcp 0 0 0.0.0.0:6379 0.0.0.0:* LISTEN 4022/redis-server *
  22. tcp 0 0 0.0.0.0:6381 0.0.0.0:* LISTEN 3519/redis-server *
  23. tcp 0 0 :::6379 :::* LISTEN 4022/redis-server *
  24. tcp 0 0 :::6381 :::* LISTEN 3519/redis-server *
  25. [root@redis-master redis]# redis-cli -a yunjisuan #登陆redis
  26. 127.0.0.1:6379> keys * #数据已经丢失
  27. (empty list or set)
  28. <div class="md-section-divider"></div>

6.2.2 Append-Only File(追加式的操作日志)

  • 另外由于快照方式是在一定间隔时间做一次的,所以如果redis意外down掉的话,就会丢失最后一次快照后的所有修改。如果应用要求不能丢失任何修改的话,可以采用aof持久化方式。下面介绍Append-only file。
  • aof比快照方式有更好的持久化性,是由于在使用aof持久化方式时,redis会将每一个收到的写命令都通过write函数追加到文件中(默认是appendonly.aof)。当redis重启时会通过重新执行文件中保存的写命令来在内存中重建整个数据库的内容.当然由于os会在内核中缓存write做的修改,所以可能不是立即写到磁盘上。这样aof方式的持久化也还是有可能会丢失部分修改。不过我们可以通过配置文件告诉redis我们想要通过fsync函数强制os写入到磁盘的时机。有三种方式如下(默认是:每秒fsync一次)
  • appendonly yes #启用aof持久化方式
  • appendfsync always #收到写命令就立即写入磁盘,最慢,但是保证完全的持久化
  • appendfsync everysec #美秒钟写入磁盘一次,在性能和持久化方面做了很好的折中
  • appendfsync no #完全依赖os,性能最好,持久化没保证
  • redis还支持一种追加式的操作日志记录,叫append only file,其日志文件以aof结尾,我们一般各为aof文件。要开启aof日志的记录,你需要在配置文件中进行如下设置:

appendonly yes

aof引发的问题:

aof的方式也同时带来了另一个问题。持久化文件会变得越来越大.例如我们调用incr test命令100次,文件中必须保存全部的100条命令,其实有99条都是多余的。因为要恢复数据库的状态其实文件中保存一条set test 100 就够了。为了压缩aof的持久化文件。redis提供了bgrewriteaof命令。收到此命令redis将使用与快照类似的方式将内存中的数据以命令的方式保存到临时文件中,最后替换原来的文件。具体过程如下:

  1. redis调用fork,现在有父子两个进程
  2. 子进程根据内存中的数据库快照,往临时文件中写入重建数据库状态的命令。
  3. 父进程继续处理client请求,除了把写命令写入到原来的aof文件中。同时把收到的写命令缓存起来.这样就能保证如果子进程重写失败的话并不会出问题。
  4. 当子进程把快照内容写入已命令方式写到临时文件中后,子进程发信号通知父进程。然后父进程把缓存的写命令也写入到临时文件。
  5. 现在父进程可以使用临时文件替换老的aof文件,并重命令名,后面收到的写命令也开始往新的aof文件中追加。

需要注意到是重写aof文件的操作,并没有读取旧的aof文件,而是将整个内存中的数据库内容用命令的方式重写了一个新的aof文件,这点和快照有点类似。接下来我们看一下实际的例子。

开启bgrewriteaof重写的方式

  1. <div class="md-section-divider"></div>
  2. ##开启AOF
  3. [root@redis-master redis]# cat -n /usr/local/redis/conf/redis.conf | grep 449
  4. 449 appendonly yes #修改本行内容开启AOF
  5. <div class="md-section-divider"></div>
  6. #重启redis服务
  7. [root@redis-master redis]# redis-cli -a yunjisuan shutdown
  8. [4022] 08 Oct 23:27:22.183 # User requested shutdown...
  9. [4022] 08 Oct 23:27:22.183 * Saving the final RDB snapshot before exiting.
  10. [4022] 08 Oct 23:27:22.195 * DB saved on disk
  11. [4022] 08 Oct 23:27:22.195 # Redis is now ready to exit, bye bye...
  12. [1]+ Done redis-server /usr/local/redis/conf/redis.conf
  13. [root@redis-master redis]# redis-server /usr/local/redis/conf/redis.conf &
  14. <div class="md-section-divider"></div>
  15. #关于bgrewriteaof重写的配置文件代码如下:
  16. [root@redis-master ~]# cat -n /usr/local/redis/conf/redis.conf | sed -n '503,521p'
  17. 503 # Automatic rewrite of the append only file.
  18. 504 # Redis is able to automatically rewrite the log file implicitly calling
  19. 505 # BGREWRITEAOF when the AOF log size grows by the specified percentage.
  20. 506 #
  21. 507 # This is how it works: Redis remembers the size of the AOF file after the #它是如何工作的呢?redis会记住AOF文件的大小
  22. 508 # latest rewrite (if no rewrite has happened since the restart, the size of #当最后一次重写的时候,如果在重启时没有重写发生。
  23. 509 # the AOF at startup is used). #那么AOF文件会在开始时被使用
  24. 510 #
  25. 511 # This base size is compared to the current size. If the current size is
  26. 512 # bigger than the specified percentage, the rewrite is triggered. Also
  27. 513 # you need to specify a minimal size for the AOF file to be rewritten, this
  28. 514 # is useful to avoid rewriting the AOF file even if the percentage increase
  29. 515 # is reached but it is still pretty small.
  30. 516 #
  31. 517 # Specify a percentage of zero in order to disable the automatic AOF
  32. 518 # rewrite feature.
  33. 519
  34. 520 auto-aof-rewrite-percentage 100 #当100%达到最小大小的时候才会执行重写
  35. 521 auto-aof-rewrite-min-size 64mb #自动重写aof文件的最小大小
posted @ 2019-07-24 15:08  L1n  阅读(401)  评论(0编辑  收藏  举报