|
| 1 | +.. _kotlin-sync-server-selection: |
| 2 | + |
| 3 | +========================== |
| 4 | +Customize Server Selection |
| 5 | +========================== |
| 6 | + |
| 7 | +.. contents:: On this page |
| 8 | + :local: |
| 9 | + :backlinks: none |
| 10 | + :depth: 1 |
| 11 | + :class: singlecol |
| 12 | + |
| 13 | +.. facet:: |
| 14 | + :name: genre |
| 15 | + :values: reference |
| 16 | + |
| 17 | +.. meta:: |
| 18 | + :keywords: read preference, write |
| 19 | + |
| 20 | +Overview |
| 21 | +-------- |
| 22 | + |
| 23 | +All MongoDB drivers follow a defined algorithm when selecting a server to read or write |
| 24 | +from. By using the ``ClusterSettings`` property of a ``MongoClientSettings`` object, you can |
| 25 | +customize this algorithm to choose the server that works best for your application. |
| 26 | + |
| 27 | +.. important:: |
| 28 | + |
| 29 | + Customizing the server selection algorithm might have unintended consequences, |
| 30 | + such as degraded read or write performance. |
| 31 | + |
| 32 | +Default Algorithm |
| 33 | +----------------- |
| 34 | + |
| 35 | +When the {+driver-short+} executes a read operation, it performs the following steps, |
| 36 | +in order, to select a MongoDB deployment: |
| 37 | + |
| 38 | +1. Selects all servers that match the active read preference from the list of known servers. |
| 39 | + |
| 40 | +#. If at least one readable server exists, the driver calls the user-defined |
| 41 | + server-selector function and passes in the list from the previous step. |
| 42 | + |
| 43 | +#. Applies the ``localThreshold`` connection setting to the list of |
| 44 | + servers returned from the function. |
| 45 | + |
| 46 | +#. Selects a server at random from the servers still on the list and |
| 47 | + executes the operation against this server. |
| 48 | + |
| 49 | +When the {+driver-short+} executes a write operation, it begins by selecting all writeable |
| 50 | +servers, not just those that match the active read preference. The remaining steps are |
| 51 | +identical. |
| 52 | + |
| 53 | +To learn more about the default server selection algorithm, which the driver follows |
| 54 | +when you don't specify any custom server selection logic, see |
| 55 | +:manual:`Server Selection Algorithm </core/read-preference-mechanics/>` in the |
| 56 | +{+mdb-server+} manual. |
| 57 | + |
| 58 | +Implementing Custom Server Selection Logic |
| 59 | +------------------------------------------ |
| 60 | + |
| 61 | +You can implement your own custom server selection logic by creating a class that |
| 62 | +implements the ``ServerSelector`` interface and overrides the ``select()`` method. The following |
| 63 | +example shows a simple custom server selection class that selects servers with a ``type`` |
| 64 | +value of ``ServerType.REPLICA_SET_SECONDARY``: |
| 65 | + |
| 66 | +.. literalinclude:: /includes/connect/ServerSelection.kt |
| 67 | + :language: kotlin |
| 68 | + :copyable: true |
| 69 | + :start-after: // start-custom-selector |
| 70 | + :end-before: // end-custom-selector |
| 71 | + |
| 72 | +You can then pass an instance of this class to your ``MongoClientSettings`` object by |
| 73 | +using the ``applyToClusterSettings()`` method. The following example shows how to create |
| 74 | +a ``MongoClient`` with an instance of the custom server selector from the preceding example: |
| 75 | + |
| 76 | +.. literalinclude:: /includes/connect/ServerSelection.kt |
| 77 | + :language: kotlin |
| 78 | + :copyable: true |
| 79 | + :start-after: // start-selector |
| 80 | + :end-before: // end-selector |
| 81 | + |
| 82 | +Using Settings to Configure Server Selection |
| 83 | +-------------------------------------------- |
| 84 | + |
| 85 | +You can specify the following server selection settings in your ``MongoClient`` object or |
| 86 | +in your connection URI: |
| 87 | + |
| 88 | +.. list-table:: |
| 89 | + :widths: 30 70 |
| 90 | + :header-rows: 1 |
| 91 | + |
| 92 | + * - Setting |
| 93 | + - Description |
| 94 | + |
| 95 | + * - ``localThreshold`` |
| 96 | + - | The latency window for server eligibility. If a server's round trip takes longer |
| 97 | + | than the fastest server's round-trip time plus this value, the server isn't |
| 98 | + | eligible for selection. |
| 99 | + | |
| 100 | + | **Data Type**: ``Integer`` |
| 101 | + | **Default**: 15 milliseconds |
| 102 | + | **Connection URI Example**: ``localThresholdMS=0`` |
| 103 | + |
| 104 | + * - ``readPreference`` |
| 105 | + - | The client's default read-preference settings. For more information on read preference |
| 106 | + options, see :manual:`Read Preference </core/read-preference/>` in the {+mdb-server+} manual. |
| 107 | + | |
| 108 | + | **Data Type**: `ReadPreference <{+core-api+}/ReadPreference.html>`__ |
| 109 | + | **Default**: ``ReadPreference.primary()`` |
| 110 | + | **Connection URI Example**: |
| 111 | + |
| 112 | + .. code-block:: none |
| 113 | + :copyable: false |
| 114 | + |
| 115 | + readPreference=primaryPreferred |
| 116 | + &maxStalenessSeconds=90 |
| 117 | + &readPreferenceTags=dc:ny,rack:1 |
| 118 | + |
| 119 | + * - ``serverSelectionTimeout`` |
| 120 | + - | The length of time the driver tries to select a server before timing out. |
| 121 | + | |
| 122 | + | **Data Type**: ``long`` |
| 123 | + | **Default**: 30 seconds |
| 124 | + | **Connection URI Example**: ``serverSelectionTimeoutMS=15000`` |
| 125 | + |
| 126 | +API Documentation |
| 127 | +----------------- |
| 128 | + |
| 129 | +To learn more about the classes and methods used in this guide, see the following API |
| 130 | +documentation: |
| 131 | + |
| 132 | +- `MongoClient <{+driver-api+}/-mongo-client/index.html>`__ |
| 133 | +- `MongoClientSettings <{+core-api+}/MongoClientSettings.html>`__ |
| 134 | +- `ServerSelector <{+core-api+}/ServerSelector.html>`__ |
| 135 | +- `ReadPreference <{+core-api+}/ReadPreference.html>`__ |
0 commit comments