2015-11-26 16:23:15.0|分类: MongoDB|浏览量: 2061
Read preference describes how MongoDB clients route read operations to the members of a replica set. By default, an application directs its read operations to the primary member in a replica set. Because write operations are issued to the single primary, reading from the primary returns the latest version of a document[1]. For an application that does not require fully up-to-date data, you can improve read throughput or reduce latency by distributing some or all reads to secondary members of the replica set. IMPORTANT Exercise care when specifying read preferences: Modes other than primary may return stale data because with asynchronous replication, data in the secondary may not reflect the most recent write operations. [1] NOTE The read preference does not affect the visibility of data; i.e, clients can see the results of writes before they are made durable:
Use CasesIndicationsThe following are common use cases for using non-primary read preference modes:
Counter-IndicationsIn general, do not use secondary and secondaryPreferred to provide extra capacity for reads 提供额外的读取能力, because:
Sharding increases read and write capacity by distributing read and write operations across a group of machines, and is often a better strategy for adding capacity. See Read Preference Processes for more information about the internal application of read preferences. Read Preference ModesIMPORTANT All read preference modes except primary may return stale陈旧的 data because secondaries replicate operations from the primary with some delay 延迟. [1] Ensure that your application can tolerate 容忍陈旧 stale data if you choose to use a non-primary mode. MongoDB drivers support five read preference modes.
The syntax for specifying the read preference mode is specific to the driver and to the idioms of the host language. Read preference modes are also available to clients connecting to a sharded cluster through a mongos. Themongos instance obeys specified read preferences when connecting to the replica set that provides eachshard in the cluster. In the mongo shell, the readPref() cursor method provides access to read preferences. For more information, see read preference background and read preference behavior. See also thedocumentation for your driver. Tag Sets标签集 不适用于primary read模式Tag sets allow you to target read operations to specific members of a replica set. Custom read preferences and write concerns evaluate tags sets in different ways. Read preferences consider the value of a tag when selecting a member to read from. Write concerns ignore the value of a tag to when selecting a member, except to consider whether or not the value is unique. You can specify tag sets with the following read preference modes: Tags are not compatible兼容 with mode primary and, in general, only apply when selecting a secondarymember of a set for a read operation. However, the nearest read mode, when combined with a tag set, selects the matching member with the lowest network latency. This member may be a primary or secondary. All interfaces use the same member selection logic to choose the member to which to direct read operations, basing the choice on read preference mode and tag sets. For information on configuring tag sets, see the Configure Replica Set Tag Sets tutorial. For more information on how read preference modes interact with tag sets, see the documentation for each read preference mode.
|