码迷,mamicode.com
首页 > 其他好文 > 详细

mgo 如何实现读写分离

时间:2014-11-19 11:35:45      阅读:435      评论:0      收藏:0      [点我收藏+]

标签:des   style   io   ar   color   os   使用   sp   for   

参照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对读写分离的性能提升最强。具体使用时根据业务需要进行权衡。

mgo 如何实现读写分离

标签:des   style   io   ar   color   os   使用   sp   for   

原文地址:http://my.oschina.net/ricktian1226/blog/346207

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!