• 1. Redis 深入浅出 Http://www.hoterran.info Http://Weibo.com/hoterran
  • 2. agendause feature architecture admin replication presistence misc
  • 3. useDownload http://redis.googlecode.com/files/redis-2.2.11.tar.gz install Redis-server > /dev/null & Redis-cli http://try.redis-db.com/
  • 4. useset/get lpush/lrem/rpop/ hset/hgetall sadd/srem multi/discard/exec
  • 5. featureAdvanced key value store like memcachedb single threaded, no lock Event demultiplexer, no use libevent 2w lines writen in c without 3rd library faster
  • 6. featureDifference with memached Data structure No only memcached k-v Linklist k-v1->v2->v3->v4... Set k - (v1,v2,v3,v4) Hash k – (k1 -v1, k2 – v2, k3 – v3) Need design a little Replication Presistence not support LRU Need multi instance use multi core
  • 7. featureDifference with MySQL Not Heap table store Not need index, so not execute plan Not compute statistic No sql No join Capacity size limited by cache a little design before dev High rw
  • 8. architecture
  • 9. architecture
  • 10. adminRedis.conf parameter intro Replication intro Persistence intro
  • 11. replicationprocedure map Dual master mode? Master slave slave mode defects Without resume broken transfer 可以架两级slave解决? Without lag(slave position)
  • 12. replication
  • 13. replicationCase replication switch old redis version running on, say, redis:6379. Install and start a new redis on redis:6380 with a different dump file location. Execute SLAVE OF redis 6379 against redis:6380. Wait for first SYNC to complete. echo "enable server redis/redis-6380" | socat stdio unix-connect:/var/run/haproxy/admin.sock echo "disable server redis/redis-6379" | socat stdio unix-connect:/var/run/haproxy/admin.sock Execute SLAVE OF no one on redis:6380. Execute SHUTDOWN on redis:6379. Assuming that you've got a HAProxy config looking something like listen redis :6378 server redis-6379 localhost:6379 check weight 256 server redis-6380 localhost:6380 check disabled
  • 14. persistenceSnapshot Fork process, loop hash table, save on file dump.rdb Yes!!! sequential write Write dump.rdb need O_DIRECT? Aof Like binlog, for recover after crash appendfsync Aof file is big use bgrewriteaof
  • 15. Login userDesign 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? Max login man?
  • 16. Login userdata 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
  • 17. Login userMax 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?
  • 18. tagRelational is suitable? book id book author 1 <> gun 2 <> japan Tag Tagname id Web 1 Web 2 Python 2 Sql select id from tag t1, tag t2 where t1.tagname = 'web' and t2.tagname = 'python' and t1.id = t2.id
  • 19. tagKv design set book:1 ”diving into python” Set book:2 ”ruby on rail” sadd tag:web 2 sadd tag:web 1 sadd tag:python 1 sinter 'tag:web' 'tag:python' sunion tag:web 'tag:python' sdiff tag:web 'tag:python' 反思?
  • 20. Sina weiboRecent record List, Lrange(关注的人列表、粉丝列表、feeds列表) Set Sadd|, sdiff, sinter (我的好友,我可能感兴趣的人) Score Zadd, zset(feeds排名) Hash Hincrby, hgetall(用户资料,关注数,粉丝数,微博数 ) Delete history data expire
  • 21. Sina weibo我关注的人 sadd user:hoterran:follows user:foolishceo sadd user:hoterran:follows user:logzgh sadd user:hoterran:follows user:sqlrush smembers user:hoterran:follows 英杰关注的人 sadd user:sqlrush:follows user:logzgh sadd user:sqlrush:follows user:hoterran
  • 22. Sina weibo我们共通关注的人 sinter user:sqlrush:follows user:hoterran:follows 我的粉丝 sadd user:logzgh:follows user:hoterran sadd user:hoterran.follower user:logzgh sadd user:hoterran.follower user:sqlrush smembers user:hoterran:follower
  • 23. Sina weibo我的好友 sinter user:hoterran:follows user:hoterran:follower 我们都关注的人 sinter user:sqlrush:follows user:hoterran:follows
  • 24. Sina weibo可能感兴趣的的人 sadd user:sqlrush:follows user:free_yz sdiff user:sqlrush:follows user:hoterran:follows 我关注的人数,我的粉丝数,我的 Hset hoterran:profile follows 1 Hset hoterran:profile follower 2 Hset hoterran:profile blog 3 Hgetall hoterran:profile Hincrby hoterran:profile blog 1
  • 25. Suitable scene轻量级的高性能消息队列服务 生产者消费者 Producer lpush Consumer blpop Redis的主要缺点是数据库容量受到物理内存的限制,不能用作海量数据的高性能读写,并且它没有原生的可扩展机制,不具有scale(可扩展)能力,要依赖客户端来实现分布式读写,因此Redis适合的场景主要局限在较小数据量的高性能操作和运算上。
  • 26. summaryDefects Failover Replication Dba level Memory limit size Advantage High qps Data structure
  • 27. qaReplace aliWangwang k-v(blob) Can satisfy Online Log requirement?