Read Preference (original) (raw)

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.

click to enlarge

By default, an application directs its read operations to theprimary member in a replica set (that is, read preference mode "primary"). But, clients can specify a read preference to send read operations to secondaries.

Read preference consists of the read preference mode and optionally, a tag set list, and themaxStalenessSecondsoption.

The following table summarizes the read preference modes:

For detailed description of the read preference modes, seeRead Preference Modes.

See also:

Warning

Secondary Reads in a Sharded Cluster with Migrations Can Miss Documents

Long-running secondary reads in a sharded cluster can miss documents if migrations are occurring.

Before deleting a chunk during chunk migration, MongoDB waits fororphanCleanupDelaySecs, or for in-progress queries involving the chunk to complete on the shard primary, whichever is longer. Queries that were initially run on a node that was primary, but continue after the node has stepped down to a secondary, will be treated as if they were initially executed on a secondary. That is, the server only waits for orphanDelayCleanupSecs if there are no queries targeting the chunk on the current primary.

Queries that target the chunk and are run on secondaries may miss documents if these queries take longer thanorphanCleanupDelaySecs.

primary

All read operations use only the current replica setprimary. [1] This is the default read mode. If the primary is unavailable, read operations produce an error or throw an exception.

The primary read preference mode is not compatible with read preference modes that use tag set lists or maxStalenessSeconds. If you specify tag set lists or a maxStalenessSeconds value with primary, the driver will produce an error.

Distributed transactions that contain read operations must use read preference primary. All operations in a given transaction must route to the same member.

primaryPreferred

In most situations, operations read from the primary member of the set. However, if the primary is unavailable, as is the case during failover situations, operations read from secondarymembers that satisfy the read preference's maxStalenessSeconds and tag set lists.

When the primaryPreferred read preference includes a maxStalenessSeconds value and there is no primary from which to read, the client estimates how stale each secondary is by comparing the secondary's last write to that of the secondary with the most recent write. The client then directs the read operation to a secondary whose estimated lag is less than or equal to maxStalenessSeconds.

When the read preference includes a tag set list (an array of tag sets) and there is no primary from which to read, the client attempts to find secondary members with matching tags (trying the tag sets in order until a match is found). If matching secondaries are found, the client selects a random secondary from the nearest group of matching secondaries. If no secondaries have matching tags, the read operation produces an error.

When the read preference includes a maxStalenessSeconds valueand a tag set list, the client filters by staleness first and then by the specified tags.

Read operations using the primaryPreferred mode may return stale data. Use themaxStalenessSeconds option to avoid reading from secondaries that the client estimates are overly stale.

secondary

Operations read only from the secondary members of the set. If no secondaries are available, then this read operation produces an error or exception.

Most replica sets have at least one secondary, but there are situations where there may be no available secondary. For example, a replica set with a primary, a secondary, and anarbiter may not have any secondaries if a member is in recovering state or unavailable.

When the secondary read preference includes a maxStalenessSeconds value, the client estimates how stale each secondary is by comparing the secondary's last write to that of the primary. The client then directs the read operation to a secondary whose estimated lag is less than or equal to maxStalenessSeconds. If there is no primary, the client uses the secondary with the most recent write for the comparison.

When the read preference includes a tag set list (an array of tag sets), the client attempts to find secondary members with matching tags (trying the tag sets in order until a match is found). If matching secondaries are found, the client selects a random secondary from the nearest group of matching secondaries. If no secondaries have matching tags, the read operation produces an error.

When the read preference includes a maxStalenessSeconds valueand a tag set list, the client filters by staleness first and then by the specified tags.

Read operations using the secondary mode may return stale data. Use themaxStalenessSeconds option to avoid reading from secondaries that the client estimates are overly stale.

secondaryPreferred

Operations typically read data from secondary members of the replica set. If the replica set has only one single primarymember and no other members, operations read data from the primary member.

When the secondaryPreferred read preference includes a maxStalenessSeconds value, the client estimates how stale each secondary is by comparing the secondary's last write to that of the primary. The client then directs the read operation to a secondary whose estimated lag is less than or equal to maxStalenessSeconds. If there is no primary, the client uses the secondary with the most recent write for the comparison. If there are no secondaries with estimated lag less than or equal to maxStalenessSeconds, the client directs the read operation to the replica set's primary.

When the read preference includes a tag set list (an array of tag sets), the client attempts to find secondary members with matching tags (trying the tag sets in order until a match is found). If matching secondaries are found, the client selects a random secondary from the nearest group of matching secondaries. If no secondaries have matching tags, the client ignores tags and reads from the primary.

When the read preference includes a maxStalenessSeconds valueand a tag set list, the client filters by staleness first and then by the specified tags.

Read operations using the secondaryPreferred mode may return stale data. Use themaxStalenessSeconds option to avoid reading from secondaries that the client estimates are overly stale.

nearest

The driver reads from a member whose network latency falls within the acceptable latency window. Reads in the nearest mode do not consider whether a member is a primary orsecondary when routing read operations: primaries and secondaries are treated equivalently.

Set this mode to minimize the effect of network latency on read operations without preference for current or stale data.

When the read preference includes a maxStalenessSeconds value, the client estimates how stale each secondary is by comparing the secondary's last write to that of the primary, if available, or to the secondary with the most recent write if there is no primary. The client will then filter out any secondary whose estimated lag is greater thanmaxStalenessSeconds and randomly direct the read to a remaining member (primary or secondary) whose network latency falls within theacceptable latency window.

If you specify a tag set list, the client attempts to find a replica set member that matches the specified tag set lists and directs reads to an arbitrary member from among the nearest group.

When the read preference includes a maxStalenessSeconds valueand a tag set list, the client filters by staleness first and then by the specified tags. From the remaining mongod instances, the client then randomly directs the read to an instance that falls within the acceptable latency window. The read preference member selectiondocumentation describes the process in detail.

Read operations using the nearest mode may return stale data. Use themaxStalenessSeconds option to avoid reading from secondaries that the client estimates are overly stale.

See also:

When using a MongoDB driver, you can specify the read preference using the driver's read preference API. See the driver API documentation. You can also set the read preference whenconnecting to the replica set or sharded cluster. For an example, see connection string.

For a given read preference, the MongoDB drivers use the samemember selection logic.

When using mongosh, seecursor.readPref() and Mongo.setReadPref().

Distributed transactions that contain read operations must use read preference primary. All operations in a given transaction must route to the same member.

Consider the following points when using $merge or$out stages in an aggregation pipeline:

For mapReduce operations, only "inline"mapReduce operations that do not write data support read preference. Otherwise, mapReduce operations run on theprimary member.