高性能 NoSQL 数据库 Redis

30
高高高 NoSQL 高高高 Redis 盛盛盛盛 盛盛盛 盛盛http://www.paitoubing.cn

description

高性能 NoSQL 数据库 Redis. 盛大在线:徐江涛. 博客: http://www.paitoubing.cn. NoSQL 大行其道. 高并发请求. APP. 海量化数据. memcached. Disk. 内存. 图灵奖得主 Jim Gray :“ 内存是新的硬盘,硬盘是新的磁带”. Redis 分享大纲. Redis 是什么. Redis 的特性. Redis 的 API 及适用场景. Redis 的使用经验 , 运维参数. Redis 是什么. What is. sponsored by:. Redis is... - PowerPoint PPT Presentation

Transcript of 高性能 NoSQL 数据库 Redis

Page 1: 高性能 NoSQL 数据库 Redis

高性能NoSQL数据库Redis

盛大在线:徐江涛

博客: http://www.paitoubing.cn

Page 2: 高性能 NoSQL 数据库 Redis

NoSQL 大行其道

APP高并发请求

海量化数据

Disk 内存

图灵奖得主 Jim Gray :“内存是新的硬盘,硬盘是新的磁带”

memcached

Page 3: 高性能 NoSQL 数据库 Redis

Redis 分享大纲

Redis 是什么

Redis 的特性

Redis 的 API 及适用场景

Redis 的使用经验 , 运维参数

Page 4: 高性能 NoSQL 数据库 Redis

Redis 是什么

sponsored by:What is

Redis is...an “advanced key-value store”

b yS A LVATO R E S A N F I L I P P O

( @ a n t i r e z )

Page 5: 高性能 NoSQL 数据库 Redis

Redis vs Memcached

Memcached 操作实例

键 (keys) 值 (values)

page:index.html <html><head>[...]

user:101:session xDrSdEwd4dSlZkEkj+

login_count “100001”

user:100:last_login_time “102736485756”

所有的操作都是基于字符串、或者是 2 进制的数据格式、操作起来大部分是 GET 、 SET 这 2 个命令操作 , 易于使用

Page 6: 高性能 NoSQL 数据库 Redis

Redis vs Memcached

和 Memcached 一样、使用简单、性能高效 (1G 1M)

异步持久化存储

支持多种数据类型 ;Strings( 字符 ) 、 Lists( 链表 ) 、Sets( 集合 ) 、 ZSets( 有序集合 ) 、 Hashes( 哈希 )

更多、更方便 API 接口 管理数据 ;More commands

and More….

Is Redis

Page 7: 高性能 NoSQL 数据库 Redis

Redis vs Memcached

Redis 数据类型实例

键 (keys) 值 (values)

page:index.html <html><head>[...]

users_logged_in_today { 1, 2, 3, 4, 5 }

latest_post_ids [201, 204, 209,..]

users_and_scores joe ~ 1.3483bert ~ 93.4fred ~ 283.22chris ~ 23774.17

String

Sets

List

ZSets

Page 8: 高性能 NoSQL 数据库 Redis

Redis 的 API 及适用场景

Strings 字符

./redis-cli SET mystring “ hello world ”

C client key value

./redis-cli GET mystring

hello world

return

Page 9: 高性能 NoSQL 数据库 Redis

GETSETMGETSETNXSETEXMSETMSETNX

Redis 的 API 及适用场景

Strings 字符

INCRINCRBYDECRDECRBYAPPENDSUBSTR

Page 10: 高性能 NoSQL 数据库 Redis

Redis 的 API 及适用场景

Expiration

当把 Redis 当作缓存服务使用时 , 你可以设置数据的过期时间 , 不管是数据类型(string,list,zsets,sets,hash) 是什么,此系列命令都有效

EXPIRE paitoubing 1234

TTL paitoubing == 1234

Page 11: 高性能 NoSQL 数据库 Redis

Redis 的 API 及适用场景

Deleting Keys

