Skip to content

Commit 8039935

Browse files
author
Sam Kleinman
committed
merge: replica set read preferences update
2 parents d165a44 + 10f511f commit 8039935

File tree

1 file changed

+37
-13
lines changed

1 file changed

+37
-13
lines changed

source/applications/replication.txt

Lines changed: 37 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ Read Preference
9797
---------------
9898

9999
Read preference describes how MongoDB clients route read operations to
100-
:term:`secondary` members of a :term:`replica set`.
100+
members of a :term:`replica set`.
101101

102102
.. index:: read preference; background
103103
.. _replica-set-read-preference-background:
@@ -109,8 +109,8 @@ By default, an application directs its read operations to the :term:`primary`
109109
member in a :term:`replica set`. Reading from the primary guarantees that
110110
read operations reflect the latest version of a document. However,
111111
for an application that does not require fully up-to-date data, you
112-
can improve read throughput by distributing some or all reads to
113-
secondary members of the replica set.
112+
can improve read throughput, or reduce latency, by distributing some or all
113+
reads to secondary members of the replica set.
114114

115115
The following are use cases where you might use secondary reads:
116116

@@ -142,7 +142,7 @@ configuration, see the appropriate :ref:`driver` API documentation.
142142
preferences dictate if the application receives stale or
143143
current data from MongoDB. Use appropriate :term:`write concern`
144144
policies to ensure proper data
145-
replication and constancy.
145+
replication and consistency.
146146

147147
If read operations account for a large percentage of your
148148
application's traffic, distributing reads to secondary members can
@@ -170,9 +170,9 @@ read preference modes:
170170
- :readmode:`secondaryPreferred`
171171
- :readmode:`nearest`
172172

173-
You can specify a read preference mode on a per-collection or
174-
per-operation basis. The syntax for specifying the read preference
175-
mode is :api:`specific to the driver and to the idioms of the host
173+
You can specify a read preference mode on connection objects, database object,
174+
collection object, or per-operation. The syntax for specifying the read
175+
preference mode is :api:`specific to the driver and to the idioms of the host
176176
language <>`.
177177

178178
Read preference modes are also available to clients connecting to a
@@ -440,7 +440,7 @@ As a result, MongoDB drivers and :program:`mongos`:
440440

441441
After this error, the driver selects a new member using the
442442
specified read preference mode. In the absence of a specified read
443-
preference, the driver uses :readmode:`PRIMARY`.
443+
preference, the driver uses :readmode:`primary`.
444444

445445
- After detecting a failover situation, [#fn-failover]_ the driver
446446
attempts to refresh the state of the replica set as quickly as
@@ -496,17 +496,19 @@ lifetime.
496496
Member Selection
497497
````````````````
498498

499-
Both clients, by way of their drivers, and :program:`mongos` instances for
500-
sharded clusters send periodic "ping," messages to all member of the
501-
replica set to determine latency from the application to each
502-
:program:`mongod` instance.
499+
Clients, by way of their drivers, and :program:`mongos` instances for
500+
sharded clusters periodically call :dbcommand:`isMaster` on all members of the
501+
replica set to update their view of the set's state, and to determine latency
502+
from the application to each :program:`mongod` instance.
503503

504504
For any operation that targets a member *other* than the
505505
:term:`primary`, the driver:
506506

507507
#. Assembles a list of suitable members, taking into account member type
508508
(i.e. secondary, primary, or all members.)
509509

510+
#. Excludes members not matching the tag sets, if specified.
511+
510512
#. Determines which suitable member is the closest to the
511513
client in absolute terms.
512514

@@ -523,7 +525,9 @@ for more information.
523525

524526
.. [#acceptable-secondary-latency] Applications can configure the
525527
threshold used in this stage. The default "acceptable latency" is
526-
15 milliseconds. For :program:`mongos` you can use the
528+
15 milliseconds, which you can override in the drivers with the
529+
`secondaryAcceptableLatencyMS` option.
530+
For :program:`mongos` you can use the
527531
:option:`--localThreshold <mongos --localThreshold>` or
528532
:setting:`localThreshold` runtime options to set this value.
529533

@@ -594,6 +598,9 @@ for the following commands:
594598
otherwise these operations must run on the :term:`primary`
595599
members.
596600

601+
:program:`mongos` currently does not route commands using read preferences; all
602+
commands are sent to shards' primaries. See :issue:`SERVER-7423`.
603+
597604
Uses for non-Primary Read Preferences
598605
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
599606

@@ -614,6 +621,16 @@ using non-:readmode:`primary` read preference modes:
614621
direct connection to a :ref:`hidden member
615622
<replica-set-hidden-members>` of the set.
616623

624+
.. warning::
625+
626+
Although it is tempting to use :readmode:`secondaryPreferred` to distribute
627+
load, if your primary cannot accept your full read load, then
628+
using :readmode:`secondaryPreferred` is dangerous: if all secondaries
629+
become unavailable, but you have enough arbiters to prevent the primary from
630+
stepping down, then this mode will redirect traffic to the primary. For this
631+
reason, use :readmode:`secondary` to distribute load,
632+
not :readmode:`secondaryPreferred`.
633+
617634
- Providing local reads for geographically distributed applications.
618635

619636
If you have application servers in multiple data centers, you may
@@ -622,6 +639,13 @@ using non-:readmode:`primary` read preference modes:
622639
read preference or the :readmode:`nearest` to avoid network
623640
latency.
624641

642+
- Maintaining availability during a failover.
643+
644+
Use :readmode:`primaryPreferred` if you want your application to do
645+
consistent reads from the primary under normal circumstances, but to allow
646+
stale reads from secondaries in an emergency. This provides a "read-only mode"
647+
for your application during a failover.
648+
625649
Using read modes other than :readmode:`primary` and
626650
:readmode:`primaryPreferred` to provide extra capacity is not in and
627651
of itself justification for non-:readmode:`primary` in many

0 commit comments

Comments
 (0)