项目中遇到一个需求:必须保证在写入后,数据都已同步到所有从节点才返回成功。MongoDB 架构使用了1主 2从 1仲裁,所以我设置了 w 为3,运作良好,符合预期。但是,当某个从节点宕机后,此时 data bearing 节点只有两个,使用 w = 3 会出现异常,需要手动改为 w = 2,等宕机的从节点重新连接后;又需要改为 w = 3。所以我在想,就像 w = majority 代表大多数 data bearing 节点,那么为什么没有 w = max 的代表所有 data bearing 节点呢?
这是在 stackoverflow 找到的讨论 => 地址
解答一:
There are numerous problems with this, I mean how do you judge the complete number of max up members while making sure you get all members of the set? What about dealing with down members?
解答二:
If your use case requires strong consistency then you should always read from the primary instead of secondaries, and use a write concern of majority / replica_safe to ensure data has replicated sufficiently for high availability in the event of failover.
解答三:
The default read preference is to direct reads to the primary for consistency. Secondaries in MongoDB replica sets are generally intended to support high availability rather than read scaling (with a few exceptions such as distribution across multiple data centres).
综上:从节点通常是用来做故障转移、保证高可用的,如果需要强一致性,那就只读Primary。
解答一我没理解,欢迎留言交流!