1MONGOC_READ_PREFS_T(3) libmongoc MONGOC_READ_PREFS_T(3)
2
3
4
5A read preference abstraction
6
8 mongoc_read_prefs_t provides an abstraction on top of the MongoDB con‐
9 nection read preferences. It allows for hinting to the driver which
10 nodes in a replica set should be accessed first and how.
11
12 You can specify a read preference mode on connection objects, database
13 objects, collection objects, or per-operation. Generally, it makes the
14 most sense to stick with the global default mode, MONGOC_READ_PRIMARY.
15 All of the other modes come with caveats that won't be covered in great
16 detail here.
17
19 ┌───────────────────────────┬────────────────────────────┐
20 │MONGOC_READ_PRIMARY │ Default mode. All opera‐ │
21 │ │ tions read from the cur‐ │
22 │ │ rent replica set primary. │
23 ├───────────────────────────┼────────────────────────────┤
24 │MONGOC_READ_SECONDARY │ All operations read from │
25 │ │ among the nearest sec‐ │
26 │ │ ondary members of the │
27 │ │ replica set. │
28 ├───────────────────────────┼────────────────────────────┤
29 │MONGOC_READ_PRIMARY_PRE‐ │ In most situations, opera‐ │
30 │FERRED │ tions read from the pri‐ │
31 │ │ mary but if it is unavail‐ │
32 │ │ able, operations read from │
33 │ │ secondary members. │
34 ├───────────────────────────┼────────────────────────────┤
35 │MONGOC_READ_SECONDARY_PRE‐ │ In most situations, opera‐ │
36 │FERRED │ tions read from among the │
37 │ │ nearest secondary members, │
38 │ │ but if no secondaries are │
39 │ │ available, operations read │
40 │ │ from the primary. │
41 ├───────────────────────────┼────────────────────────────┤
42 │MONGOC_READ_NEAREST │ Operations read from among │
43 │ │ the nearest members of the │
44 │ │ replica set, irrespective │
45 │ │ of the member's type. │
46 └───────────────────────────┴────────────────────────────┘
47
49 Tag sets allow you to specify custom read preferences and write con‐
50 cerns so that your application can target operations to specific mem‐
51 bers.
52
53 Custom read preferences and write concerns evaluate tags sets in dif‐
54 ferent ways: read preferences consider the value of a tag when select‐
55 ing a member to read from, while write concerns ignore the value of a
56 tag when selecting a member, except to consider whether or not the
57 value is unique.
58
59 You can specify tag sets with the following read preference modes:
60
61 • primaryPreferred
62
63 • secondary
64
65 • secondaryPreferred
66
67 • nearest
68
69 Tags are not compatible with MONGOC_READ_PRIMARY and, in general, only
70 apply when selecting a secondary member of a set for a read operation.
71 However, the nearest read mode, when combined with a tag set, will se‐
72 lect the nearest member that matches the specified tag set, which may
73 be a primary or secondary.
74
75 Tag sets are represented as a comma-separated list of colon-separated
76 key-value pairs when provided as a connection string, e.g.
77 dc:ny,rack:1.
78
79 To specify a list of tag sets, using multiple readPreferenceTags, e.g.
80
81 readPreferenceTags=dc:ny,rack:1;readPreferenceTags=dc:ny;readPreferenceTags=
82
83 Note the empty value for the last one, which means "match any secondary
84 as a last resort".
85
86 Order matters when using multiple readPreferenceTags.
87
88 Tag Sets can also be configured using mongoc_read_prefs_set_tags().
89
90 All interfaces use the same member selection logic to choose the member
91 to which to direct read operations, basing the choice on read prefer‐
92 ence mode and tag sets.
93
95 When connected to replica set running MongoDB 3.4 or later, the driver
96 estimates the staleness of each secondary based on lastWriteDate values
97 provided in server hello responses.
98
99 Max Staleness is the maximum replication lag in seconds (wall clock
100 time) that a secondary can suffer and still be eligible for reads. The
101 default is MONGOC_NO_MAX_STALENESS, which disables staleness checks.
102 Otherwise, it must be a positive integer at least MONGOC_SMALL‐
103 EST_MAX_STALENESS_SECONDS (90 seconds).
104
105 Max Staleness is also supported by sharded clusters of replica sets if
106 all servers run MongoDB 3.4 or later.
107
109 When connecting to a sharded cluster running MongoDB 4.4 or later,
110 reads can be sent in parallel to the two "best" hosts. Once one result
111 returns, any other outstanding operations that were part of the hedged
112 read are cancelled.
113
114 When the read preference mode is MONGOC_READ_NEAREST and the sharded
115 cluster is running MongoDB 4.4 or later, hedged reads are enabled by
116 default. Additionally, hedged reads may be explicitly enabled or dis‐
117 abled by calling mongoc_read_prefs_set_hedge() with a BSON document,
118 e.g.
119
120 {
121 enabled: true
122 }
123
124 Appropriate values for the enabled key are true or false.
125
127 MongoDB, Inc
128
130 2017-present, MongoDB, Inc
131
132
133
134
1351.25.1 Nov 08, 2023 MONGOC_READ_PREFS_T(3)