项目中遇到一个需求:必须保证在写入后,数据都已同步到所有从节点才返回成功。MongoDB 架构使用了1主 2从 1仲裁,所以我设置了 w 为3,运作良好,符合预期。但是,当某个从节点宕机后,此时 data bearing 节点只有两个,使用 w = 3 会出现异常,需要手动改为 w = 2,等宕机的从节点重新连接后;又需要改为 w = 3。所以我在想,就像 w = majority 代表大多数 data bearing 节点,那么为什么没有 w = max 的代表所有 data bearing 节点呢?
这是在 stackoverflow 找到的讨论 => 原文地址
MongoDB 没这个选项大体上是因为:
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.
如果你需要强一致性,那就只读 primary 节点,并且使用 w = majority 来保证数据被完整备份,做到故障转移。
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).
从节点通常是用来做故障转移的,而不是分担读请求的。