mongodb的Sharding和replica Sets
为什么要用replica Sets
?
?1、数据冗余
?? replica Sets为你存储多份数据备份提供了一个自动算法
? 允许驱动对write concern的控制,这样可以确保多个节点的数据全部write成功以后才给client发送成功的反馈。
? ? ? writeconcern_safe
2、自动故障转移
?? replica Sets中的每一个节点都是对等的,只有一个primary节点
?? 驱动可以感知到replica Sets中的primary的变化
??
? 3、read Scaling 读刻度
?? 默认情况下,primary节点可以读和写
?? 大部分驱动提供slaveOk函数,声明一些操作可以在secondary节点上进行。当使用slaveOk时,可以将read操作分摊到几个节点上。
?
Sharding
?
{time : “2011-01-01”} …… {time : “2011-10-01”}
{_id :ObjectID()
?User_id : 23
?Time : “2011-12-100:12:23”
?……
}
?????? 如果我们以_id作为sharding-key,那么是可以保证颗粒性的,但是当一个shard出现故障的时候,几乎所有的用户都会受到影响(丢失一部分数据)。
?????? 但是如果我们以user_id为开头作为key的话,那么出现故障时,有一部分user看不到所有的数据,但是其他的user是可以正常使用的,这个是我们可以做的最好的选择。
6、index optimization 索引优化
?
?
?
使用mongodb插数据,没有抛出异常,却发现有丢失数据的情况发生,可以试一下设置一下的参数:
WriteConcern.NONE : No exceptions thrown.WriteConcern.NORMAL : Exceptions are only thrown when the primary node is unreachable for a read, or the full replica set is unreachable.WriteConcern.SAFE : Same as the above, but exceptions thrown when there is a server error on writes or reads.? Calls getLastError().WriteConcern.REPLICAS_SAFE : Tries to write to two separate nodes.? Same as the above, but will throw an exception if two writes are not possible.WriteConcern.FSYNC_SAFE : Same as WriteConcern.SAFE, but also waits for write to be written to disk.?
?
?
?