文档章节

mgo 如何实现读写分离

ricktian1226
 ricktian1226
发布于 2014/11/19 10:08
字数 615
阅读 779
收藏 3

参照mgo的session.SetMode函数的说明:

func (*Session) SetMode

func (s *Session) SetMode(consistency mode, refresh bool)

SetMode changes the consistency mode for the session.

SetMode函数用来变换session的一致性模式。

In the Strong consistency mode reads and writes will always be made to the primary server using a unique connection so that reads and writes are fully consistent, ordered, and observing the most up-to-date data. This offers the least benefits in terms of distributing load, but the most guarantees. See also Monotonic and Eventual.

在强一致模式下,读写都是在主节点上通过同一个连接实现,这样读写是完全一致、有序并且是最新的数据。这种模式在分布式负载架构下没有多少益处,但是保证了读写数据的一致性。再看看Monotonic 和Eventual模式。

In the Monotonic consistency mode reads may not be entirely up-to-date, but they will always see the history of changes moving forward, the data read will be consistent across sequential queries in the same session, and modifications made within the session will be observed in following queries (read-your-writes).

Monotonic一致模式下,读到的数据不一定是最新的,但是在同一个session中的一系列读保持着数据一致性,而session中的修改在其后的查询中可以得到体现,

In practice, the Monotonic mode is obtained by performing initial reads on a unique connection to an arbitrary secondary, if one is available, and once the first write happens, the session connection is switched over to the primary server. This manages to distribute some of the reading load with secondaries, while maintaining some useful guarantees.

In the Eventual consistency mode reads will be made to any secondary in the cluster, if one is available, and sequential reads will not necessarily be made with the same connection. This means that data may be observed out of order. Writes will of course be issued to the primary, but independent writes in the same Eventual session may also be made with independent connections, so there are also no guarantees in terms of write ordering (no read-your-writes guarantees either).

The Eventual mode is the fastest and most resource-friendly, but is also the one offering the least guarantees about ordering of the data read and written.

If refresh is true, in addition to ensuring the session is in the given consistency mode, the consistency guarantees will also be reset (e.g. a Monotonic session will be allowed to read from secondaries again). This is equivalent to calling the Refresh function.

Shifting between Monotonic and Strong modes will keep a previously reserved connection for the session unless refresh is true or the connection is unsuitable (to a secondary server in a Strong session).

总结下来,如下表格所示:


Strong Monotonic Eventual
read read to primary server read to arbitrary secondary
read to any secondary
write write to primary server
write to primary
write to primary
connection unique
unique independent
guarantee
the most some useful the least

strong可靠性最强,eventual对读写分离的性能提升最强。具体使用时根据业务需要进行权衡。

© 著作权归作者所有

共有 人打赏支持
ricktian1226
粉丝 0
博文 7
码字总数 2728
作品 0
厦门
关于golang mgo用户提交注入风险

我在网上找了一下,关于mongodb确实存在注入风险。 但在golang中mgo,如果没有对用户提交数据进行有效过滤,仅使用mgo的bson.M查询,是否也会出现被注入的风险? bson.M{"_id",userPostID 如...

fotomxq
2016/12/19
154
1
go 使用mongodb

mongodb官方没有关于go的mongo的驱动,因此只能使用第三方驱动,mgo就是使用最多的一种。 官网:http://labix.org/mgo 文档:http://godoc.org/gopkg.in/mgo.v2 等下继续补充。。。。。 mgo使...

solate
2016/12/13
36
0
用过mgo的同学进来指点一下,如何将一个json直接存储到mongoDB中

RT,mgo存储的方式貌似是先转化为struct然后用Insert存储入mongoDB。 但如果我抓到的直接是json,先转化为struct然后再存入不是多此一举吗(mongoDB本身就可以直接存储json啊)? 而且抓到的jso...

神州浪子
2014/10/08
1K
1
redis在cluster模式下如何实现读写分离?

根据redis官方资料描述,通过slaveof的方式配置主从模式,可以实现读写分离,即主节点实现写,从节点实现读; 但是如果使用cluster模式启动,通过如下方式来创建集群,并自动分配主从关系,启...

sunshinexs
2015/05/25
877
2
Amoeba for Mysql 如何

http://www.oschina.net/bbs/thread/10387 这是红薯发的一篇文章,现在项目中想使用 Amoeba进行mysql的读写分离,有经验的哥们给点意见, 这个软件如何。或者有其他的实现读写分离方式。 my...

rigger
2011/04/23
781
4

没有更多内容

加载失败,请刷新页面

加载更多

Bytom资产发行与部署合约教程

比原项目仓库: Github地址:https://github.com/Bytom/bytom Gitee地址:https://gitee.com/BytomBlockchain/bytom 发行资产 在比原链上发行资产比较方便快捷,使用节点的dashboard图形界面...

比原链Bytom
14分钟前
0
0
Ext ComboBox 实现下拉多选,全选,反选

Ext ComboBox下拉选中-全选反选逻辑处理 Ext ComboBox 实现下拉多选,全选,反选 方法一: 代码 var me = this;var isMultiSelect = true;//是否设置为下拉多选me.selectValues = [];//保存...

javaART
17分钟前
0
0
Swoole Windows 版(4.2.1)

https://pan.baidu.com/s/1uTm77_cp4kn0_xMgO1DpIw Swoole Windows 版(内部版本,swoole-4.2.1,php-7.1,必须为64位系统,Win7或更高版本)。 解压后,将 $dir/bin 目录,设置到 系统的环境...

老查
19分钟前
0
0
美团点评上市受追捧,成中国第四大互联网企业

从建立到上市,蔚来用了不到4年,拼多多3年,趣头条更是仅用了2年3个月。在这波中概股上市浪潮中,等待了漫长8年的美团点评也终于迎来登陆资本市场的时刻。20日上午,美团创始人兼CEO王兴终于...

Mr_zebra
20分钟前
0
0
Mysql-mybatis批量插入

话不多说直接上代码吧 <insert id="batchSave" >insert into table_name (`name`,age)values<foreach collection="list" index="index" item="item" open="(" separator="," close=......

落叶清风
26分钟前
0
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部