SlideShare a Scribd company logo
1 of 20
Download to read offline
redis 适用场景与实现


     田琪
主题


    数据类型与实现

    持久化与复制

    容量规划

    适用场景

    相关参数与优化
redis 概述

   a disk backed in-memory database

    高性能网络接口 + 数据结构集合
网络接口
   单线程 IO 复用
   aeEvent 封装 IO 与定时器
   默认支持 10K 连接
AE_SETSIZE (1024*10)
   why not multi-thread
       cache coherency
       complexity with locks
       Deadlocks
redis 数据类型
 •   string
 •   hash
 •   list
 •   set
 •   sorted set
redis 基础数据结构
 • redisObject
    数据指针 + type + encoding
 • 内存容量占用 ( 不包含数据本身 )
    16 字节( 64 位 , 不开启 vm )
    40 字节 ( 64 位,开启 vm)
    redis 里面所有存储对象 (key,value) 都
    redisObject 类型
 • 案例
   set,get,incr 等
redis list - 双向链表
• 内存占用
    list 每个元素占用内存
    listNode( 前后指针,数据节点指针 )
    数据本身指针 (robj)
    24 字节 + 16 字节 = 40 字节
    40 字节 + 元素本身大小


     相关参数
    list-max-ziplist-entries
    list-max-ziplist-value

• 使用场景
    关注列表,粉丝列表 ...
redis hash
 • 数据结构实现
   zipmap & real hashmap
 • 内存容量占用
   zipmap 更省内存
   相关参数
  hash-max-zipmap-entries
  hash-max-zipmap-value
 • 使用场景
   用户计数器
redis set & sorted set
 • set 数据结构实现
    hashmap (value 为 NULL)
 • sorted set 数据结构实现
    hashmap(element->score)
    skiplist ( 按 element 插入有序 )
  相关参数


    set-max-intset-entries
 • 使用场景
   用户发件箱 mid
hash & rehash
    双 hash 表做 rehash
    预规划减小 rehash 次数
    rehash 时每次操作迁移一个 bucket
    相关参数
     DICT_HT_INITIAL_SIZE 4
     REDIS_HT_MINFILL     10
     Activerehashing
redis 持久化机制
 • snapshot
  save 参数
 • aof
  appendfsync 参数
 • vm
  vm is not the way to go for the future
  slow restarts,slow saving,slow replication,complex code
 • diskstore
   key,value stored on disk
   memory works as a cache ( read through,write back)
redis 复制
 • 实现机制
  快照同步
 • 存在的问题
   无增量复制
   slave 表重建
redis 容量规划与建议


 • 业务需要哪几种数据类型
 • 预先做好容量规划
 • sharding 策略与 HA
  rolling/pre-sharding/ 双写 / 多 slave
 • 使用 pipeline 减小网络 IO
redis 适用场景
 • MC + MySQL 不能完全解决 web2.0 的需求
    mc 穿透问题,跨机房问题 ,...
 • 众多开源 nosql 百花齐放
    Cassandra,Riak,Voldemort
    Hbase
    CouchDB,MongoDB
    ...
 • what we need
  – 小数据量,高速读写访问
  – 大数据量,有明显热点数据
  – 大数据量,无热点数据
异构读写分离
参数小结

•   vm-enabled
•   maxmemory
•   save/appendfsync
•   hash-max-zipmap-entries
•   hash-max-zipmap-value
参数小结 (> 2.2)


    list-max-ziplist-entries

    list-max-ziplist-value

    Set-max-intset-entries
源码中部分参数

•   DICT_HT_INITIAL_SIZE 4
•   REDIS_HT_MINFILL     10
•   AE_SETSIZE (1024*10)
•   REDIS_SHARED_INTEGERS 10000
小结
•   redis 最佳使用方式是 in-memory
•   根据业务选择合适数据类型
•   做好容量规划以及 pre-sharding
•   不要过度依赖复制和持久化
•   使用 pipeline 减小网络 IO
•   先了解实现后使用,如 pubsub,scripting
•   no one-size-fit-all product, understanding the
    tradeoffs
Thanks
欢迎随时交流:

我的微博 : @ 摇摆巴赫
http://t.sina.com.cn/bachmozart
我的博客:
http://www.tianqidev.com/

More Related Content

What's hot

