Mongodb读取首选项 [英] Mongodb Read preferences

查看:224
本文介绍了Mongodb读取首选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

设置:

我有一个mongodb的PRIMARY和两个SECONDARY实例. 两个辅助实例之一与我的Web应用程序托管在同一区域.

I have one PRIMARY and two SECONDARY instances of mongodb. One of the two secondary instances is hosted in the same region as my web app.

我正在使用pymongo进行连接.

I am using pymongo for connecting.

查询:

如何以较低的延迟连接到SECONDARY.

How can I get connection to a SECONDARY with lower latency.

目前我正在这样做:

  from pymongo import ReplicaSetConnection
  from pymongo import ReadPreference

  db = ReplicaSetConnection('localhost:27017', replicaSet='rs1')['my_db']
  db.read_preference = ReadPreference.SECONDARY

我可以连接到SECONDARY中的任何一个. 如何强制以较低的延迟从实例获取连接

I get a connection to any one of the SECONDARY. How can I force to get connection from the instance with lower latency

谢谢!

推荐答案

此答案非常适合我的情况,因为我最近的辅助服务器与Web服务器位于同一区域(与在同一台计算机上运行一样好),因此数据传输会非常快,而且我不必为带宽使用付费(考虑到数据量,在一个月内这可能会变得很重要).

This answer is very specific to my situation, because my nearest secondary is in the same region as my web server (as good as running on the same machine), so the data transfer would be insanely fast, plus I don't have to pay for bandwidth usage (which over the month might become significant considering the amount of data).

否则,"secondary_acceptable_latency_ms"的默认值= 15ms.因此,这并不重要.

Otherwise the default value for 'secondary_acceptable_latency_ms' = 15ms. So it doesn't really matter.

因此,如果有人发现自己处在相同的情况下,该怎么办:

So in case someone finds himself/herself in the same situation, this is what to do:

    from pymongo import ReplicaSetConnection
    from pymongo import ReadPreference

    db = ReplicaSetConnection('localhost:27017', replicaSet='rs1')['my_db']
    db.read_preference = ReadPreference.SECONDARY
    db.secondary_acceptable_latency_ms = 0.001

谢谢!

这篇关于Mongodb读取首选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