MongoDB 分布式架构演进 - PostgreS · • 通过raft 协议选举出Primary •...
Transcript of MongoDB 分布式架构演进 - PostgreS · • 通过raft 协议选举出Primary •...
![Page 1: MongoDB 分布式架构演进 - PostgreS · • 通过raft 协议选举出Primary • 所有写请求都写到Primary,并同步到Secondary • 当Primary 故障时,自动选出新的Primary](https://reader033.fdocuments.us/reader033/viewer/2022042507/5f427002aa2f56107a77f685/html5/thumbnails/1.jpg)
Postgres Conference China 2016中国用户大会Postgres Conference China 2016中国用户大会
MongoDB 分布式架构演进
张友东(林青)
阿里云数据库技术团队 201
6Pos
tgre
s中国
用户
大会
![Page 2: MongoDB 分布式架构演进 - PostgreS · • 通过raft 协议选举出Primary • 所有写请求都写到Primary,并同步到Secondary • 当Primary 故障时,自动选出新的Primary](https://reader033.fdocuments.us/reader033/viewer/2022042507/5f427002aa2f56107a77f685/html5/thumbnails/2.jpg)
Postgres Conference China 2016中国用户大会 2
016P
ostg
res中
国用
户大
会
![Page 3: MongoDB 分布式架构演进 - PostgreS · • 通过raft 协议选举出Primary • 所有写请求都写到Primary,并同步到Secondary • 当Primary 故障时,自动选出新的Primary](https://reader033.fdocuments.us/reader033/viewer/2022042507/5f427002aa2f56107a77f685/html5/thumbnails/3.jpg)
Postgres Conference China 2016中国用户大会
MongoDB 核心优势
•文档模型灵活灵活
•复制集高可用高可用
•分片集群可扩展可扩展
201
6Pos
tgre
s中国
用户
大会
![Page 4: MongoDB 分布式架构演进 - PostgreS · • 通过raft 协议选举出Primary • 所有写请求都写到Primary,并同步到Secondary • 当Primary 故障时,自动选出新的Primary](https://reader033.fdocuments.us/reader033/viewer/2022042507/5f427002aa2f56107a77f685/html5/thumbnails/4.jpg)
Postgres Conference China 2016中国用户大会
文档模型
– 接近真实对象模型,对开发人员友好
– Schema free,适应灵活多变的需求,快速迭代
– 数组、内嵌文档支持,数据聚集,提升读写性能 2
016P
ostg
res中
国用
户大
会
![Page 5: MongoDB 分布式架构演进 - PostgreS · • 通过raft 协议选举出Primary • 所有写请求都写到Primary,并同步到Secondary • 当Primary 故障时,自动选出新的Primary](https://reader033.fdocuments.us/reader033/viewer/2022042507/5f427002aa2f56107a77f685/html5/thumbnails/5.jpg)
Postgres Conference China 2016中国用户大会
今天不谈文档模型
201
6Pos
tgre
s中国
用户
大会
![Page 6: MongoDB 分布式架构演进 - PostgreS · • 通过raft 协议选举出Primary • 所有写请求都写到Primary,并同步到Secondary • 当Primary 故障时,自动选出新的Primary](https://reader033.fdocuments.us/reader033/viewer/2022042507/5f427002aa2f56107a77f685/html5/thumbnails/6.jpg)
Postgres Conference China 2016中国用户大会
主要内容
• 如何保证数据高可靠?
• 如何保证服务高可用?
• 如何实现水平扩展?
单节点
单节点
主备主备复制集
复制集
分片集群分片集群
201
6Pos
tgre
s中国
用户
大会
![Page 7: MongoDB 分布式架构演进 - PostgreS · • 通过raft 协议选举出Primary • 所有写请求都写到Primary,并同步到Secondary • 当Primary 故障时,自动选出新的Primary](https://reader033.fdocuments.us/reader033/viewer/2022042507/5f427002aa2f56107a77f685/html5/thumbnails/7.jpg)
Postgres Conference China 2016中国用户大会
单节点
Mongod
ClientClient
ClientClient
ClientClient
• 数据单点• 服务单点
201
6Pos
tgre
s中国
用户
大会
![Page 8: MongoDB 分布式架构演进 - PostgreS · • 通过raft 协议选举出Primary • 所有写请求都写到Primary,并同步到Secondary • 当Primary 故障时,自动选出新的Primary](https://reader033.fdocuments.us/reader033/viewer/2022042507/5f427002aa2f56107a77f685/html5/thumbnails/8.jpg)
Postgres Conference China 2016中国用户大会
主备节点
Master SlaveSlavesync data
• Master 宕机无法服务写请求• 只能容忍一个节点失效
201
6Pos
tgre
s中国
用户
大会
![Page 9: MongoDB 分布式架构演进 - PostgreS · • 通过raft 协议选举出Primary • 所有写请求都写到Primary,并同步到Secondary • 当Primary 故障时,自动选出新的Primary](https://reader033.fdocuments.us/reader033/viewer/2022042507/5f427002aa2f56107a77f685/html5/thumbnails/9.jpg)
Postgres Conference China 2016中国用户大会
一主多备
Master
SlaveSlavesync data
SlaveSlavesync data
• 数据可靠性更高• 扩展读服务能力
201
6Pos
tgre
s中国
用户
大会
![Page 10: MongoDB 分布式架构演进 - PostgreS · • 通过raft 协议选举出Primary • 所有写请求都写到Primary,并同步到Secondary • 当Primary 故障时,自动选出新的Primary](https://reader033.fdocuments.us/reader033/viewer/2022042507/5f427002aa2f56107a77f685/html5/thumbnails/10.jpg)
Postgres Conference China 2016中国用户大会
复制集
• 通过 raft 协议选举出 Primary• 所有写请求都写到 Primary,并同步到 Secondary• 当 Primary 故障时,自动选出新的 Primary 节点
Primary
SecondarySecondaryheartbeat
SecondarySecondary
201
6Pos
tgre
s中国
用户
大会
![Page 11: MongoDB 分布式架构演进 - PostgreS · • 通过raft 协议选举出Primary • 所有写请求都写到Primary,并同步到Secondary • 当Primary 故障时,自动选出新的Primary](https://reader033.fdocuments.us/reader033/viewer/2022042507/5f427002aa2f56107a77f685/html5/thumbnails/11.jpg)
Postgres Conference China 2016中国用户大会
高可用
SecondarySecondary
heartbeat SecondarySecondary
Primary
ClientClient
heartbeat
heartbeat
Client与复制集所有成员保
持心跳,不断检测后端节点状态,发生新选举时,自动 failover
201
6Pos
tgre
s中国
用户
大会
![Page 12: MongoDB 分布式架构演进 - PostgreS · • 通过raft 协议选举出Primary • 所有写请求都写到Primary,并同步到Secondary • 当Primary 故障时,自动选出新的Primary](https://reader033.fdocuments.us/reader033/viewer/2022042507/5f427002aa2f56107a77f685/html5/thumbnails/12.jpg)
Postgres Conference China 2016中国用户大会
写策略
WriteConcern: {w: 1} WriteConcern: {w: 2}
201
6Pos
tgre
s中国
用户
大会
![Page 13: MongoDB 分布式架构演进 - PostgreS · • 通过raft 协议选举出Primary • 所有写请求都写到Primary,并同步到Secondary • 当Primary 故障时,自动选出新的Primary](https://reader033.fdocuments.us/reader033/viewer/2022042507/5f427002aa2f56107a77f685/html5/thumbnails/13.jpg)
Postgres Conference China 2016中国用户大会
选举规则
节点数 大多数
1 1
2 2
3 2
4 3
5 3
6 4
7 4
https://raft.github.io/
201
6Pos
tgre
s中国
用户
大会
![Page 14: MongoDB 分布式架构演进 - PostgreS · • 通过raft 协议选举出Primary • 所有写请求都写到Primary,并同步到Secondary • 当Primary 故障时,自动选出新的Primary](https://reader033.fdocuments.us/reader033/viewer/2022042507/5f427002aa2f56107a77f685/html5/thumbnails/14.jpg)
Postgres Conference China 2016中国用户大会
仲裁节点
Primary
SecondarySecondaryheartbeat
Arbitervote only
Arbiter节点只参与投票,不存储数据
201
6Pos
tgre
s中国
用户
大会
![Page 15: MongoDB 分布式架构演进 - PostgreS · • 通过raft 协议选举出Primary • 所有写请求都写到Primary,并同步到Secondary • 当Primary 故障时,自动选出新的Primary](https://reader033.fdocuments.us/reader033/viewer/2022042507/5f427002aa2f56107a77f685/html5/thumbnails/15.jpg)
Postgres Conference China 2016中国用户大会
选举优先级
Primary
SecondarySecondaryheartbeat
Secondarypriority = 0
机房 A
机房 B
201
6Pos
tgre
s中国
用户
大会
![Page 16: MongoDB 分布式架构演进 - PostgreS · • 通过raft 协议选举出Primary • 所有写请求都写到Primary,并同步到Secondary • 当Primary 故障时,自动选出新的Primary](https://reader033.fdocuments.us/reader033/viewer/2022042507/5f427002aa2f56107a77f685/html5/thumbnails/16.jpg)
Postgres Conference China 2016中国用户大会
隐藏节点
Primary
SecondarySecondaryheartbeat
HiddenHidden
mongodump
对客户端不可见
201
6Pos
tgre
s中国
用户
大会
![Page 17: MongoDB 分布式架构演进 - PostgreS · • 通过raft 协议选举出Primary • 所有写请求都写到Primary,并同步到Secondary • 当Primary 故障时,自动选出新的Primary](https://reader033.fdocuments.us/reader033/viewer/2022042507/5f427002aa2f56107a77f685/html5/thumbnails/17.jpg)
Postgres Conference China 2016中国用户大会
延迟节点
Primary
SecondarySecondaryheartbeat
DelayedDelayed
mongodump
数据落后其他节点一段固定时间
201
6Pos
tgre
s中国
用户
大会
![Page 18: MongoDB 分布式架构演进 - PostgreS · • 通过raft 协议选举出Primary • 所有写请求都写到Primary,并同步到Secondary • 当Primary 故障时,自动选出新的Primary](https://reader033.fdocuments.us/reader033/viewer/2022042507/5f427002aa2f56107a77f685/html5/thumbnails/18.jpg)
Postgres Conference China 2016中国用户大会
复制集的问题
• 存储容量受限于单个 Primary• 写服务能力受限于单个 Primary
Primary
SecondarySecondaryheartbeat
SecondarySecondary
201
6Pos
tgre
s中国
用户
大会
![Page 19: MongoDB 分布式架构演进 - PostgreS · • 通过raft 协议选举出Primary • 所有写请求都写到Primary,并同步到Secondary • 当Primary 故障时,自动选出新的Primary](https://reader033.fdocuments.us/reader033/viewer/2022042507/5f427002aa2f56107a77f685/html5/thumbnails/19.jpg)
Postgres Conference China 2016中国用户大会
客户端分片
SecondarySecondary
SecondarySecondary
Primary
SecondarySecondary
SecondarySecondary
Primary
SecondarySecondary
SecondarySecondary
Primary
ClientClient
Shard1 Shard2 Shard3
hash 路由
201
6Pos
tgre
s中国
用户
大会
![Page 20: MongoDB 分布式架构演进 - PostgreS · • 通过raft 协议选举出Primary • 所有写请求都写到Primary,并同步到Secondary • 当Primary 故障时,自动选出新的Primary](https://reader033.fdocuments.us/reader033/viewer/2022042507/5f427002aa2f56107a77f685/html5/thumbnails/20.jpg)
Postgres Conference China 2016中国用户大会
Proxy分片
Shard1
ClientClient
Shard2
Shard2 Shard3
Mongos hash 路由
201
6Pos
tgre
s中国
用户
大会
![Page 21: MongoDB 分布式架构演进 - PostgreS · • 通过raft 协议选举出Primary • 所有写请求都写到Primary,并同步到Secondary • 当Primary 故障时,自动选出新的Primary](https://reader033.fdocuments.us/reader033/viewer/2022042507/5f427002aa2f56107a77f685/html5/thumbnails/21.jpg)
Postgres Conference China 2016中国用户大会
Proxy分片
Shard1
ClientClient
Shard2
Shard2 Shard3
Mongos
201
6Pos
tgre
s中国
用户
大会
![Page 22: MongoDB 分布式架构演进 - PostgreS · • 通过raft 协议选举出Primary • 所有写请求都写到Primary,并同步到Secondary • 当Primary 故障时,自动选出新的Primary](https://reader033.fdocuments.us/reader033/viewer/2022042507/5f427002aa2f56107a77f685/html5/thumbnails/22.jpg)
Postgres Conference China 2016中国用户大会
可扩展分片
Shard1
ClientClient
Shard2
Shard2 Shard3
Mongos ConfigServer
Chunk (100, 200) Shard1Chunk (200, 300) Shard2Chunk (300, 400) Shard3….
201
6Pos
tgre
s中国
用户
大会
![Page 23: MongoDB 分布式架构演进 - PostgreS · • 通过raft 协议选举出Primary • 所有写请求都写到Primary,并同步到Secondary • 当Primary 故障时,自动选出新的Primary](https://reader033.fdocuments.us/reader033/viewer/2022042507/5f427002aa2f56107a77f685/html5/thumbnails/23.jpg)
Postgres Conference China 2016中国用户大会
分片方式-范围
范围 所在分片
Chunk1 [minKey, ‐75) Shard2
Chunk2 [‐75, 25) Shard1
Chunk3 [25, 175) Shard3
Chunk4 [175, MaxKey] Shard1
• 根据某个字段的值,顺序划分为多个范围,每个范围对应一个 Shard,能很好的支持范围查询
201
6Pos
tgre
s中国
用户
大会
![Page 24: MongoDB 分布式架构演进 - PostgreS · • 通过raft 协议选举出Primary • 所有写请求都写到Primary,并同步到Secondary • 当Primary 故障时,自动选出新的Primary](https://reader033.fdocuments.us/reader033/viewer/2022042507/5f427002aa2f56107a77f685/html5/thumbnails/24.jpg)
Postgres Conference China 2016中国用户大会
分片方式-hash
• 根据某个字段的hash值,顺序划分为多个范围,每个范围对应一个 Shard,能将数据均匀的分布到各个 Shard 2
016P
ostg
res中
国用
户大
会
![Page 25: MongoDB 分布式架构演进 - PostgreS · • 通过raft 协议选举出Primary • 所有写请求都写到Primary,并同步到Secondary • 当Primary 故障时,自动选出新的Primary](https://reader033.fdocuments.us/reader033/viewer/2022042507/5f427002aa2f56107a77f685/html5/thumbnails/25.jpg)
Postgres Conference China 2016中国用户大会
增加、删除 Shard
Shard1
ClientClient
Shard2
Shard2 Shard3
Mongos ConfigServer
Chunk (100, 200) Shard1Chunk (200, 300) Shard2Chunk (300, 400) Shard3….
Shard4 201
6Pos
tgre
s中国
用户
大会
![Page 26: MongoDB 分布式架构演进 - PostgreS · • 通过raft 协议选举出Primary • 所有写请求都写到Primary,并同步到Secondary • 当Primary 故障时,自动选出新的Primary](https://reader033.fdocuments.us/reader033/viewer/2022042507/5f427002aa2f56107a77f685/html5/thumbnails/26.jpg)
Postgres Conference China 2016中国用户大会
自动负载均衡
100 ~ 200100 ~ 200
‐100 ~ 0‐100 ~ 0
100 ~ 200100 ~ 200
100 ~ 200100 ~ 200
300 ~ 400300 ~ 400 500 ~ 600500 ~ 600
Mongos
100 ~ 200100 ~ 200自动迁移
Mongos自动根据各Shard 存储的 Chunk 数量进行负载均衡
201
6Pos
tgre
s中国
用户
大会
![Page 27: MongoDB 分布式架构演进 - PostgreS · • 通过raft 协议选举出Primary • 所有写请求都写到Primary,并同步到Secondary • 当Primary 故障时,自动选出新的Primary](https://reader033.fdocuments.us/reader033/viewer/2022042507/5f427002aa2f56107a77f685/html5/thumbnails/27.jpg)
Postgres Conference China 2016中国用户大会 2
016P
ostg
res中
国用
户大
会
![Page 28: MongoDB 分布式架构演进 - PostgreS · • 通过raft 协议选举出Primary • 所有写请求都写到Primary,并同步到Secondary • 当Primary 故障时,自动选出新的Primary](https://reader033.fdocuments.us/reader033/viewer/2022042507/5f427002aa2f56107a77f685/html5/thumbnails/28.jpg)
Postgres Conference China 2016中国用户大会
广告时间
• MongoDB中文社区mongoing.com
• 阿里云 MongoDB数据库目前已支持3节点复制集,分片集群即将上线。
201
6Pos
tgre
s中国
用户
大会
![Page 29: MongoDB 分布式架构演进 - PostgreS · • 通过raft 协议选举出Primary • 所有写请求都写到Primary,并同步到Secondary • 当Primary 故障时,自动选出新的Primary](https://reader033.fdocuments.us/reader033/viewer/2022042507/5f427002aa2f56107a77f685/html5/thumbnails/29.jpg)
Postgres Conference China 2016中国用户大会Postgres Conference China 2016中国用户大会
Thanks!Q & A
201
6Pos
tgre
s中国
用户
大会