Ocean base海量结构化数据存储系统 hadoop in china
Ocean base海量结构化数据存储系统 hadoop in chinaOcean base海量结构化数据存储系统 hadoop in china
Ocean base海量结构化数据存储系统 hadoop in china
knuthocean
 
高性能并发Web服务器实现核心内幕
高性能并发Web服务器实现核心内幕高性能并发Web服务器实现核心内幕
高性能并发Web服务器实现核心内幕
ideawu
 
Exadata那点事
Exadata那点事Exadata那点事
Exadata那点事
freezr
 
OTN软硬件结合数据库解决方案
OTN软硬件结合数据库解决方案OTN软硬件结合数据库解决方案
OTN软硬件结合数据库解决方案
freezr
 

What's hot (20)

分布式Key Value Store漫谈
分布式Key Value Store漫谈分布式Key Value Store漫谈
分布式Key Value Store漫谈
 
Hbase
HbaseHbase
Hbase
 
豆瓣数据架构实践
豆瓣数据架构实践豆瓣数据架构实践
豆瓣数据架构实践
 
美团点评技术沙龙010-点评RDS系统介绍
美团点评技术沙龙010-点评RDS系统介绍美团点评技术沙龙010-点评RDS系统介绍
美团点评技术沙龙010-点评RDS系统介绍
 
Hbase架构简介、实践
Hbase架构简介、实践Hbase架构简介、实践
Hbase架构简介、实践
 
Ocean base海量结构化数据存储系统 hadoop in china
Ocean base海量结构化数据存储系统 hadoop in chinaOcean base海量结构化数据存储系统 hadoop in china
Ocean base海量结构化数据存储系统 hadoop in china
 
高性能并发Web服务器实现核心内幕
高性能并发Web服务器实现核心内幕高性能并发Web服务器实现核心内幕
高性能并发Web服务器实现核心内幕
 
美团点评技术沙龙14:美团云对象存储系统
美团点评技术沙龙14:美团云对象存储系统美团点评技术沙龙14:美团云对象存储系统
美团点评技术沙龙14:美团云对象存储系统
 
张铁安:Feed系统架构浅析
张铁安:Feed系统架构浅析张铁安:Feed系统架构浅析
张铁安:Feed系统架构浅析
 
阿里CDN技术揭秘
阿里CDN技术揭秘阿里CDN技术揭秘
阿里CDN技术揭秘
 
Hacking Nginx at Taobao
Hacking Nginx at TaobaoHacking Nginx at Taobao
Hacking Nginx at Taobao
 
分布式缓存与队列
分布式缓存与队列分布式缓存与队列
分布式缓存与队列
 
Hadoop hive
Hadoop hiveHadoop hive
Hadoop hive
 
分布式存储的元数据设计
分布式存储的元数据设计分布式存储的元数据设计
分布式存储的元数据设计
 
NoSQL-MongoDB介紹
NoSQL-MongoDB介紹NoSQL-MongoDB介紹
NoSQL-MongoDB介紹
 
Exadata那点事
Exadata那点事Exadata那点事
Exadata那点事
 
大型系统的缓存标准化之路—从主从多级重client到一体化
大型系统的缓存标准化之路—从主从多级重client到一体化大型系统的缓存标准化之路—从主从多级重client到一体化
大型系统的缓存标准化之路—从主从多级重client到一体化
 
OTN软硬件结合数据库解决方案
OTN软硬件结合数据库解决方案OTN软硬件结合数据库解决方案
OTN软硬件结合数据库解决方案
 
大规模数据库存储方案
大规模数据库存储方案大规模数据库存储方案
大规模数据库存储方案
 
Building the Production Ready EB level Storage Product from Ceph - Dongmao Zhang
Building the Production Ready EB level Storage Product from Ceph - Dongmao ZhangBuilding the Production Ready EB level Storage Product from Ceph - Dongmao Zhang
Building the Production Ready EB level Storage Product from Ceph - Dongmao Zhang
 

Viewers also liked

高性能No sql数据库redis
高性能No sql数据库redis高性能No sql数据库redis
高性能No sql数据库redis
paitoubing
 
High powered messaging with RabbitMQ
High powered messaging with RabbitMQHigh powered messaging with RabbitMQ
High powered messaging with RabbitMQ
James Carr
 

Viewers also liked (20)

新浪微博开放平台Redis实战
新浪微博开放平台Redis实战新浪微博开放平台Redis实战
新浪微博开放平台Redis实战
 
