Connecting to a replica set is not fundamentally different from connecting to a single server. In this section, we will show some examples that use the official mongo-ruby-driver. We will use the following steps for the replica set, as follows:
- First, we need to set our host and options objects:
client_host = ['hostname:port']
client_options = {
database: 'signals',
replica_set: 'xmr_btc'
}
In the preceding example, we are getting ready to connect to hostname:port, in the database signals in replica_set xmr_btc.
- Calling the initializer on Mongo::Client will now return a client object that contains a connection to our replica set and database:
client = Mongo::Client.new(client_host, client_options)
The client object has the same options it has when connecting to a single server.
- Having a singleton client object is a rule that can be overridden in some cases. We should create different client objects if we have different classes of connections to our replica set.
An example would be having a client object for most operations, and then another client object for operations that are fine with only reading from secondaries:
client_reporting = client.with(:read => { :mode => :secondary })
- This Ruby MongoDB client command will return a copy of the MongoDB:Client object with a read preference secondary that can be used, for example, for reporting purposes.
Some of the most useful options that we can use in our client_options initialization object are as follows:
Option |
Description |
Type |
Default |
replica_set |
As used in our example: the replica set name. |
String |
None |
write |
The write concern options as a hash object; the available options are w, wtimeout, j, and fsync. That is, to specify writes to two servers, with journaling, flushing to disk (fsync) true, and a timeout of 1 second: { write: { w: 2, j: true, wtimeout: 1000, fsync: true } } |
Hash |
{ w: 1 } |
read |
The read preference mode as a hash. Available options are mode and tag_sets. That is, to limit reads from secondary servers that have tag UKWrites: { read: |
Hash |
{ mode: primary } |
user |
The name of the user to authenticate with. |
String |
None |
password |
The password of the user to authenticate with. |
String |
None |
connect |
Using :direct, we can force treat a replica set member as a standalone server, bypassing auto-discovery. Other options include: :direct, :replica_set, and :sharded. |
Symbol |
None |
heartbeat_frequency |
How often replica set members will communicate to check whether they are all alive. |
Float |
10 |
database |
Database connection. |
String |
admin |
Similar to connecting to a standalone server, there are also options for SSL and authentication that are used in the same way.
We can also configure the connection pool by setting the following code:
min_pool_size(defaults to 1 connection),
max_pool_size(defaults to 5),
wait_queue_timeout(defaults to 1 in seconds).
The MongoDB driver will try to reuse existing connections, if available, or it will open a new connection. Once the pool limit has been reached, the driver will block, waiting for a connection to be released to use it.