DEL paitoubing

EXISTS paitoubing == 0 (false)

Page 12: 高性能 NoSQL 数据库 Redis

Redis 的 API 及适用场景

Lists

a b c d e f

LPUSH RPUSH

LPOP RPOP

LPUSH paitoubing a

Page 13: 高性能 NoSQL 数据库 Redis

Redis 的 API 及适用场景

Lists

a b c d e f

LLEN == 6

x

LREM 1 b

LRANGE 2 3

LINDEX 5

Page 14: 高性能 NoSQL 数据库 Redis

Redis 的 API 及适用场景

队列服务 Queues

a b c d e f

RPUSH

LPOPRPUSH paitoubing abcRPUSH paitoubing defLPOP paitoubing == “abc”LPOP paitoubing == “def”LPOP paitoubing == (nil)

Page 15: 高性能 NoSQL 数据库 Redis

Redis 的 API 及适用场景

集合 Sets

uid:1:followers bruce tom zhangsan lisi wangermazi xiaoyueyue fengjie

uid:2:followersxiaoyueyue fengjie liyuchun furongjiejie

SREM uid:1:followers wangermazi

x

SMOVE uid:1:followers uid:2:followers lisi

SADD uid:2:followers tom

Page 16: 高性能 NoSQL 数据库 Redis

Redis 的 API 及适用场景

集合 Sets

uid:1:followers bruce tom zhangsan lisi wangermazi xiaoyueyue fengjie

uid:2:followersxiaoyueyue fengjie liyuchun furongjiejie

SCARD uid:1:followers == 7SISMEMBER uid:1:followers xinxin == 0 (meaning false)SRANDMEMBER uid:1:followers == “xiaoyueyue”

SMEMBERS uid:2:followers

== xiaoyueyue fengjie liyuchun furongjiejie

Page 17: 高性能 NoSQL 数据库 Redis

Redis 的 API 及适用场景

集合 Sets (交集、并集、差集)

uid:1:followers bruce tom zhangsan lisi wangermazi xiaoyueyue fengjie

uid:2:followersxiaoyueyue fengjie liyuchun furongjiejie

xiaoyueyue fengjie

SINTER uid:1:followers uid:2:followers == xiaoyueyue fengjie

SINTERSTORE SUNION SUNIONSTORE SDIFF SDIFFSTORE

Page 18: 高性能 NoSQL 数据库 Redis

Redis 的 API 及适用场景

有序集合 ZSets

Like Sets

每个元素 , 增加了 rank, 或是 score

no time!

Page 19: 高性能 NoSQL 数据库 Redis

Redis 的 API 及适用场景

Hashes (哈希)产品 :Product 1

created_at : 1290149988product_id : 1name : 盛大点券available : 100

HSET product:1 created_at 1290149988HSET product:1 name “ 盛大点券”

HSET product:1 available 100

HGET product:1 name == 盛大点券

HLEN product:1 == 3

HKEYS product:1 == created_at,

name,availableHGETALL product:1 ==

created_at => 102374657 name=> 盛大点券 available=> 100

HVALS HEXISTS HINCRBY HMGET HMSET

Page 20: 高性能 NoSQL 数据库 Redis

Redis 的 API 及适用场景

Redis Social Network (社会化网络)

UGC

好友:双向关系

粉丝:单向关系

text photo

more…..blog 、 Message 、 photo 、

Page 21: 高性能 NoSQL 数据库 Redis

Redis 的 API 及适用场景

新增用户

usernameuserid

INCR next_user_id Return s [uid]SET user:[uid]:name [username]

SET username:[username] [id]

Int 整形

变量值新增日记

contentauthor

I NCR next_post_id Return s [pid]

SET post:[pid]:content [content]

SET post:[pid]:author [uid]

LPUSH user:[uid]:posts [pid]

LPUSH posts:global [pid]

Redis Social Network (社会化网络)

Unique IDs

Page 22: 高性能 NoSQL 数据库 Redis