高性能No sql数据库redis
高性能No sql数据库redis高性能No sql数据库redis
高性能No sql数据库redis
 
深入了解Redis
深入了解Redis深入了解Redis
深入了解Redis
 
Redis in Practice
Redis in PracticeRedis in Practice
Redis in Practice
 
Everything you always wanted to know about Redis but were afraid to ask
Everything you always wanted to know about Redis but were afraid to askEverything you always wanted to know about Redis but were afraid to ask
Everything you always wanted to know about Redis but were afraid to ask
 
Redis深入浅出
Redis深入浅出Redis深入浅出
Redis深入浅出
 
数据结构复习笔记 静态链表
数据结构复习笔记 静态链表数据结构复习笔记 静态链表
数据结构复习笔记 静态链表
 
Redis and Groovy and Grails - gr8conf 2011
Redis and Groovy and Grails - gr8conf 2011Redis and Groovy and Grails - gr8conf 2011
Redis and Groovy and Grails - gr8conf 2011
 
Taste Rabbitmq
Taste RabbitmqTaste Rabbitmq
Taste Rabbitmq
 
Introduction to RabbitMQ | Meetup at Pivotal Labs
Introduction to RabbitMQ | Meetup at Pivotal LabsIntroduction to RabbitMQ | Meetup at Pivotal Labs
Introduction to RabbitMQ | Meetup at Pivotal Labs
 
RabbitMQ Messaging
RabbitMQ MessagingRabbitMQ Messaging
RabbitMQ Messaging
 
Integrating RabbitMQ with PHP
Integrating RabbitMQ with PHPIntegrating RabbitMQ with PHP
Integrating RabbitMQ with PHP
 
Scaling applications with RabbitMQ at SunshinePHP
Scaling applications with RabbitMQ   at SunshinePHPScaling applications with RabbitMQ   at SunshinePHP
Scaling applications with RabbitMQ at SunshinePHP
 
RabbitMQ fairly-indepth
RabbitMQ fairly-indepthRabbitMQ fairly-indepth
RabbitMQ fairly-indepth
 
RabbitMQ Data Ingestion at Craft Conf
RabbitMQ Data Ingestion at Craft ConfRabbitMQ Data Ingestion at Craft Conf
RabbitMQ Data Ingestion at Craft Conf
 
RabbitMQ
RabbitMQRabbitMQ
RabbitMQ
 
RabbitMQ Operations
RabbitMQ OperationsRabbitMQ Operations
RabbitMQ Operations
 
Using Redis at Facebook
Using Redis at FacebookUsing Redis at Facebook
Using Redis at Facebook
 
High powered messaging with RabbitMQ
High powered messaging with RabbitMQHigh powered messaging with RabbitMQ
High powered messaging with RabbitMQ
 
RabbitMQ Data Ingestion
RabbitMQ Data IngestionRabbitMQ Data Ingestion
RabbitMQ Data Ingestion
 

Similar to redis 适用场景与实现

分布式Key-value漫谈
分布式Key-value漫谈分布式Key-value漫谈
分布式Key-value漫谈
lovingprince58
 
大规模网站架构
大规模网站架构大规模网站架构
大规模网站架构
drewz lin
 
Cassandra Technical and history overview
Cassandra Technical and history overviewCassandra Technical and history overview
Cassandra Technical and history overview
Xu Wang
 
Redis介绍
Redis介绍Redis介绍
Redis介绍
锐 张
 
基于My sql的分布式数据库实践
基于My sql的分布式数据库实践基于My sql的分布式数据库实践
基于My sql的分布式数据库实践
锐 张
 

Similar to redis 适用场景与实现 (20)

Redis 介绍 -田琪
Redis 介绍 -田琪Redis 介绍 -田琪
Redis 介绍 -田琪
 
百度系统部分布式系统介绍 马如悦 Sacc2010
百度系统部分布式系统介绍 马如悦 Sacc2010百度系统部分布式系统介绍 马如悦 Sacc2010
百度系统部分布式系统介绍 马如悦 Sacc2010
 
Nosql三步曲
Nosql三步曲Nosql三步曲
Nosql三步曲
 
Mysql调优
Mysql调优Mysql调优
Mysql调优
 
The Construction and Practice of Apache Pegasus in Offline and Online Scenari...
The Construction and Practice of Apache Pegasus in Offline and Online Scenari...The Construction and Practice of Apache Pegasus in Offline and Online Scenari...
The Construction and Practice of Apache Pegasus in Offline and Online Scenari...
 
