Skip to content

Commit 92560fa

Browse files
committed
DOCSP-51321: Configure CRUD operations
1 parent 4265714 commit 92560fa

File tree

2 files changed

+434
-1
lines changed

2 files changed

+434
-1
lines changed

source/crud/configure.txt

Lines changed: 323 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,326 @@ Configure CRUD Operations
88
:local:
99
:backlinks: none
1010
:depth: 2
11-
:class: singlecol
11+
:class: singlecol
12+
13+
Overview
14+
--------
15+
16+
In this guide, you can learn how to configure **write concern**, **read concern**,
17+
and **read preference** options to modify the way that the {+driver-short+} runs
18+
read and write operations on replica sets.
19+
20+
Read and Write Settings Precedence
21+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
22+
23+
You can set write concern, read concern, and read preference options at the following
24+
levels:
25+
26+
- Client, which sets the *default for all operation executions* unless overridden
27+
- Transaction
28+
- Database
29+
- Collection
30+
31+
This list also indicates the increasing order of precedence of the option settings. For
32+
example, if you set a read concern for a transaction, it will override the read
33+
concern settings inherited from the client.
34+
35+
Write concern, read concern, and read preference options allow you to customize the
36+
causal consistency and availability of the data in your replica sets. To see a full
37+
list of these options, see the following guides in the {+mdb-server+} manual:
38+
39+
- :manual:`Read Preference </core/read-preference/>`
40+
- :manual:`Read Concern </reference/read-concern/>`
41+
- :manual:`Write Concern </reference/write-concern/>`
42+
43+
.. _kotlin-sync-read-write-config:
44+
45+
Configure Read and Write Operations
46+
-----------------------------------
47+
48+
You can control how the driver routes read operations among replica set members
49+
by setting a read preference. You can also control how the driver waits for
50+
acknowledgment of read and write operations on a replica set by setting read and
51+
write concerns.
52+
53+
The following sections show how to configure these read and write settings
54+
at various levels.
55+
56+
.. _kotlin-sync-read-write-client:
57+
58+
Client Configuration
59+
~~~~~~~~~~~~~~~~~~~~
60+
61+
This example shows how to set the read preference, read concern, and
62+
write concern of a ``MongoClient`` instance by passing a ``MongoClientSettings``
63+
instance to the ``MongoClient.create()`` method. The code configures the
64+
following settings:
65+
66+
- ``secondary`` read preference: Read operations retrieve data from
67+
secondary replica set members.
68+
- ``LOCAL`` read concern: Read operations return the instance's most recent data
69+
without guaranteeing that the data has been written to a majority of the replica
70+
set members.
71+
- ``W2`` write concern: The primary replica set member and one secondary member
72+
must acknowledge the write operation.
73+
74+
.. literalinclude:: /includes/configure-crud.kt
75+
:language: kotlin
76+
:dedent:
77+
:start-after: start-client-settings
78+
:end-before: end-client-settings
79+
80+
Alternatively, you can specify the read and write settings in the connection
81+
URI, which is passed as a parameter to the ``MongoClient.create()`` method:
82+
83+
.. literalinclude:: /includes/configure-crud.kt
84+
:language: kotlin
85+
:dedent:
86+
:start-after: start-client-settings-uri
87+
:end-before: end-client-settings-uri
88+
89+
.. _kotlin-sync-read-write-transaction:
90+
91+
Transaction Configuration
92+
~~~~~~~~~~~~~~~~~~~~~~~~~
93+
94+
This example shows how to set the read preference, read concern, and
95+
write concern of a transaction by passing a ``TransactionOptions``
96+
instance to the ``withTransaction()`` method. Transactions run within
97+
**sessions**, which are groupings of related read or write operations that you
98+
intend to run sequentially. Before applying the transaction options, use the
99+
``startSession()`` method to start a session.
100+
101+
.. tip::
102+
103+
To learn more about sessions, see :manual:`Server Sessions </reference/server-sessions/>`
104+
in the {+mdb-server+} manual.
105+
106+
The example configures the following settings:
107+
108+
- ``primary`` read preference: Read operations retrieve data from
109+
the primary replica set member.
110+
- ``MAJORITY`` read concern: Read operations return the instance's most recent data
111+
that has been written to a majority of replica set members.
112+
- ``W1`` write concern: The primary replica set member must acknowledge the
113+
write operation.
114+
115+
.. literalinclude:: /includes/configure-crud.kt
116+
:language: kotlin
117+
:dedent:
118+
:start-after: start-transaction-settings
119+
:end-before: end-transaction-settings
120+
121+
.. _kotlin-sync-read-write-database:
122+
123+
Database Configuration
124+
~~~~~~~~~~~~~~~~~~~~~~
125+
126+
This example shows how to set the read preference, read concern, and
127+
write concern of a database called ``test_database`` by chaining setter
128+
methods to the ``getDatabase()`` method. The code configures the following
129+
settings:
130+
131+
- ``primaryPreferred`` read preference: Read operations retrieve data from
132+
the primary replica set member, or secondary members if the primary is unavailable.
133+
- ``AVAILABLE`` read concern: Read operations return the instance's most recent data
134+
without guaranteeing that the data has been written to a majority of the replica
135+
set members.
136+
- ``MAJORITY`` write concern: The majority of all replica set members
137+
must acknowledge the write operation.
138+
139+
.. literalinclude:: /includes/configure-crud.kt
140+
:language: kotlin
141+
:dedent:
142+
:start-after: start-database-settings
143+
:end-before: end-database-settings
144+
145+
.. _kotlin-sync-read-write-collection:
146+
147+
Collection Configuration
148+
~~~~~~~~~~~~~~~~~~~~~~~~
149+
150+
This example shows how to set the read preference, read concern, and
151+
write concern of a collection called ``test_collection`` by chaining setter
152+
methods to the ``getCollection()`` method. The code configures the following
153+
settings:
154+
155+
- ``secondaryPreferred`` read preference: Read operations retrieve data from
156+
secondary replica set members, or the primary members if no secondary members are
157+
available.
158+
- ``AVAILABLE`` read concern: Read operations return the instance's most recent data
159+
without guaranteeing that the data has been written to a majority of the replica
160+
set members.
161+
- ``UNACKNOWLEDGED`` write concern: Replica set members do not need to acknowledge
162+
the write operation.
163+
164+
.. literalinclude:: /includes/configure-crud.kt
165+
:language: kotlin
166+
:dedent:
167+
:start-after: start-collection-settings
168+
:end-before: end-collection-settings
169+
170+
.. _kotlin-sync-read-write-advanced:
171+
172+
Advanced Read Configurations
173+
----------------------------
174+
175+
The following sections describe ways to further customize how the {+driver-short+}
176+
routes read operations.
177+
178+
.. _kotlin-sync-sharded-clusters:
179+
180+
Sharded Clusters
181+
~~~~~~~~~~~~~~~~
182+
183+
You can specify a read preference when connecting to a sharded cluster.
184+
MongoDB uses sharding to divide datasets by key ranges and distribute data across multiple
185+
database instances. A sharded cluster, or the set of nodes in a sharded deployment,
186+
includes the following components:
187+
188+
- **Shard**: A replica set that contains a subset of the sharded data.
189+
- **Mongos**: A query router that provides an interface between your
190+
application and the sharded cluster.
191+
- **Config servers**: Servers that store the cluster's configuration settings
192+
and metadata.
193+
194+
.. tip::
195+
196+
To learn more about sharded clusters, see :manual:`Sharding </sharding/>`
197+
in the {+mdb-server+} manual.
198+
199+
When reading from the replica set shards, mongos applies your specified read
200+
preference. The read preference is re-evaluated for each operation.
201+
202+
The following example shows how to connect to a sharded cluster and specify a
203+
``secondary`` read preference in your connection string:
204+
205+
.. literalinclude:: /includes/configure-crud.kt
206+
:language: kotlin
207+
:dedent:
208+
:start-after: start-sharded-cluster-uri
209+
:end-before: end-sharded-cluster-uri
210+
211+
.. _kotlin-sync-tag-sets:
212+
213+
Tag Sets
214+
~~~~~~~~
215+
216+
In {+mdb-server+}, you can apply key-value :manual:`tags
217+
</core/read-preference-tags/>` to replica set members
218+
according to any criteria you choose. You can then use those
219+
tags to target one or more members for a read operation.
220+
221+
By default, the {+driver-short+} ignores tags when choosing a member
222+
to read from. To instruct the {+driver-short+} to prefer certain tags,
223+
pass the tags as a list to your read preference setter method.
224+
225+
Suppose you are connected to a replica set that contains members hosted
226+
at multiple data centers across the United States. You want the driver to
227+
prefer reads from secondary replica set members in the following order:
228+
229+
1. Members from the New York data center, tagged with ``("dc", "ny")``
230+
#. Members from the San Francisco data center, tagged with ``("dc", "sf")``
231+
#. Any secondary members
232+
233+
This code example passes a list of tags representing the preceding replica
234+
set members to the ``ReadPreference.secondary()`` setter method. Then, the code
235+
passes the read preference information to the ``withReadPreference()`` method
236+
to set the read order on the database:
237+
238+
.. literalinclude:: /includes/configure-crud.kt
239+
:language: kotlin
240+
:dedent:
241+
:start-after: start-tag-set
242+
:end-before: end-tag-set
243+
244+
Load Balancing
245+
~~~~~~~~~~~~~~
246+
247+
When connecting to a sharded cluster or a replica set, the {+driver-short+} uses
248+
**load balancing** to handle read and write requests. Load balancing allows the driver to
249+
distribute these requests across multiple servers, which avoids overwhelming
250+
any one server and ensures optimal performance.
251+
252+
When connecting to a sharded cluster, the {+driver-short+} determines the closest mongos
253+
instance by calculating which one has the lowest network round-trip time. Then, the driver
254+
determines the latency window by adding this mongos's average round-trip time to the
255+
:ref:`localThresholdMS value <kotlin-sync-local-threshold>`. The driver load balances requests
256+
across up to two random mongos instances that fall within the latency window. For each request,
257+
the driver chooses the server with the lower operation load by determining its ``operationCount``
258+
value.
259+
260+
When connecting to a replica set, the {+driver-short+} first selects replica set members
261+
according to your read preference. Then, the driver follows the same process as
262+
described in the preceding paragraph. After calculating the latency window, the driver
263+
selects up to two random replica set members that fall within the window and chooses
264+
the member with the lower ``operationCount`` value to receive the request.
265+
266+
.. tip::
267+
268+
To learn more about load balancing, see :manual:`Sharded Cluster Balancer
269+
</core/sharding-balancer-administration/>` in the {+mdb-server+} manual.
270+
271+
.. _kotlin-sync-local-threshold:
272+
273+
Local Threshold
274+
```````````````
275+
276+
The {+driver-short+} uses the local threshold value to calculate the
277+
latency window for server selection. This value determines the servers
278+
that are eligible to receive read and write requests.
279+
280+
By default, the driver uses only mongos instances or replica set members whose
281+
ping times are within 15 milliseconds of the nearest server. To
282+
distribute reads among servers with higher latencies, set the ``localThreshold``
283+
option in a ``MongoClientSettings`` instance or the ``localThresholdMS`` option
284+
in your connection URI.
285+
286+
.. note::
287+
288+
When selecting replica set members from a single mongos instance, the
289+
{+driver-short+} ignores the ``localThresholdMS`` option. In this case, use the
290+
:manual:`localThreshold </reference/program/mongos/#std-option-mongos.--localThreshold>`
291+
command-line option.
292+
293+
The following example connects to a replica set and specifies a local threshold
294+
of 35 milliseconds. Select the :guilabel:`MongoClientSettings` or :guilabel:`Connection URI`
295+
tab to see corresponding code for each approach:
296+
297+
.. tabs::
298+
299+
.. tab:: MongoClientSettings
300+
:tabid: settings
301+
302+
.. literalinclude:: /includes/configure-crud.kt
303+
:language: rust
304+
:dedent:
305+
:start-after: start-local-threshold-settings
306+
:end-before: end-local-threshold-settings
307+
308+
309+
.. tab:: Connection URI
310+
:tabid: uri
311+
312+
.. literalinclude:: /includes/configure-crud.kt
313+
:language: rust
314+
:dedent:
315+
:start-after: start-local-threshold-uri
316+
:end-before: end-local-threshold-uri
317+
318+
In the preceding example, the {+driver-short+} distributes reads among matching members
319+
within 35 milliseconds of the closest member's ping time.
320+
321+
API Documentation
322+
-----------------
323+
324+
To learn more about any of the methods or types discussed in this
325+
guide, see the following API documentation:
326+
327+
- `MongoClient <{+driver-api+}/-mongo-client/index.html>`__
328+
- `MongoClientSettings <{+core-api+}/MongoClientSettings.html>`__
329+
- `TransactionOptions <{+core-api+}/TransactionOptions.html>`_
330+
- `startTransaction() <{+driver-api+}/-client-session/start-transaction.html>`__
331+
- `MongoDatabase <{+driver-api+}/-mongo-database/index.html>`__
332+
- `MongoCollection <{+driver-api+}/-mongo-collection/index.html>`__
333+
- `TagSet <{+core-api+}/TagSet.html>`_

0 commit comments

Comments
 (0)