Top Banner
http://jiaqing.me 2012-05-21 Another Introduce to Redis
28

Another Introduce to Redis

Jul 07, 2015

Download

Technology

jiaqing zheng
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Another Introduce to Redis

http://jiaqing.me

2012-05-21

Another Introduce to Redis

Page 2: Another Introduce to Redis

初识Redis

实践Redis

优化Redis

2

大纲

Page 3: Another Introduce to Redis

3

Part 1

初识Redis

Page 4: Another Introduce to Redis

4

介绍

官方定义:

Redis is an open source, advanced key-value store. It is often referred to as a data structure server since keys can contain strings, hashes, lists, sets and sorted sets.

Page 5: Another Introduce to Redis

5

介绍

官方定义:

Redis is an open source, advanced key-value store. It is often referred to as a data structure server since keys can contain strings, hashes, lists, sets and sorted sets.

主要卖点:

Key-value 内存级 高性能 复杂数据结构

Page 6: Another Introduce to Redis

6

相同点:

• 都基于内存,性能都足够高

Redis vs Memcached

Page 7: Another Introduce to Redis

7

Redis vs Memcached

相同点:

• 都基于内存,性能都足够高

不同点:

• Redis支持复杂类型

• Redis支持主从复制

• Redis支持数据持久化

• Redis支持虚拟内存

• Redis只使用单核

• Redis自身暂不支持sharding

Page 8: Another Introduce to Redis

8

数据类型

key:

• String类型

• 非二进制安全: “my name” Error “myname\n” Error

• 约定俗成的命名: object-type:id:field 比如 user:1000:password

Page 9: Another Introduce to Redis

9

数据类型

value:

• String: 二进制安全, 最大1G

• List :String类型的双向链表

• Set :String类型的无序集合,可取交集、并集、差集

• SortSet :可排序的Set, 可指定score, 区间操作

• Hash:String类型的k-v映射

Page 10: Another Introduce to Redis

10

eg.list

LPUSH mylist a # now the list is “a”

LPUSH mylist b # now the list is “b”, “a”

RPUSH mylist c # now the list is “b”, “a”, “c”

Page 11: Another Introduce to Redis

11

eg.hash

动手尝试 http://try.redis-db.com/

命令手册 http://redis.io/commands

中文参考 http://redis.readthedocs.org/en/latest/

Page 12: Another Introduce to Redis

12

事务

支持连接级别的简单事务

• multi :启动事务

• exec:提交事务

• discard:放弃事务

redis> multiOKredis> incr aQUEUEDredis> incr bQUEUEDredis> exec1. (integer) 12. (integer) 1

Page 13: Another Introduce to Redis

13

持久化

Snapshotting(默认):

• 将内存数据写入二进制文件

• 可配置N秒执行一次

save 900 1 #900秒内如果超过1个key被修改,则发起快照保存

save 300 10 #300秒内容如超过10个key被修改,则发起快照保存

save 60 10000 #...• 宕机时丢失最后一次快照

Page 14: Another Introduce to Redis

14

持久化

AOF(append-only file):

• 每次写请求写入文件

• 自定义写入的时机

appendfsync always #每次收到写命令就立即强制写入磁盘

appendfsync everysec #每秒钟强制写入磁盘一次

appendfsync no #完全依赖OS,性能最好,持久化没保证

Page 15: Another Introduce to Redis

15

主从复制

• Master可带多个Slave

• 初次同步数据,Master不阻塞,Slave阻塞

Page 16: Another Introduce to Redis

16

虚拟内存

• 突破单机内存限制

• 只会将value放入swap

• 不推荐使用

Page 17: Another Introduce to Redis

17

Redis客户端

CC++C#JavaLuaNode.jsPerlGoRubyPHP…查看全部 http://redis.io/clients

Page 18: Another Introduce to Redis

18

Part 2

实践Redis

Page 19: Another Introduce to Redis

19

简单的微博

• 个人信息

• 关注列表

• 粉丝列表

• 推送信息

Page 20: Another Introduce to Redis

20

个人信息

• 用户唯一标识(ID)

• 录入个人相关信息

SET global:newUserId 0…INCR global:nextUserId => 1000SET uid:1000:username asukaSET uid:1000:password *****SET uid:1000:url /asukaSET uid:1000:xxx xxxxxxxxx…

Page 21: Another Introduce to Redis

21

关注/粉丝

• 添加关注

• 添加粉丝

• 统计粉丝数

• 获取关注列表

• 共同关注

# 添加关注

SADD uid:1000:following 1001SADD uid:1000:following 1002SADD uid:1000:following 1003…# 添加粉丝

SADD uid:1000:followers 0001SADD uid:1000:followers 0002SADD uid:1000:followers 0003…#获取关注列表

SMEMBERS uid:1000:following=>1002,1003,1001#统计多少粉丝

SCARD uid:1000:followers=>3#共同关注

SINTER uid:1000:following uid:1001:following…

Page 22: Another Introduce to Redis

22

推送消息

• 发送消息

• 接收消息

# 发送消息

SET postid:900001 我的第一条微博

RPUSH uid:1000:posts 900001

# 接收消息RPUSH uid:1001:receive 900001

# 翻页查看

LRANGE uid:1001:receive 0 9=>900001,893201,939922,…..

Page 23: Another Introduce to Redis

23

Part 3

优化Redis

Page 24: Another Introduce to Redis

24

避免使用VM

• 频繁swap性能差

• 官方版本准备去除该功能

Page 25: Another Introduce to Redis

25

合理利用CPU

• 单线程的优势

• 单机多实例 : 实例数 = CPU - 1

Page 26: Another Introduce to Redis

26

重视容量规划

• 合理预估内存/带宽

• 实时监控同样重要

Page 27: Another Introduce to Redis

27

Part 4

参考资料http://redis.io/

http://blog.nosqlfan.com

http://www.slideshare.net/search/slideshow?searchfrom=header&q=redis

Page 28: Another Introduce to Redis

28

http://jiaqing.me

2012-05-21

Another Introduce to Redis

-END-