Read Preference
cookqq ›博客列表 ›MongoDB

Read Preference

2015-11-26 16:23:15.0|分类: MongoDB|浏览量: 2205

摘要: 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.

Read preference describes how MongoDB clients route read operations to the members of a replica set.

Read operations to a replica set. Default read preference routes the read to the primary. Read preference of ``nearest`` routes the read to the nearest member.

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:

  • Regardless of write concern, other clients can see the result of the write operations before the write operation is acknowledged to the issuing client.

  • Clients can read data which may be subsequently rolled back.

Use Cases

Indications

The following are common use cases for using non-primary read preference modes:

  • Running systems operations that do not affect the front-end application.这并不影响前端应用程序

    NOTE

    Read preferences aren’t relevant有关地 to direct connections to a single mongod instance. However, in order to perform 执行 read operations on a direct connection to a secondary member of a replica set, you must set a read preference, such as secondary.

  • Providing提供 local reads for geographically distributed applications.

    If you have application servers in multiple data centers, you may consider having a geographically distributed replica set and using a non primary read preference or the nearest. This allows the client to read from the lowest-latency members, rather than always reading from the primary.

  • Maintaining availability during a failover.故障转移期间保持可用性

    Use primaryPreferred if you want an application to read from the primary under normal circumstances, but to allow stale reads from secondaries in an emergency. This provides a “read-only mode” for your application during a failover.

Counter-Indications

In general, do not use secondary and secondaryPreferred to provide extra capacity for reads 提供额外的读取能力, because:

  • All members of a replica have roughly equivalent write traffic 大致相等的写流量, as a result secondaries will service reads at roughly the same rate as the primary.

  • Because replication is asynchronous 异步 and there is some amount of delay 延迟 between a successful write operation and its replication to secondaries, reading from a secondary can return out-of-date data.

  • Distributing read operations to secondaries can compromise availability 可用性 if any members of the set are unavailable because the remaining members of the set will need to be able to handle all application requests.

  • For queries of sharded collections, for clusters with the balancer active, secondaries may return stale results with missing or duplicated data because of incomplete or terminated migrations.

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 Modes

IMPORTANT

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.

Read Preference ModeDescription
primary Default mode. All operations read from the current replica set primary. 从主副本读数据
primaryPreferredIn most situations, operations read from the primary but if it is unavailable, operations read from secondary members.主副本读优先级高,如果主副本不可见,再从次副本读取
secondaryAll operations read from the secondary members of the replica set.从次副本读取数据
secondaryPreferredIn most situations, operations read from secondary members but if no secondarymembers are available, operations read from the primary.次副本读优先级高,如果次副本不可见,再从主副本读取
nearestOperations read from member of the replica set with the least network latency, irrespective of the member’s type.从网络最近处进行数据读取

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:

  • primaryPreferred

  • secondary

  • secondaryPreferred

  • nearest

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.

[1](1, 2, 3) In some circumstances, two nodes in a replica set may transiently believe that they are the primary, but at most, one of them will be able to complete writes with {w: majority} write concern. The node that can complete {w: majority} writes is the current primary, and the other node is a former primary that has not yet recognized its demotion, typically due to a network partition. When this occurs, clients that connect to the former primary may observe stale data despite having requested read preference primary.


一键分享文章

分类列表

  • • struts源码分析
  • • flink
  • • struts
  • • redis
  • • kafka
  • • ubuntu
  • • zookeeper
  • • hadoop
  • • activiti
  • • linux
  • • 成长
  • • NIO
  • • 关键词提取
  • • mysql
  • • android studio
  • • zabbix
  • • 云计算
  • • mahout
  • • jmeter
  • • hive
  • • ActiveMQ
  • • lucene
  • • MongoDB
  • • netty
  • • flume
  • • 我遇到的问题
  • • GRUB
  • • nginx
  • • 大家好的文章
  • • android
  • • tomcat
  • • Python
  • • luke
  • • android源码编译
  • • 安全
  • • MPAndroidChart
  • • swing
  • • POI
  • • powerdesigner
  • • jquery
  • • html
  • • java
  • • eclipse
  • • shell
  • • jvm
  • • highcharts
  • • 设计模式
  • • 列式数据库
  • • spring cloud
  • • docker+node.js+zookeeper构建微服务
版权所有 cookqq 感谢访问 支持开源 京ICP备15030920号
CopyRight 2015-2018 cookqq.com All Right Reserved.