Connecting to a replica set is not fundamentally different to connecting to a single server. In this section, we will show some examples using the official mongo-ruby-driver.
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 host:port hostname, in database signals in the 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 then has the same options as it has when connecting to a single server.
The client object should be used as a singleton, created once and reused across our code base.
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 okay with reading just 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. Available options are w, wtimeout, j, 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: :direct, :replica_set, :sharded. |
Symbol |
none |
heartbeat_frequency |
How often replica set members will communicate to check if they are all alive. |
Float |
10 |
database |
Database to connect. |
String |
admin |
Similar to connecting to a standalone server, there are also options around SSL and authentication used in the same way.
We can also configure the connection pool by setting the following:
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 else 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.