SORT

ZCARD

MONITOR

SLAVEOFRENAME

SELECTSAVE

SUBSCRIBE

PUBLISH

Redis 的 API 及适用场景

Enough commands! (版本更新快)

Page 23: 高性能 NoSQL 数据库 Redis

Redis 的 安装

下载: http://code.google.com/p/redis/

Redis的安装

tar zxvf redis-version.tar.gzcd redis-versionmake

由于没有 make install ,所以得把源代码目录里的关键文件手动复制到适当的位置:

cp redis.conf /etc/cp redis-benchmark redis-cli redis-server /usr/bin/

修改 /etc/redis.conf daemonize yes

启动 redis 服务# /usr/bin/redis-server /etc/redis.conf

Page 24: 高性能 NoSQL 数据库 Redis

Redis 的 安装

Redis.conf (配置文件)daemonize yes => 以守护进程的方式运行

maxmemory => Redis 在启动时会把所有数据加载到内存中 , 设置使用内存限制 , 新的 vm 机制 ,key 存放内存 ,value 会存放在 swap 区 , 不建议 可通过 consistent hashing 把数据分布到多个服务器上

save 900 1save 300 10save 60 10000

=> 900 秒内有 1 个改变, 300 秒内有 10 个改变, 60 秒内有 10000 个改变, redis 就会内存中的 key 保存到数据库文件中去

dbfilename dump.rdb 保存数据的路径slaveof 192.168.1.2 6379 => 在启动时, REDIS 会自动从 MASTER 上把数据

先同步过来,而无需我们手动进行 MASTER 上每有一次落地保存,会自动向 SLAVE 进行同步。当然这里的问题是,如果 MASTER 不保存,SLAVE 也就无法得到这些数据,这和 REDIS 本身的内存写磁盘逻辑是一样

Page 25: 高性能 NoSQL 数据库 Redis

Redis 的 安装

requirepass  => 连接密码(注释 )

rdbcompression  => 存储至本地数据库时是否压缩数据,默认为 yes

masterauth <master-password> =>   当本机为从服务时, 设置主服务的连接密码(注释)

maxclients  => 最大客户端连接数,默认不限制(注释)appendonly  => 是否在每次更新操作后进行日志记录,如果不开启,可能会在断电时导致一段时间内的数据丢失。因为 redis 本身同步数据文件是按上面 save条件来同步的,所以有的数据会在一段时间内只存在于内存中。默认值为 no

appendfsync  => 更新日志条件,共有 3 个可选值。no 表示等操作系统进行数据缓存同步到磁盘,always 表示每次更新操作后手动调用 fsync()将数据写到磁盘,everysec 表示每秒同步一次(默认值)。

vm-enabled  是否使用虚拟内存

Page 26: 高性能 NoSQL 数据库 Redis

NO PRODUCT !

Redis 的主从 - 集群

client client client

master master master

slave slave slave

CONSISTENT HASH / 定容

read

write

同步数据

https://github.com/antirez/redis/raw/master/design-documents/REDIS-CLUSTER

Sample demo,

风险!

SESSION

slave

Page 27: 高性能 NoSQL 数据库 Redis

Redis 的开发语言支持

Language Support

Ruby, Python, PHP, Erlang,Tcl, Perl, Lua, Java, Scala,Clojure, C#, C/C++,JavaScript/Node.js, Haskell,IO, Go

Page 28: 高性能 NoSQL 数据库 Redis

6379 在是手机按键上 MERZ对应的号码MERZ取自意大利歌女 Alessia Merz的名字

6379

Redis 端 口 的故事

Why?

Page 29: 高性能 NoSQL 数据库 Redis

Redis 的扩展阅读

1 、 http://code.google.com/p/redis/

我怎么获得更多,更全的 Redis信息 ?

2 、 http://redis.io

3 、 http://antirez.com/

4 、 http://www.google.com

Page 30: 高性能 NoSQL 数据库 Redis

END&

QA