分布式存储与TDDL
分布式存储与TDDL分布式存储与TDDL
分布式存储与TDDL
 
浅析分布式存储架构—设计自己的存储- 58同城徐振华
浅析分布式存储架构—设计自己的存储- 58同城徐振华浅析分布式存储架构—设计自己的存储- 58同城徐振华
浅析分布式存储架构—设计自己的存储- 58同城徐振华
 
分布式Key-value漫谈
分布式Key-value漫谈分布式Key-value漫谈
分布式Key-value漫谈
 
Redis介绍
Redis介绍 Redis介绍
Redis介绍
 
大规模网站架构
大规模网站架构大规模网站架构
大规模网站架构
 
Comment System of 56.com
Comment System of 56.comComment System of 56.com
Comment System of 56.com
 
云梯的多Namenode和跨机房之路
云梯的多Namenode和跨机房之路云梯的多Namenode和跨机房之路
云梯的多Namenode和跨机房之路
 
Redis分享
Redis分享Redis分享
Redis分享
 
Cassandra Technical and history overview
Cassandra Technical and history overviewCassandra Technical and history overview
Cassandra Technical and history overview
 
Redis介绍
Redis介绍Redis介绍
Redis介绍
 
Cephfs架构解读和测试分析
Cephfs架构解读和测试分析Cephfs架构解读和测试分析
Cephfs架构解读和测试分析
 
Redis
RedisRedis
Redis
 
Ocean base 千亿级海量数据库-日照
Ocean base 千亿级海量数据库-日照Ocean base 千亿级海量数据库-日照
Ocean base 千亿级海量数据库-日照
 
基于My sql的分布式数据库实践
基于My sql的分布式数据库实践基于My sql的分布式数据库实践
基于My sql的分布式数据库实践
 
基于MySQL的分布式数据库实践
基于MySQL的分布式数据库实践基于MySQL的分布式数据库实践
基于MySQL的分布式数据库实践
 

More from iammutex

Realtime hadoopsigmod2011
Realtime hadoopsigmod2011Realtime hadoopsigmod2011
Realtime hadoopsigmod2011
iammutex
 

More from iammutex (20)

Scaling Instagram
Scaling InstagramScaling Instagram
Scaling Instagram
 
MongoDB 在盛大大数据量下的应用
MongoDB 在盛大大数据量下的应用MongoDB 在盛大大数据量下的应用
MongoDB 在盛大大数据量下的应用
 
8 minute MongoDB tutorial slide
8 minute MongoDB tutorial slide8 minute MongoDB tutorial slide
8 minute MongoDB tutorial slide
 
skip list
skip listskip list
skip list
 
Thoughts on Transaction and Consistency Models
Thoughts on Transaction and Consistency ModelsThoughts on Transaction and Consistency Models
Thoughts on Transaction and Consistency Models
 
Rethink db&tokudb调研测试报告
Rethink db&tokudb调研测试报告Rethink db&tokudb调研测试报告
Rethink db&tokudb调研测试报告
 
Introduction to couchdb
Introduction to couchdbIntroduction to couchdb
Introduction to couchdb
 
What every data programmer needs to know about disks
What every data programmer needs to know about disksWhat every data programmer needs to know about disks
What every data programmer needs to know about disks
 
Ooredis
OoredisOoredis
Ooredis
 
Ooredis
OoredisOoredis
Ooredis
 
redis运维之道
redis运维之道redis运维之道
redis运维之道
 
Realtime hadoopsigmod2011
Realtime hadoopsigmod2011Realtime hadoopsigmod2011
Realtime hadoopsigmod2011
 
[译]No sql生态系统
[译]No sql生态系统[译]No sql生态系统
[译]No sql生态系统
 
Couchdb + Membase = Couchbase
Couchdb + Membase = CouchbaseCouchdb + Membase = Couchbase
Couchdb + Membase = Couchbase
 
Redis cluster
Redis clusterRedis cluster
Redis cluster
 
Redis cluster
Redis clusterRedis cluster
Redis cluster
 
Hadoop introduction berlin buzzwords 2011
Hadoop introduction   berlin buzzwords 2011Hadoop introduction   berlin buzzwords 2011
Hadoop introduction berlin buzzwords 2011
 
