Overview
The MongoDB Ruby Driver follows a defined algorithm or the read preference defined in the client options when selecting a server to read or write from.
Server Selection Algorithm
When the Ruby driver executes a read operation, it performs the following steps, in order, to select a MongoDB deployment:
From the list of known servers, the Ruby driver selects all servers that match the active read preference.
The Ruby driver removes any server from the list that doesn't meet the
localThresholdMS
connection setting criteria, and returns the updated list.The Ruby driver selects a server at random from the servers still on the list and executes the operation against this server.
When the Ruby driver executes a write operation, it begins by selecting all writeable servers, not just those that match the active read preference. The remaining steps are identical.
To learn more about the server-selection algorithm, see the Server Selection Algorithm in the MongoDB Server manual.
Customize Server Selection
For replica sets, you can select a read preference to determine which member the Ruby driver reads from. For more information about how to configure a read preference, see the Read Preference section of the Configure CRUD Operations guide.
API Documentation
For more information about the Ruby driver's server-selection algorithm, see the following API documentation: