SlideShare a Scribd company logo
1 of 93
Download to read offline
BeansDB 设计与实现

                       刘洪清
                Davies@douban.com
                      2011/4/8



11年4月8日星期五
•需求

             • 设计

             • 实现

             • 回顾

11年4月8日星期五
产品需求




11年4月8日星期五
产品需求
             • 图片
             • 头像,个人相册, 图片墙
             • 电台单曲



11年4月8日星期五
产品需求
             • 图片
             • 头像,个人相册, 图片墙
             • 电台单曲
             • 长文本
             • 评论,日记,作品,博客, 小组话题
             • 用户属性, 条目属性
11年4月8日星期五
技术需求




11年4月8日星期五
技术需求
             • 存储大量小文件(字段)




11年4月8日星期五
技术需求
             • 存储大量小文件(字段)
             • 可扩展性




11年4月8日星期五
技术需求
             • 存储大量小文件(字段)
             • 可扩展性
             • 可靠性



11年4月8日星期五
技术需求
             • 存储大量小文件(字段)
             • 可扩展性
             • 可靠性
             • 可用性


11年4月8日星期五
技术需求
             • 存储大量小文件(字段)
             • 可扩展性
             • 可靠性
             • 可用性
             • 一致性

11年4月8日星期五
技术需求
             • 存储大量小文件(字段)
             • 可扩展性
             • 可靠性
             • 可用性
             • 一致性
             • 低成本
11年4月8日星期五
两个原则




11年4月8日星期五
两个原则

             • KISS



11年4月8日星期五
两个原则

             • KISS
             • 尽量复用开源方案


11年4月8日星期五
初步方案




11年4月8日星期五
初步方案
         • 存储大量小文件
             • key/value, get/set/delete
             • hash, bucket



11年4月8日星期五
初步方案
         • 存储大量小文件
             • key/value, get/set/delete
             • hash, bucket
         • 可扩展性
             • 独立存储节点,独立数据目录
             • bucket 扩展
11年4月8日星期五
初步方案
   Node1     Node2        Node3           Node4




             set a                get b


                     Clients


11年4月8日星期五
深入设计




11年4月8日星期五
深入设计
             • 可靠性
             • 多机冗余(N=3)
             • 同步写, 依次读




11年4月8日星期五
深入设计
             • 可靠性
             • 多机冗余(N=3)
             • 同步写, 依次读
             • 一致性
             • 最终一致性
             • Hash Tree 同步 (外部脚本)
             • 冲突: 独立版本号+更新时间
11年4月8日星期五
Node1      Node2        Node3   Node4



               Proxy 1



                Clients

11年4月8日星期五
Node1         Node2           Node3   Node4



                     Proxy 1

             set a

                      Clients

11年4月8日星期五
Node1         Node2           Node3     Node4



                     Proxy 1

             set a                get b

                      Clients

11年4月8日星期五
Sync with HT



  Node1            Node2          Node3     Node4



                       Proxy 1

               set a                get b

                        Clients

11年4月8日星期五
Sync with HT



  Node1            Node2          Node3     Node4



                       Proxy 1
                                            Node5
               set a                get b

                        Clients

11年4月8日星期五
Sync with HT



  Node1            Node2          Node3     Node4



                       Proxy 1
                                            Node5
               set a                get b

                        Clients

11年4月8日星期五
Sync with HT



  Node1            Node2          Node3     Node4
                                                Sync


                       Proxy 1
                                            Node5
               set a                get b

                        Clients

11年4月8日星期五
Sync with HT



  Node1            Node2          Node3     Node4
                                                Sync


                       Proxy 1
                                            Node5
               set a                get b

                        Clients

11年4月8日星期五
Sync with HT



  Node1            Node2          Node3     Node4
                                                Sync


                       Proxy 1
                                            Node5
               set a                get b

                        Clients

11年4月8日星期五
存储节点实现




11年4月8日星期五
存储节点实现
             • Key/Value存储引擎
              • ACID(宽松)




11年4月8日星期五
存储节点实现
             • Key/Value存储引擎
              • ACID(宽松)
             • 网络协议
              • memcache 协议



11年4月8日星期五
存储节点实现
             • Key/Value存储引擎
              • ACID(宽松)
             • 网络协议
              • memcache 协议
             • 同步接口
              • Hash Tree(哈希树)

11年4月8日星期五
存储节点实现
             • Key/Value存储引擎
              • ACID(宽松)
             • 网络协议
              • memcache 协议
             • 同步接口
              • Hash Tree(哈希树)
             • 多线程模型
11年4月8日星期五
存储引擎




11年4月8日星期五
存储引擎
             • Tokyo Cabinet
              • 简单, 轻量, 小数据时性能不错
              • crash, 大数据量时性能不好




11年4月8日星期五
存储引擎
             • Tokyo Cabinet
              • 简单, 轻量, 小数据时性能不错
              • crash, 大数据量时性能不好
             • Berkeley DB
              • 过重, 存储稍大的文件性能不好


11年4月8日星期五
存储引擎
             • Tokyo Cabinet
              • 简单, 轻量, 小数据时性能不错
              • crash, 大数据量时性能不好
             • Berkeley DB
              • 过重, 存储稍大的文件性能不好
             • Bitcask
              • 内存索引 + 日志结构数据文件
11年4月8日星期五
内存索引
                      追加写入




数据格式                Bitcask

11年4月8日星期五
内存索引
                      追加写入

                     hint




数据格式                Bitcask

11年4月8日星期五
优化Bitcask




11年4月8日星期五
优化Bitcask
             • 每bucket一个Bitcask




11年4月8日星期五
优化Bitcask
             • 每bucket一个Bitcask
             • 数据空间
              • 256 对齐, 最多256个, 自动压缩
              • 在线GC, 外部控制



11年4月8日星期五
优化Bitcask
             • 每bucket一个Bitcask
             • 数据空间
              • 256 对齐, 最多256个, 自动压缩
              • 在线GC, 外部控制
             • 启动时间
              • hint 文件, QuickLZ L3压缩
              • 多线程
11年4月8日星期五
Hash Tree
                  0     Node
                  1                  key1        Key
                                     key2      Version
                  2                  key3       Hash
      Root                           key4      Position
             0    3
                      Hash(Key) = 0x02f93fea
   Bucket 0                      0
                  e                     Hash
                                 1
                  f                     Count


11年4月8日星期五
Hash Tree优化




11年4月8日星期五
Hash Tree优化
             • Hash Table