Couchdb and me
Couchdb and meCouchdb and me
Couchdb and me
 
No sql but even less security
No sql but even less securityNo sql but even less security
No sql but even less security
 
10 Key MongoDB Performance Indicators
10 Key MongoDB Performance Indicators  10 Key MongoDB Performance Indicators
10 Key MongoDB Performance Indicators
 

redis 适用场景与实现

  • 2. 主题  数据类型与实现  持久化与复制  容量规划  适用场景  相关参数与优化
  • 3. redis 概述 a disk backed in-memory database 高性能网络接口 + 数据结构集合
  • 4. 网络接口  单线程 IO 复用  aeEvent 封装 IO 与定时器  默认支持 10K 连接 AE_SETSIZE (1024*10)  why not multi-thread  cache coherency  complexity with locks  Deadlocks
  • 5. redis 数据类型 • string • hash • list • set • sorted set
  • 6. redis 基础数据结构 • redisObject 数据指针 + type + encoding • 内存容量占用 ( 不包含数据本身 ) 16 字节( 64 位 , 不开启 vm ) 40 字节 ( 64 位,开启 vm) redis 里面所有存储对象 (key,value) 都 redisObject 类型 • 案例 set,get,incr 等
  • 7. redis list - 双向链表 • 内存占用 list 每个元素占用内存 listNode( 前后指针,数据节点指针 ) 数据本身指针 (robj) 24 字节 + 16 字节 = 40 字节 40 字节 + 元素本身大小  相关参数 list-max-ziplist-entries list-max-ziplist-value • 使用场景 关注列表,粉丝列表 ...
  • 8. redis hash • 数据结构实现 zipmap & real hashmap • 内存容量占用 zipmap 更省内存  相关参数 hash-max-zipmap-entries hash-max-zipmap-value • 使用场景 用户计数器
  • 9. redis set & sorted set • set 数据结构实现 hashmap (value 为 NULL) • sorted set 数据结构实现 hashmap(element->score) skiplist ( 按 element 插入有序 )  相关参数 set-max-intset-entries • 使用场景 用户发件箱 mid
  • 10. hash & rehash  双 hash 表做 rehash  预规划减小 rehash 次数  rehash 时每次操作迁移一个 bucket  相关参数 DICT_HT_INITIAL_SIZE 4 REDIS_HT_MINFILL 10 Activerehashing
  • 11. redis 持久化机制 • snapshot save 参数 • aof appendfsync 参数 • vm vm is not the way to go for the future slow restarts,slow saving,slow replication,complex code • diskstore key,value stored on disk memory works as a cache ( read through,write back)
  • 12. redis 复制 • 实现机制 快照同步 • 存在的问题 无增量复制 slave 表重建
  • 13. redis 容量规划与建议 • 业务需要哪几种数据类型 • 预先做好容量规划 • sharding 策略与 HA rolling/pre-sharding/ 双写 / 多 slave • 使用 pipeline 减小网络 IO
  • 14. redis 适用场景 • MC + MySQL 不能完全解决 web2.0 的需求 mc 穿透问题,跨机房问题 ,... • 众多开源 nosql 百花齐放 Cassandra,Riak,Voldemort Hbase CouchDB,MongoDB ... • what we need – 小数据量,高速读写访问 – 大数据量,有明显热点数据 – 大数据量,无热点数据
  • 16. 参数小结 • vm-enabled • maxmemory • save/appendfsync • hash-max-zipmap-entries • hash-max-zipmap-value
  • 17. 参数小结 (> 2.2)  list-max-ziplist-entries  list-max-ziplist-value  Set-max-intset-entries
  • 18. 源码中部分参数 • DICT_HT_INITIAL_SIZE 4 • REDIS_HT_MINFILL 10 • AE_SETSIZE (1024*10) • REDIS_SHARED_INTEGERS 10000
  • 19. 小结 • redis 最佳使用方式是 in-memory • 根据业务选择合适数据类型 • 做好容量规划以及 pre-sharding • 不要过度依赖复制和持久化 • 使用 pipeline 减小网络 IO • 先了解实现后使用,如 pubsub,scripting • no one-size-fit-all product, understanding the tradeoffs
  • 20. Thanks 欢迎随时交流: 我的微博 : @ 摇摆巴赫 http://t.sina.com.cn/bachmozart 我的博客: http://www.tianqidev.com/