redis深入浅出

19
redis 深深深深

Upload: iammutex

Post on 06-May-2015

15.881 views

Category:

Technology


7 download

DESCRIPTION

NoSQLFan作者阮若夷在CSDN的BigData大会发言稿:《Redis深入浅出》系统讲解了Redis内部数据结构及实现原理,并列举了使用Redis进行存储的一些典型使用方法。

TRANSCRIPT

Page 1: Redis深入浅出

redis深入浅出

Page 2: Redis深入浅出

Agenda

Feature&Architecture Admin&capcity forecast Replication Presistence Table design

Page 3: Redis深入浅出

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

Page 4: Redis深入浅出

String

Set hello world

Set hello 1234567 Set hello 1234

Page 5: Redis深入浅出

List

Lpush list aaaa bbb ccc

Double link list, so can lpush,rpush,lpop,rpop

Page 6: Redis深入浅出

Zipmap

Hset test hello world

Hset test aliyun dba

Zipmap defect:Zmlen one bytes, only 253 subkey

Page 7: Redis深入浅出

Architecture

Page 8: Redis深入浅出

Rehash

Page 9: Redis深入浅出

Cache forecast

string 类型的内存大小 = 键值个数 * (dictEntry大 小 + redisObject 大小 + 包含 key的 sds 大小 +

包含 value的 sds大小 ) + bucket 个数 * 4

zipmap 类型的内存大小 = hashkey 个数 * (dictEntry 大小 + redisObject 大小 + 包含 key的sds 大小 + subkey的总大小 ) + bucket 个数 * 4

Jemalloc size class

Page 10: Redis深入浅出

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

Page 11: Redis深入浅出

Replication

procedure map Master slave – slave – slave - slave

defects Without resume broken transfer Without lag(slave position)

Page 12: Redis深入浅出

replication

Page 13: Redis深入浅出

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

Page 14: Redis深入浅出

Auxiliary thread

Page 15: Redis深入浅出

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!!!

Page 16: Redis深入浅出

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

Page 17: Redis深入浅出

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?

Page 18: Redis深入浅出

Suitable Scene

轻量级的高性能消息队列服务 ,生产者消费者 跨机器的共享内存 Redis的主要缺点是数据库容量受到物理内存的限制,不能用作海量数据的高性能读写,并且它没有原生的可扩展机制,不具有scale(可扩展)能力,要依赖客户端来实现分布式读写,因此 Redis适合的场景主要局限在较小数据量的高性能操作和运算上。

Page 19: Redis深入浅出

QA

@hoterran

www.hoterran.info