11年4月8日星期五
Hash Tree优化
             • Hash Table
             • Node, Item 连续存放
              • {1}{16}{256}
              • {Size, Count, [{Ver, Hash, Pos, Key}, ....]
              • 128 个自动分裂


11年4月8日星期五
Hash Tree优化
             • Hash Table
             • Node, Item 连续存放
              • {1}{16}{256}
              • {Size, Count, [{Ver, Hash, Pos, Key}, ....]
              • 128 个自动分裂
             • 重新编码key
              • /topic/1234567/body=>{/topic/%d/body,
                123}
11年4月8日星期五
同步过程
    节点 A




    节点 B

    客户端
                    时间


11年4月8日星期五
同步过程
    节点 A      V1




    节点 B       V1


    客户端      Set
                           时间


11年4月8日星期五
同步过程
    节点 A      V1    V1




    节点 B       V1


    客户端      Set
                           时间


11年4月8日星期五
同步过程
    节点 A      V1    V1




    节点 B       V1     V2


    客户端      Set    Set
                           时间


11年4月8日星期五
同步过程
    节点 A      V1    V1       V2


                           Sync

    节点 B       V1     V2


    客户端      Set    Set
                                  时间


11年4月8日星期五
同步过程
    节点 A      V1    V1       V2


                           Sync

    节点 B       V1     V2          V2


    客户端      Set    Set
                                       时间


11年4月8日星期五
同步过程
    节点 A      V1    V1       V2


                           Sync

    节点 B       V1     V2          V2


    客户端      Set    Set     Get
                                       时间


11年4月8日星期五
同步过程
    节点 A      V1    V1       V2                 V-3


                           Sync

    节点 B       V1     V2          V2


    客户端      Set    Set     Get        Delete
                                                      时间


11年4月8日星期五
同步过程
    节点 A      V1    V1       V2                 V-3


                           Sync                 Sync

    节点 B       V1     V2          V2            V-3


    客户端      Set    Set     Get        Delete
                                                       时间


11年4月8日星期五
同步过程
    节点 A      V1    V1       V2                 V-3


                           Sync                 Sync

    节点 B       V1     V2          V2            V-3


    客户端      Set    Set     Get        Delete          GC
                                                            时间


11年4月8日星期五
网络协议




11年4月8日星期五
网络协议
             • memcache 文本协议
              • 复用memcached的网络协议层代码
              • 有大量客户端可用



11年4月8日星期五
网络协议
             • memcache 文本协议
              • 复用memcached的网络协议层代码
              • 有大量客户端可用
             • 适当扩展
              • @fff, ?xxxx, flush_all
              • get_multi, set_multi, delete_multi
11年4月8日星期五
IO与多线程模型




11年4月8日星期五
IO与多线程模型
             • 并发与IO
             • 并发连接多, 并发请求少
             • 异步网络IO, 同步磁盘IO




11年4月8日星期五
IO与多线程模型
             • 并发与IO
             • 并发连接多, 并发请求少
             • 异步网络IO, 同步磁盘IO
             • 多线程模型
             • 多个连接与线程绑定
             • 半同步, 半异步
             • leader follower
11年4月8日星期五
Leader-Follower模型
             Leader     try to lock

                       wait for event
             Mutex
                      remove event fd

                         unlock

         Follower do the work (blocking)


11年4月8日星期五
Proxy 实现




11年4月8日星期五
Proxy 实现
             • Go 实现
              • 易于实现高并发应用, 性能可接受




11年4月8日星期五
Proxy 实现
             • Go 实现
              • 易于实现高并发应用, 性能可接受
             • 自动路由
              • 根据 Merkle Tree得到数据分布
              • 更高可用性


11年4月8日星期五
Proxy 实现
             • Go 实现
              • 易于实现高并发应用, 性能可接受
             • 自动路由
              • 根据 Merkle Tree得到数据分布
              • 更高可用性
             • 负载均衡
              • 减少IO慢的节点的请求量
11年4月8日星期五
版本历史




11年4月8日星期五
版本历史
             • 0.1, like MogileFS           2008.8
              • WebDAV, inotify, sync, client




11年4月8日星期五
版本历史
             • 0.1, like MogileFS            2008.8
              • WebDAV, inotify, sync, client
             • 0.2, like TokyoTrant             2008.12
              • TC, HTree, sync, mc client



11年4月8日星期五
版本历史
             • 0.1, like MogileFS            2008.8
              • WebDAV, inotify, sync, client
             • 0.2, like TokyoTrant             2008.12
              • TC, HTree, sync, mc client
             • 0.3, like memcachedb             2009.6
              • HTree in TC, on Disk

11年4月8日星期五
版本历史(2)




11年4月8日星期五
版本历史(2)

             • 0.4        2010.2
              • proxy



11年4月8日星期五
版本历史(2)

             • 0.4                         2010.2
              • proxy
             • 0.5   reload                2010.10
              • Bitcask, Leader/Follower

11年4月8日星期五
实际部署案例(1)




11年4月8日星期五
实际部署案例(1)
             • 数据量
             • 图片, 音频: 1k到20M
             • 310M x 3 = 930 M
             • 590G x 16 x 3 = 28T
             • 17 个节点, 约 50 块SATA硬盘



11年4月8日星期五
实际部署案例(1)
             • 数据量
             • 图片, 音频: 1k到20M
             • 310M x 3 = 930 M
             • 590G x 16 x 3 = 28T
             • 17 个节点, 约 50 块SATA硬盘
             • 性能:
             • 250 qps左右, 有CDN
             • Med/Avg/90%/99%: 16/29/69/232 ms
11年4月8日星期五
各节点数据分布




11年4月8日星期五
线上运行状态截图 (图片, mp3, 3亿)




11年4月8日星期五
11年4月8日星期五
实际部署案例(2)




11年4月8日星期五
实际部署案例(2)
             • 数据量
             • 文本字段: 100到100k
             • 550M x 3 = 1.65 B
             • 50G x 16 x 3 = 2.4T
             • 13 个节点, 约 13 块SATA硬盘



11年4月8日星期五
实际部署案例(2)
             • 数据量
             • 文本字段: 100到100k
             • 550M x 3 = 1.65 B
             • 50G x 16 x 3 = 2.4T
             • 13 个节点, 约 13 块SATA硬盘
             • 性能:
             • 200 qps左右, 有memcached作缓存
             • Med/Avg/90%/99%: 1/14/15/104 ms
11年4月8日星期五
Thanks!
              Q/A ?


11年4月8日星期五
原型开发




11年4月8日星期五
原型开发
             • 原型开发
             • 快速验证想法




11年4月8日星期五
原型开发
             • 原型开发
              • 快速验证想法
             • Python
              • Cython 扩展


11年4月8日星期五
原型开发
             • 原型开发
              • 快速验证想法
             • Python
              • Cython 扩展
             • Go
              • cgo 扩展, 容易开发高并发应用
11年4月8日星期五

More Related Content

Featured

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by HubspotMarius Sescu
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTExpeed Software
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsPixeldarts
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthThinkNow
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfmarketingartwork
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024Neil Kimberley
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsKurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summarySpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentLily Ray
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best PracticesVit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project managementMindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...RachelPearson36
 

Featured (20)

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPT
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 

Qcon 2011:Beansdb 的设计与实现

  • 1. BeansDB 设计与实现 刘洪清 Davies@douban.com 2011/4/8 11年4月8日星期五
  • 2. •需求 • 设计 • 实现 • 回顾 11年4月8日星期五
  • 4. 产品需求 • 图片 • 头像,个人相册, 图片墙 • 电台单曲 11年4月8日星期五
  • 5. 产品需求 • 图片 • 头像,个人相册, 图片墙 • 电台单曲 • 长文本 • 评论,日记,作品,博客, 小组话题 • 用户属性, 条目属性 11年4月8日星期五
  • 7. 技术需求 • 存储大量小文件(字段) 11年4月8日星期五
  • 8. 技术需求 • 存储大量小文件(字段) • 可扩展性 11年4月8日星期五
  • 9. 技术需求 • 存储大量小文件(字段) • 可扩展性 • 可靠性 11年4月8日星期五
  • 10. 技术需求 • 存储大量小文件(字段) • 可扩展性 • 可靠性 • 可用性 11年4月8日星期五
  • 11. 技术需求 • 存储大量小文件(字段) • 可扩展性 • 可靠性 • 可用性 • 一致性 11年4月8日星期五
  • 12. 技术需求 • 存储大量小文件(字段) • 可扩展性 • 可靠性 • 可用性 • 一致性 • 低成本 11年4月8日星期五
  • 14. 两个原则 • KISS 11年4月8日星期五
  • 15. 两个原则 • KISS • 尽量复用开源方案 11年4月8日星期五
  • 17. 初步方案 • 存储大量小文件 • key/value, get/set/delete • hash, bucket 11年4月8日星期五
  • 18. 初步方案 • 存储大量小文件 • key/value, get/set/delete • hash, bucket • 可扩展性 • 独立存储节点,独立数据目录 • bucket 扩展 11年4月8日星期五
  • 19. 初步方案 Node1 Node2 Node3 Node4 set a get b Clients 11年4月8日星期五
  • 21. 深入设计 • 可靠性 • 多机冗余(N=3) • 同步写, 依次读 11年4月8日星期五
  • 22. 深入设计 • 可靠性 • 多机冗余(N=3) • 同步写, 依次读 • 一致性 • 最终一致性 • Hash Tree 同步 (外部脚本) • 冲突: 独立版本号+更新时间 11年4月8日星期五
  • 23. Node1 Node2 Node3 Node4 Proxy 1 Clients 11年4月8日星期五
  • 24. Node1 Node2 Node3 Node4 Proxy 1 set a Clients 11年4月8日星期五
  • 25. Node1 Node2 Node3 Node4 Proxy 1 set a get b Clients 11年4月8日星期五
  • 26. Sync with HT Node1 Node2 Node3 Node4 Proxy 1 set a get b Clients 11年4月8日星期五
  • 27. Sync with HT Node1 Node2 Node3 Node4 Proxy 1 Node5 set a get b Clients 11年4月8日星期五
  • 28. Sync with HT Node1 Node2 Node3 Node4 Proxy 1 Node5 set a get b Clients 11年4月8日星期五
  • 29. Sync with HT Node1 Node2 Node3 Node4 Sync Proxy 1 Node5 set a get b Clients 11年4月8日星期五
  • 30. Sync with HT Node1 Node2 Node3 Node4 Sync Proxy 1 Node5 set a get b Clients 11年4月8日星期五
  • 31. Sync with HT Node1 Node2 Node3 Node4 Sync Proxy 1 Node5 set a get b Clients 11年4月8日星期五
  • 33. 存储节点实现 • Key/Value存储引擎 • ACID(宽松) 11年4月8日星期五
  • 34. 存储节点实现 • Key/Value存储引擎 • ACID(宽松) • 网络协议 • memcache 协议 11年4月8日星期五
  • 35. 存储节点实现 • Key/Value存储引擎 • ACID(宽松) • 网络协议 • memcache 协议 • 同步接口 • Hash Tree(哈希树) 11年4月8日星期五
  • 36. 存储节点实现 • Key/Value存储引擎 • ACID(宽松) • 网络协议 • memcache 协议 • 同步接口 • Hash Tree(哈希树) • 多线程模型 11年4月8日星期五
  • 38. 存储引擎 • Tokyo Cabinet • 简单, 轻量, 小数据时性能不错 • crash, 大数据量时性能不好 11年4月8日星期五
  • 39. 存储引擎 • Tokyo Cabinet • 简单, 轻量, 小数据时性能不错 • crash, 大数据量时性能不好 • Berkeley DB • 过重, 存储稍大的文件性能不好 11年4月8日星期五
  • 40. 存储引擎 • Tokyo Cabinet • 简单, 轻量, 小数据时性能不错 • crash, 大数据量时性能不好 • Berkeley DB • 过重, 存储稍大的文件性能不好 • Bitcask • 内存索引 + 日志结构数据文件 11年4月8日星期五
  • 41. 内存索引 追加写入 数据格式 Bitcask 11年4月8日星期五
  • 42. 内存索引 追加写入 hint 数据格式 Bitcask 11年4月8日星期五
  • 44. 优化Bitcask • 每bucket一个Bitcask 11年4月8日星期五
  • 45. 优化Bitcask • 每bucket一个Bitcask • 数据空间 • 256 对齐, 最多256个, 自动压缩 • 在线GC, 外部控制 11年4月8日星期五
  • 46. 优化Bitcask • 每bucket一个Bitcask • 数据空间 • 256 对齐, 最多256个, 自动压缩 • 在线GC, 外部控制 • 启动时间 • hint 文件, QuickLZ L3压缩 • 多线程 11年4月8日星期五
  • 47. Hash Tree 0 Node 1 key1 Key key2 Version 2 key3 Hash Root key4 Position 0 3 Hash(Key) = 0x02f93fea Bucket 0 0 e Hash 1 f Count 11年4月8日星期五
  • 49. Hash Tree优化 • Hash Table 11年4月8日星期五
  • 50. Hash Tree优化 • Hash Table • Node, Item 连续存放 • {1}{16}{256} • {Size, Count, [{Ver, Hash, Pos, Key}, ....] • 128 个自动分裂 11年4月8日星期五
  • 51. Hash Tree优化 • Hash Table • Node, Item 连续存放 • {1}{16}{256} • {Size, Count, [{Ver, Hash, Pos, Key}, ....] • 128 个自动分裂 • 重新编码key • /topic/1234567/body=>{/topic/%d/body, 123} 11年4月8日星期五
  • 52. 同步过程 节点 A 节点 B 客户端 时间 11年4月8日星期五
  • 53. 同步过程 节点 A V1 节点 B V1 客户端 Set 时间 11年4月8日星期五
  • 54. 同步过程 节点 A V1 V1 节点 B V1 客户端 Set 时间 11年4月8日星期五
  • 55. 同步过程 节点 A V1 V1 节点 B V1 V2 客户端 Set Set 时间 11年4月8日星期五
  • 56. 同步过程 节点 A V1 V1 V2 Sync 节点 B V1 V2 客户端 Set Set 时间 11年4月8日星期五
  • 57. 同步过程 节点 A V1 V1 V2 Sync 节点 B V1 V2 V2 客户端 Set Set 时间 11年4月8日星期五
  • 58. 同步过程 节点 A V1 V1 V2 Sync 节点 B V1 V2 V2 客户端 Set Set Get 时间 11年4月8日星期五
  • 59. 同步过程 节点 A V1 V1 V2 V-3 Sync 节点 B V1 V2 V2 客户端 Set Set Get Delete 时间 11年4月8日星期五
  • 60. 同步过程 节点 A V1 V1 V2 V-3 Sync Sync 节点 B V1 V2 V2 V-3 客户端 Set Set Get Delete 时间 11年4月8日星期五
  • 61. 同步过程 节点 A V1 V1 V2 V-3 Sync Sync 节点 B V1 V2 V2 V-3 客户端 Set Set Get Delete GC 时间 11年4月8日星期五
  • 63. 网络协议 • memcache 文本协议 • 复用memcached的网络协议层代码 • 有大量客户端可用 11年4月8日星期五
  • 64. 网络协议 • memcache 文本协议 • 复用memcached的网络协议层代码 • 有大量客户端可用 • 适当扩展 • @fff, ?xxxx, flush_all • get_multi, set_multi, delete_multi 11年4月8日星期五
  • 66. IO与多线程模型 • 并发与IO • 并发连接多, 并发请求少 • 异步网络IO, 同步磁盘IO 11年4月8日星期五
  • 67. IO与多线程模型 • 并发与IO • 并发连接多, 并发请求少 • 异步网络IO, 同步磁盘IO • 多线程模型 • 多个连接与线程绑定 • 半同步, 半异步 • leader follower 11年4月8日星期五
  • 68. Leader-Follower模型 Leader try to lock wait for event Mutex remove event fd unlock Follower do the work (blocking) 11年4月8日星期五
  • 70. Proxy 实现 • Go 实现 • 易于实现高并发应用, 性能可接受 11年4月8日星期五
  • 71. Proxy 实现 • Go 实现 • 易于实现高并发应用, 性能可接受 • 自动路由 • 根据 Merkle Tree得到数据分布 • 更高可用性 11年4月8日星期五
  • 72. Proxy 实现 • Go 实现 • 易于实现高并发应用, 性能可接受 • 自动路由 • 根据 Merkle Tree得到数据分布 • 更高可用性 • 负载均衡 • 减少IO慢的节点的请求量 11年4月8日星期五
  • 74. 版本历史 • 0.1, like MogileFS 2008.8 • WebDAV, inotify, sync, client 11年4月8日星期五
  • 75. 版本历史 • 0.1, like MogileFS 2008.8 • WebDAV, inotify, sync, client • 0.2, like TokyoTrant 2008.12 • TC, HTree, sync, mc client 11年4月8日星期五
  • 76. 版本历史 • 0.1, like MogileFS 2008.8 • WebDAV, inotify, sync, client • 0.2, like TokyoTrant 2008.12 • TC, HTree, sync, mc client • 0.3, like memcachedb 2009.6 • HTree in TC, on Disk 11年4月8日星期五
  • 78. 版本历史(2) • 0.4 2010.2 • proxy 11年4月8日星期五
  • 79. 版本历史(2) • 0.4 2010.2 • proxy • 0.5 reload 2010.10 • Bitcask, Leader/Follower 11年4月8日星期五
  • 81. 实际部署案例(1) • 数据量 • 图片, 音频: 1k到20M • 310M x 3 = 930 M • 590G x 16 x 3 = 28T • 17 个节点, 约 50 块SATA硬盘 11年4月8日星期五
  • 82. 实际部署案例(1) • 数据量 • 图片, 音频: 1k到20M • 310M x 3 = 930 M • 590G x 16 x 3 = 28T • 17 个节点, 约 50 块SATA硬盘 • 性能: • 250 qps左右, 有CDN • Med/Avg/90%/99%: 16/29/69/232 ms 11年4月8日星期五
  • 84. 线上运行状态截图 (图片, mp3, 3亿) 11年4月8日星期五
  • 87. 实际部署案例(2) • 数据量 • 文本字段: 100到100k • 550M x 3 = 1.65 B • 50G x 16 x 3 = 2.4T • 13 个节点, 约 13 块SATA硬盘 11年4月8日星期五
  • 88. 实际部署案例(2) • 数据量 • 文本字段: 100到100k • 550M x 3 = 1.65 B • 50G x 16 x 3 = 2.4T • 13 个节点, 约 13 块SATA硬盘 • 性能: • 200 qps左右, 有memcached作缓存 • Med/Avg/90%/99%: 1/14/15/104 ms 11年4月8日星期五
  • 89. Thanks! Q/A ? 11年4月8日星期五
  • 91. 原型开发 • 原型开发 • 快速验证想法 11年4月8日星期五
  • 92. 原型开发 • 原型开发 • 快速验证想法 • Python • Cython 扩展 11年4月8日星期五
  • 93. 原型开发 • 原型开发 • 快速验证想法 • Python • Cython 扩展 • Go • cgo 扩展, 容易开发高并发应用 11年4月8日星期五