前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >MongoDB读策略之read Preference

MongoDB读策略之read Preference

作者头像
AsiaYe
发布2020-12-29 11:15:04
3K0
发布2020-12-29 11:15:04
举报
文章被收录于专栏:DBA随笔DBA随笔

//

MongoDB读策略之read Preference

//

01

read Preference概念

Read Preference描述MongoDB客户端如何路由读操作到复制集成员。

默认情况下,客户端直接将它的读操作发送到primary成员上,但同时客户端可以定义一个读操作的读取顺序,例如优先读secondary成员。定义这个读取顺序的选项,就是Read Preference。

Read Preference这个选项由read preference mode、tag set以及maxStalenessSeconds参数组成(后两者可选)。其中:

maxStalenessSeconds :代表最大延时时间。单词stale代表旧数据。也就是非旧数据的最大延时时间,最少设置为90s。用官方文档的话来描述,就是:

The read preference maxStalenessSeconds option lets you specify a maximum replication lag, or “staleness”, for reads from secondaries. When a secondary’s estimated staleness exceeds maxStalenessSeconds, the client stops using it for read operations.

意思是如果从服务器的预期延时时间超过了maxStalenessSeconds时间,则不会从它上面读取。

注意:

1、选定除Primary之外的Read Preference可能返回旧的数据,因为复制是异步进行的。

2、Secondary上面的数据可能不能反映最近的写操作

3、Read Preference不影响数据是否可见,客户机可以在写入结果得到确认或已传播到大多数副本集成员之前看到它们。

02

选项

Read Preference常见的模式:

1、primary

默认模式,当前的读操作都从primary上面读。

2、primaryPreferred

多数情况下,读操作从primary读,特殊情况从secondary读

3、secondary

所有操作从secondary上读

4、secondaryPreferred

多数情况下从secondary上读,特殊情况从primary读

5、nearest

从网络延时最低的那个节点读,不管是primary还是secondary

上面说过了,Read Preference这个选项由read preference mode、tag set以及maxStalenessSeconds参数组成,如果一个复制集中的成员member有tag标签,可以通过下面的办法来让read操作定位到带有某个标签的成员上。(当然,Primary Mode不支持标签,因为它只有一个固定的节点。)

代码语言:javascript
复制
[ { "region": "South", "datacenter": "A" }, { } ]     // Find members with both tag values. If none are found, read from any eligible member.
[ { "region": "South" }, { "datacenter": "A" }, { } ] // Find members with the specified region tag. Only if not found, then find members with the specified datacenter tag. If none are found, read from any eligible member.
[ { "datacenter": "A" }, { "region": "South" }, { } ] // Find members with the specified datacenter tag. Only if not found, then find members with the specified region tag. If none are found, read from any eligible member.
[ { "region": "South" }, { } ]                        // Find members with the specified region tag value. If none are found, read from any eligible member.
[ { "datacenter": "A" }, { } ]                        // Find members with the specified datacenter tag value. If none are found, read from any eligible member.
[ { } ]                                               // Find any eligible member.

三、使用方法

MongoDB shell中调用readPreference的方法:

代码语言:javascript
复制
db.collection.find({}).readPref( "secondary", [ { "region": "South" } ] )

连接串中调用readPreference的方法:

复制集

mongodb://db0.example.com,db1.example.com,db2.example.com/?replicaSet=myRepl&readPreference=secondary&maxStalenessSeconds=120

分片集群

mongodb://mongos1.example.com,mongos2.example.com/?readPreference=secondary&maxStalenessSeconds=120

带tag的定义方式:

mongodb://mongos1.example.com,mongos2.example.com/?readPreference=secondary&readPreferenceTags=dc:ny,rack:r1&readPreferenceTags=dc:ny&readPreferenceTags=xxx

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2020-12-23,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 DBA随笔 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
云数据库 MongoDB
腾讯云数据库 MongoDB(TencentDB for MongoDB)是腾讯云基于全球广受欢迎的 MongoDB 打造的高性能 NoSQL 数据库,100%完全兼容 MongoDB 协议,支持跨文档事务,提供稳定丰富的监控管理,弹性可扩展、自动容灾,适用于文档型数据库场景,您无需自建灾备体系及控制管理系统。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档