V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
dingyaguang117
V2EX  ›  问与答

两台 MongoDB 的复制集,怎么做查询负载均衡?

  •  
  •   dingyaguang117 · Oct 21, 2015 · 1891 views
    This topic created in 3841 days ago, the information mentioned may be changed or developed.
    read_preference 的选项里面,

    PRIMARY: Queries are sent to the primary of the replica set.
    PRIMARY_PREFERRED: Queries are sent to the primary if available, otherwise a secondary.
    SECONDARY: Queries are distributed among secondaries. An error is raised if no secondaries are available.
    SECONDARY_PREFERRED: Queries are distributed among secondaries, or the primary if no secondary is available.
    NEAREST: Queries are distributed among all members.

    最符合的似乎是 NEAREST , 但是实测只是读了本机的 MongoDB ,另外一台机器的完全空闲。。。


    难道我要手动建立 2 个 MongoClient 然后随机查询某个库么。。。
    6 replies    2015-10-21 14:55:19 +08:00
    9hills
        1
    9hills  
       Oct 21, 2015 via iPhone
    Rep 推荐三台
    typcn
        2
    typcn  
       Oct 21, 2015
    查询负载均衡当然是在程序端做

    从连接池取连接的时候根据两台数据库的繁忙程序随机选择
    dingyaguang117
        3
    dingyaguang117  
    OP
       Oct 21, 2015
    @typcn 根据繁忙这个 mongodb 怎么做?
    dingyaguang117
        4
    dingyaguang117  
    OP
       Oct 21, 2015
    @9hills 是的,但是现在只有两台,官方并没有提到过这种情况, sf 上还有人直接说不要这样做。。。

    不过我想的也许可以从架构方面充分利用这两台机器。比如用 nginx 做分发,跑 2 个 web server,每个都 nearest
    9hills
        5
    9hills  
       Oct 21, 2015
    @dingyaguang117 我感觉你的文档是不是有问题啊,我刚查了下,对 nearest 的定义和你那个不同:

    https://docs.mongodb.org/manual/reference/read-preference/

    nearest Operations read from member of the replica set with the least network latency, irrespective of the member ’ s type.


    继续看: https://docs.mongodb.org/manual/core/read-preference-mechanics/#replica-set-read-preference-behavior-nearest

    nearest 是默认从延时<15ms 的节点读取。所以如果两台机器有一台延时>15ms ,那么就会只落在一台了

    有个办法是修改这个 15ms 的值,如果两台的延时都落在这个时间内,那么是两台随机读
    dingyaguang117
        6
    dingyaguang117  
    OP
       Oct 21, 2015
    @9hills 因为 webserver 部署与 primary mongodb 在一台机器上,所以 延迟必然是本机最低了
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   841 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 44ms · UTC 21:10 · PVG 05:10 · LAX 14:10 · JFK 17:10
    ♥ Do have faith in what you're doing.