SamMan
SamMan

Reputation: 98

Which node is one connected to in c# mongo driver when using replica set

I have a connection string set up as follows:

mongodb://54.225.?.?,54.214.?.?, 54.214.?.?/rs0?connect=replicaset;replicaSet=rs0;slaveOk=true;readPreference=nearest

I have readPreference set to nearest. During testing and production sometimes we would like to guarantee and check if we are communicating with certain nodes that are closest to the client. I understand this is handled automatically but what method through the driver can I use to find out the actual replica set member I am connected to for a specific database call.

For example I have three data centers and I want our application to use the one nearest to it but I want to make sure that we are connecting to it as the application requires maximal performance and reduced latency.

Upvotes: 1

Views: 686

Answers (1)

Asya Kamsky
Asya Kamsky

Reputation: 42352

You are not connected to a single server - you are connected to the entire replica set. Therefore since the driver keeps connections open to all members in the set it knows to send writes to the primary and it knows when it can send certain special reads to a secondary (unless it's a hidden secondary).

If you want to guarantee that reads go to a certain data center, then instead of "nearest" you should use "tags" - that's when you associate tags with members of replica set so that you can specify you want certain reads to only go to members which are tagged with a particular label.

Please note that since the driver determines "nearest" via ping times you may be defeating best performance by forcing it to go to physically nearest data center which may currently be experiencing longer latencies than further away data centers.

Upvotes: 1

Related Questions