mongodb 中的读写

如果一个replica set中有4个member,只有3个可用,那么默认情况下写会一直等待,直到4个成员都可用,可以在写入的时候指定策略,使写操作 不会被阻塞
db.products.insert(
{ item: “envelopes”, qty : 100, type: “Clasp” },
{ writeConcern: { w: 2, wtimeout: 5000 } }
)
上面的策略使写入2个成员或超过5秒钟就返回,不等待。
修改默认的策略
cfg = rs.conf()
cfg.settings = {}
cfg.settings.getLastErrorDefaults = { w: “majority”, wtimeout: 5000 }
rs.reconfig(cfg)

mongodb有下面的几种读的策略
Read Preference Mode Description
primary Default mode. All operations read from the current replica set primary.
primaryPreferred In most situations, operations read from the primary but if it is unavailable, operations read from secondary members.
secondary All operations read from the secondary members of the replica set.
secondaryPreferred In most situations, operations read from secondary members but if no secondary members are available, operations read from the primary.
nearest Operations read from member of the replica set with the least network latency, irrespective of the member’s type.

可以指定 read preference mode,参考https://api.mongodb.com/?_ga=1.181859943.1558166735.1471845414

选节点的过程如下;
When you select non-primary read preference, the driver will determine which member to target using the following process:

Assembles a list of suitable members, taking into account member type (i.e. secondary, primary, or all members).
Excludes members not matching the tag sets, if specified.
Determines which suitable member is the closest to the client in absolute terms.
Builds a list of members that are within a defined ping distance (in milliseconds) of the “absolute nearest” member.
Applications can configure the threshold used in this stage. The default “acceptable latency” is 15 milliseconds, which you can override in the drivers with their own secondaryAcceptableLatencyMS option. For mongos you can use the –localThreshold or localPingThresholdMs runtime options to set this value.
Selects a member from these hosts at random. The member receives the read operation.
成员之间可能有延迟,为了能每次查询的结果一直,可以设置请求关联,每次请求都在一个成员上
Because secondary members of a replica set may lag behind the current primary by different amounts, reads for secondary members may reflect data at different points in time. To prevent sequential reads from jumping around in time, the driver can associate application threads to a specific member of the set after the first read, thereby preventing reads from other members. The thread will continue to read from the same member until:

The application performs a read with a different read preference,
The thread terminates, or
The client receives a socket exception, as is the case when there’s a network error or when the mongod closes connections during a failover. This triggers a retry, which may be transparent to the application.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值