Overview
All MongoDB drivers follow a defined algorithm when selecting a server to read or write
from. By using the ClusterSettings
property of a MongoClientSettings
object, you can
customize this algorithm to choose the server that works best for your application.
Important
Customizing the server selection algorithm might have unintended consequences, such as degraded read or write performance.
Server Selection Algorithm
When the Kotlin Sync driver executes a read operation, it performs the following steps, in order, to select a MongoDB deployment:
Selects all servers from the list of known servers suitable for the requested operation, barring those that the driver considers unavailable or problematic
For reads, selects all servers that match the active read preference
For writes, selects all writeable servers
Calls the user-defined server-selector function, if the user provides one, and passes in the list from the previous step
Applies the
localThreshold
connection setting to the list of servers returned from the functionSelects at most two random servers from the list of servers that match the preceding criteria, then selects the server with fewer outstanding concurrent operations
When the Kotlin Sync driver executes a write operation, it begins by selecting all writeable servers from the list of known servers, not just those that match the active read preference. The remaining steps are identical to the preceding list.
To learn more about the MongoDB server selection algorithm, see Server Selection Algorithm in the MongoDB Server manual.
Implement Custom Server Selection Logic
You can implement your own custom server selection logic by creating a class that
implements the ServerSelector
interface. The following
example shows a simple custom server selection class that selects servers with a type
value of ServerType.REPLICA_SET_SECONDARY
:
class CustomServerSelector : ServerSelector { override fun select(cluster: ClusterDescription): List<ServerDescription> { return cluster.serverDescriptions.filter { it.type == ServerType.REPLICA_SET_SECONDARY } } }
Use the applyToClusterSettings()
method to pass an instance of this class to your
MongoClientSettings
. The following example shows how to create
a MongoClient
with an instance of the custom server selector class from the preceding example:
val settings = MongoClientSettings.builder() .applyConnectionString(ConnectionString("<connection URI>")) .applyToClusterSettings { builder -> builder.serverSelector(CustomServerSelector()) } .build() val mongoClient = MongoClient.create(settings)
Use Settings to Configure Server Selection
You can specify the following server selection settings in your MongoClient
object or
in your connection URI:
Setting | Description | |||
---|---|---|---|---|
| The latency window for server eligibility. If a server's round trip takes longer than the fastest server's round-trip time plus this value, the server isn't eligible for selection. You can specify this setting to a ClusterSettings object
in addition to the connection URI.Data Type: Integer Default: 15 milliseconds Connection URI Example: localThresholdMS=0 | |||
| The client's default read-preference settings. For more information on read preference
options, see Read Preference in the MongoDB Server manual.
You can specify this setting to a MongoClientSettings object in addition to the
connection URI.Data Type: ReadPreference Default: ReadPreference.primary() Connection URI Example:
| |||
| The length of time the driver tries to select a server before timing out.
You can specify this setting to a ClusterSettings object in addition to the
connection URI.Data Type: Long Default: 30 seconds Connection URI Example: serverSelectionTimeoutMS=15000 |
API Documentation
To learn more about the classes and methods used in this guide, see the following API documentation: