redis深入浅出
DESCRIPTION
NoSQLFan作者阮若夷在CSDN的BigData大会发言稿:《Redis深入浅出》系统讲解了Redis内部数据结构及实现原理,并列举了使用Redis进行存储的一些典型使用方法。TRANSCRIPT
redis深入浅出
Agenda
Feature&Architecture Admin&capcity forecast Replication Presistence Table design
Feature
redis memcached
data structure string|list|hash|set|zset string
thread model third(main thread, two auxiliary thread)
many
demultiplexer ae.c libevent(3rd library)
persistence yes no
replication yes no
design weak no
String
Set hello world
Set hello 1234567 Set hello 1234
List
Lpush list aaaa bbb ccc
Double link list, so can lpush,rpush,lpop,rpop
Zipmap
Hset test hello world
Hset test aliyun dba
Zipmap defect:Zmlen one bytes, only 253 subkey
Architecture
Rehash
Cache forecast
string 类型的内存大小 = 键值个数 * (dictEntry大 小 + redisObject 大小 + 包含 key的 sds 大小 +
包含 value的 sds大小 ) + bucket 个数 * 4
zipmap 类型的内存大小 = hashkey 个数 * (dictEntry 大小 + redisObject 大小 + 包含 key的sds 大小 + subkey的总大小 ) + bucket 个数 * 4
Jemalloc size class
Admin
Redis.conf(static), config set(dynamic)
– maxmemory <bytes>
– Appendfsync everysec(fsync pagecache to disk)
– hash-max-zipmap-entries 512
– hash-max-zipmap-value 64
Replication
procedure map Master slave – slave – slave - slave
defects Without resume broken transfer Without lag(slave position)
replication
Persistence
Snapshot Fork process, loop hash table, save file dump.rdb Yes!!! sequential write Write dump.rdb need O_DIRECT
Aof Like binlog, for recover after crash Auto bgrewrteaof Auxiliary threads Postpone fsync
Auxiliary thread
Table desgin login user
Design a login user system Heap table
userid login_times last_login_time 1 5 2011-1-1 2 1 2011-1-2 3 2 2011-1-3
Last login man? Create index on last_login_time Max login man? Create index on login_times Index, explan plan, compute statistics is suck!!!
Login user
data
Set userid:1:login_times 5
Set userid:2:login_times 1
Set userid:3:login_times 2
Set userid:1:last_login 2011-1-1
Set userid:2:last_login 2011-1-2
Set userid:3:last_login 2011-1-3
Last login
lpush user_last_login 1
lpush user_last_login 2
lpush user_last_login 3
ltrim user_last_login 0 1
Login user
Max login man zadd user:login_times 5 1 zadd user:login_times 1 2 zadd user:login_times 2 3 zcard user:login_times zrangebyscore user:login_times 3 +inf withscores
Column store data?
Suitable Scene
轻量级的高性能消息队列服务 ,生产者消费者 跨机器的共享内存 Redis的主要缺点是数据库容量受到物理内存的限制,不能用作海量数据的高性能读写,并且它没有原生的可扩展机制,不具有scale(可扩展)能力,要依赖客户端来实现分布式读写,因此 Redis适合的场景主要局限在较小数据量的高性能操作和运算上。
QA
@hoterran
www.hoterran.info