You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/READ_PREFERENCE.md
+70-10Lines changed: 70 additions & 10 deletions
Original file line number
Diff line number
Diff line change
@@ -1,15 +1,22 @@
1
1
# Read Preference in Ruby
2
2
3
-
## Setting the read preference
3
+
## About Read Preference
4
4
5
-
You can using the `:read` option to specify a query's read preference. There are for now two possible options:
5
+
Read preferences determine the candidate replica set members to which a query or command can be sent. They consist of a *mode* specified as a symbol and an array of hashes known as *tag_sets*.
*Please Note*: Behavior of some read preference modes have changed in version 1.7.0:
17
+
18
+
*`:secondary_preferred` mode is now used to prefer reads from secondary members (before this was the behavior of `:secondary`).
19
+
*`:secondary_only` mode (which only allowed reads from secondaries) is now called `:secondary`.
13
20
14
21
## Read preference inheritance
15
22
@@ -33,7 +40,60 @@ You can examine the read preference on any object by calling its `read_preferenc
33
40
@db.read_preference
34
41
@collection.read_preference
35
42
36
-
## Future work
43
+
## Modes
44
+
45
+
You can using the `:read` option to specify a query's read preference mode. There are five possible options.
46
+
47
+
### :primary
48
+
49
+
With primary, all read operations from the client will use the primary member only. This is the default read preference.
50
+
51
+
If the primary is unavailable, all operations with this preference produce an error or throw an exception. Primary read preference modes are not compatible with read preferences modes that use tag sets If you specify a tag set with primary, the driver will produce an error.
52
+
53
+
### :primary_preferred
54
+
55
+
With the primaryPreferred read preference mode, operations will read from the primary member of the set in most situations. However, if the primary is unavailable, as is the case during failover situations, then these read operations can read from secondary members.
56
+
57
+
When the read preference includes a tag set, the client will first read from the primary, if it is available, and then from secondaries that match the specified tags. If there are no secondaries with tags that match the specified tags, this read operation will produce an error.
58
+
59
+
### :secondary
60
+
61
+
With the secondary read preference mode, operations will read from the secondary member of the set if available. However, if there are no secondaries available, then these operations will produce an error or exception.
62
+
63
+
Most sets have at least one secondary, but there are situations where there may not be an available secondary. For example, a set with a primary, a secondary, and an arbiter may not have any secondaries if a member is ever in recovering mode.
64
+
65
+
When the read preference includes a tag set, the client will attempt to find a secondary members that match the specified tag set and directs reads to a random secondary from among the nearest group. If there are no secondaries with tags that match the specified tag set, this read operation will produce an error.
66
+
67
+
### :secondary_preferred
68
+
69
+
With the secondaryPreferred, operations will read from secondary members, but in situations where the set only has a primary instance, the read operation will use the set’s primary.
70
+
71
+
When secondaryPreferred reads from a secondary and the read preference includes a tag set, the client will attempt to find a secondary members that match the specified tag set and directs reads to a random secondary from among the nearest group. If there are no secondaries with tags that match the specified tag set, this read operation will produce an error.
72
+
73
+
### :nearest
74
+
75
+
With the nearest, the driver will read from the nearest member of the set according to the member selection process nearest read operations will not have any consideration for the type of the set member. Reads in nearest mode may read from both primaries and secondaries.
76
+
77
+
Set this mode when you want minimize the effect of network latency on read operations without preference for current or stale data.
78
+
79
+
If you specify a tag set, the client will attempt to find a secondary members that match the specified tag set and directs reads to a random secondary from among the nearest group.
80
+
81
+
## Tag Sets
82
+
83
+
Tag sets can be used in for data center awareness by filtering secondary read operations. Primary reads occur independent of any tags.
84
+
85
+
A member matches a tag set if its tags match all the tags in the set. For example, a member tagged "{ dc: 'ny', rack: 2, size: 'large' }" matches the tag set "{ dc: 'ny', rack: 2 }". A member's extra tags don't affect whether it's a match.
86
+
87
+
Here is an example of a query which sends read operations to members in rack 2.
Tag set keys may be symbols or strings. Tag set values should be specified using strings. The `to_s` method will be called on any values provided in the tag set.
92
+
93
+
Tag sets are used in conjunction with read preference mode. In this example, because we specified a mode of secondary_preferred, if no secondaries can be found that match the tag_set `{:rack => '2'}` then the primary will be used for the query.
94
+
95
+
If only one tag set is provided, the set can be passed as a single hash parameter iteself without the enclosing array.
In the v2.0 release of the driver, you'll also be able to specify a read preference consisting of a set of tags. This way,
39
-
you'll be able to direct reads to a replica set member. You can follow this issue's progress here: (https://jira.mongodb.org/browse/RUBY-326).
99
+
Specifiying tag_sets for mode `:primary` is considered an error and will raise a MongoArgumentError as tag_sets do not affect selection of primary members and only primary members can be selected in that particular mode.
0 commit comments