|
| 1 | +.. _csharp-read-write-config: |
| 2 | + |
| 3 | +==================================== |
| 4 | +Configure Operations on Replica Sets |
| 5 | +==================================== |
| 6 | + |
| 7 | +.. contents:: On this page |
| 8 | + :local: |
| 9 | + :backlinks: none |
| 10 | + :depth: 2 |
| 11 | + :class: singlecol |
| 12 | + |
| 13 | +.. facet:: |
| 14 | + :name: genre |
| 15 | + :values: reference |
| 16 | + |
| 17 | +.. meta:: |
| 18 | + :keywords: configuration, availability, causal consistency, code example |
| 19 | + |
| 20 | +Overview |
| 21 | +-------- |
| 22 | + |
| 23 | +In this guide, you can learn how to use the **write concern**, **read concern**, |
| 24 | +and **read preference** configurations to modify the way that MongoDB runs |
| 25 | +create, read, update, and delete (CRUD) operations on replica sets. |
| 26 | + |
| 27 | +You can set these configurations at the following levels: |
| 28 | + |
| 29 | +1. Client, which sets the default for all operation executions unless overridden |
| 30 | +#. Transaction |
| 31 | +#. Database |
| 32 | +#. Collection |
| 33 | + |
| 34 | +This list is in increasing order of precedence. For example, if you set |
| 35 | +read concerns at both the client and the database levels, the read |
| 36 | +concern specified at the database level overrides the read concern at the |
| 37 | +client level. |
| 38 | + |
| 39 | +Write Concern |
| 40 | +------------- |
| 41 | + |
| 42 | +Write concern specifies the level of acknowledgement requested from MongoDB for |
| 43 | +write operations before the operation successfully returns. Operations that |
| 44 | +don't specify an explicit write concern inherit the global default write concern |
| 45 | +setting. |
| 46 | + |
| 47 | +You can set the write concern by setting the ``WriteConcern`` option on a |
| 48 | +``MongoClientSettings``, ``MongoDatabaseSettings``, or ``MongoCollectionSettings`` object, |
| 49 | +or by using the ``WithWriteConcern()`` method of a client, database, or collection instance. |
| 50 | + |
| 51 | +The ``WriteConcern`` option and ``WithWriteConcern()`` method accept a |
| 52 | +``WriteConcern`` instance as a parameter. You can specify the write concern by |
| 53 | +using one of the following values: |
| 54 | + |
| 55 | +- ``WriteConcern.Acknowledged``: The write operation returns after the operation |
| 56 | + is written to memory. |
| 57 | +- ``WriteConcern.W1``: The write operation returns after only the primary node acknowledges |
| 58 | + the write operation, without waiting for acknowledgement from secondary nodes. |
| 59 | +- ``WriteConcern.W2``: The write operation returns after the primary node and |
| 60 | + at least one secondary node acknowledge the write operation. |
| 61 | +- ``WriteConcern.W3``: The write operation returns after the primary node and |
| 62 | + at least two secondary nodes acknowledge the write operation. |
| 63 | +- ``WriteConcern.WMajority``: The write operation returns after a majority of the |
| 64 | + replica set members acknowledge the write operation. |
| 65 | +- ``WriteConcern.Unacknowledged``: The write operation returns after the primary |
| 66 | + node processes the write operation. |
| 67 | + |
| 68 | +The following example sets the write concern to ``WriteConcern.WMajority`` for an instance of |
| 69 | +``MongoClient``: |
| 70 | + |
| 71 | +.. literalinclude:: /includes/fundamentals/code-examples/ReplicaSetConfigs.cs |
| 72 | + :start-after: start-write-concern-client |
| 73 | + :end-before: end-write-concern-client |
| 74 | + :language: csharp |
| 75 | + :dedent: |
| 76 | + |
| 77 | +The following example sets the write concern to ``WriteConcern.WMajority`` for a collection: |
| 78 | + |
| 79 | +.. literalinclude:: /includes/fundamentals/code-examples/ReplicaSetConfigs.cs |
| 80 | + :start-after: start-write-concern-collection |
| 81 | + :end-before: end-write-concern-collection |
| 82 | + :language: csharp |
| 83 | + :dedent: |
| 84 | + |
| 85 | +.. note:: Clients, Collections, and Databases are Immutable |
| 86 | + |
| 87 | + ``IMongoClient``, ``IMongoDatabase``, and ``IMongoCollection`` instances are immutable. |
| 88 | + When you set the write concern on a client, database, or collection, the method returns a new |
| 89 | + instance with the specified settings and does not affect the original instance. |
| 90 | + |
| 91 | +For more information about write concern, see :manual:`Write Concern |
| 92 | +</reference/write-concern/>` in the {+mdb-server+} manual. |
| 93 | + |
| 94 | +Read Concern |
| 95 | +------------ |
| 96 | + |
| 97 | +Read concern specifies the following behaviors: |
| 98 | + |
| 99 | +- Level of :manual:`causal consistency |
| 100 | + </core/causal-consistency-read-write-concerns>` across replica sets |
| 101 | +- :manual:`Isolation guarantees </core/read-isolation-consistency-recency/>` maintained |
| 102 | + during a query |
| 103 | + |
| 104 | +You can specify the read concern by setting the ``ReadConcern`` option on a |
| 105 | +``MongoClientSettings``, ``MongoDatabaseSettings``, or ``MongoCollectionSettings`` object, |
| 106 | +or by using the ``WithReadConcern()`` method on a client, database, or collection. |
| 107 | + |
| 108 | +The ``ReadConcern`` option and ``WithReadConcern()`` method both accept a single parameter |
| 109 | +that specifies the read concern level. |
| 110 | + |
| 111 | +You can set the following read concern levels: |
| 112 | + |
| 113 | +- ``ReadConcern.Local``: The query returns the instance's most recent data. Provides no guarantee |
| 114 | + that the data has been written to a majority of the replica set members. |
| 115 | +- ``ReadConern.Available``: The query returns the instance's most recent data. |
| 116 | + Provides no guarantee that the data has been written to a majority of the |
| 117 | + replica set members. ``ReadConcern.Available`` is not available for use with |
| 118 | + causally consistent sessions and transactions. |
| 119 | +- ``ReadConcern.Majority``: The query returns data that has been acknowledged by |
| 120 | + a majority of the replica set members. |
| 121 | +- ``ReadConcern.Linearizable``: The query returns data that reflects all |
| 122 | + successful writes that completed before the start of the read operation. |
| 123 | + ``ReadConcern.Linearizable`` is not available for use with causally consistent |
| 124 | + sessions and transactions. |
| 125 | +- ``ReadConcern.Snapshot``: The query returns majority-committed data as it appears across shards, from a |
| 126 | + specific single point in the recent past. |
| 127 | + |
| 128 | +For more information about the read concern levels, see :manual:`Read Concern |
| 129 | +Levels </reference/read-concern/#read-concern-levels>` in the {+mdb-server+} |
| 130 | +manual. |
| 131 | + |
| 132 | +The following example sets the read concern to ``ReadConcern.Majority`` for an instance of |
| 133 | +``MongoClient``: |
| 134 | + |
| 135 | +.. literalinclude:: /includes/fundamentals/code-examples/ReplicaSetConfigs.cs |
| 136 | + :start-after: start-read-concern-client |
| 137 | + :end-before: end-read-concern-client |
| 138 | + :language: csharp |
| 139 | + :dedent: |
| 140 | + |
| 141 | +The following example sets the read concern to ``ReadConcern.Majority`` for a |
| 142 | +collection: |
| 143 | + |
| 144 | +.. literalinclude:: /includes/fundamentals/code-examples/ReplicaSetConfigs.cs |
| 145 | + :start-after: start-read-concern-collection |
| 146 | + :end-before: end-read-concern-collection |
| 147 | + :language: csharp |
| 148 | + :dedent: |
| 149 | + |
| 150 | +To learn more about read concern, see :manual:`Read Concern |
| 151 | +</reference/read-concern>` in the {+mdb-server+} manual. |
| 152 | + |
| 153 | +Read Preference |
| 154 | +--------------- |
| 155 | + |
| 156 | +Read preference determines which member of a replica set MongoDB reads when |
| 157 | +running a query. You can set the read preference by setting the ``ReadPreference`` option on a |
| 158 | +``MongoClientSettings``, ``MongoDatabaseSettings``, or ``MongoCollectionSettings`` object, |
| 159 | +or by using the ``WithReadPreference()`` method on a client, database, or collection. |
| 160 | + |
| 161 | +The ``ReadPreference`` option and ``WithReadPreference()`` method accept a read |
| 162 | +preference mode as a parameter. You can set the read preference mode to one of |
| 163 | +the following values: |
| 164 | + |
| 165 | +- ``ReadPreference.Primary``: The query returns data from the primary node. |
| 166 | +- ``ReadPreference.PrimaryPreferred``: The query returns data from the primary node if |
| 167 | + available. Otherwise, the query returns data from a secondary node. |
| 168 | +- ``ReadPreference.Secondary``: The query returns data from a secondary node. |
| 169 | +- ``ReadPreference.SecondaryPreferred``: The query returns data from a secondary node if |
| 170 | + available, Otherwise, the query returns data from the primary node. |
| 171 | +- ``ReadPreference.Nearest``: The query returns data from the node with the lowest |
| 172 | + network latency. |
| 173 | + |
| 174 | +The following example sets the read preference to ``ReadPreference.Secondary`` |
| 175 | +for an instance of ``MongoClient``: |
| 176 | + |
| 177 | +.. literalinclude:: /includes/fundamentals/code-examples/ReplicaSetConfigs.cs |
| 178 | + :start-after: start-read-preference-client |
| 179 | + :end-before: end-read-preference-client |
| 180 | + :language: csharp |
| 181 | + :dedent: |
| 182 | + |
| 183 | +The following example sets the read preference to ``ReadPreference.Secondary`` for a collection: |
| 184 | + |
| 185 | +.. literalinclude:: /includes/fundamentals/code-examples/ReplicaSetConfigs.cs |
| 186 | + :start-after: start-read-preference-collection |
| 187 | + :end-before: end-read-preference-collection |
| 188 | + :language: csharp |
| 189 | + :dedent: |
| 190 | + |
| 191 | +For more information about read preference, see :manual:`Read Preference |
| 192 | +</core/read-preference/>` in the {+mdb-server+} manual. |
| 193 | + |
| 194 | +API Documentation |
| 195 | +----------------- |
| 196 | + |
| 197 | +To learn more about any of the types discussed in this |
| 198 | +guide, see the following API documentation: |
| 199 | + |
| 200 | +- `MongoClientSettings <{+new-api-root+}/MongoDB.Driver/MongoDB.Driver.MongoClientSettings.html>`__ |
| 201 | +- `MongoDatabaseSettings <{+new-api-root+}/MongoDB.Driver/MongoDB.Driver.MongoDatabaseSettings.html>`__ |
| 202 | +- `MongoCollectionSettings <{+new-api-root+}/MongoDB.Driver/MongoDB.Driver.MongoCollectionSettings.html>`__ |
| 203 | +- `WriteConcern <{+new-api-root+}/MongoDB.Driver/MongoDB.Driver.WriteConcern.html>`__ |
| 204 | +- `ReadConcern <{+new-api-root+}/MongoDB.Driver/MongoDB.Driver.ReadConcern.html>`__ |
| 205 | +- `ReadPreference <{+new-api-root+}/MongoDB.Driver/MongoDB.Driver.ReadPreference.html>`__ |
0 commit comments