MongoDb Driver 2 - Singleton Instance Resilience [duplicate]
Asked Answered
N

1

8

Does anyone have any prior experience of working with the MongoDB C# Driver (v2+) with singleton style connection/collection instances in terms of connection resilience?

The Problem

We have recently made a push to secure our wider infrastructure, essentially running everything over SSL even internally. As such we recently secured one of our primary MongoDb clusters. One of the most immediate differences was the increase in cost of connecting to the cluster. Our old solution followed a typical repository pattern whereby a subscriber to a message would instantiate the repository and also new up a connection before allowing it be GCd after the processing of that message. We saw a pretty major drop in message processing performance as a result of switching to SSL - more than we'd honestly expected!

The Question(s)

Is there a recommended way to work with IMongoClient, IMongoDatabase, IMongoCollection<T> with respect to long-lifetime instances (weeks/months). Does it gracefully handle..

  1. Specific nodes going down within the cluster (but the cluster remaining available)
  2. The entire cluster going offline (e.g. major network outage)
  3. Would setting larger timeouts etc. be wise and what's the impact?

My current understanding is that 1. should be no problem, for 2. it attempts to buffer operations within the timeout window and process them once the cluster is available again. 3. depends largely on memory and timeliness of the operation (something we'll need to think about internally).

If the cluster is down longer than the various timeouts allow I'm assuming an exception is thrown at which point I'm also assuming that my instance(s) would become useless unless I can tell the driver to retry connecting to the cluster indefinitely. To me this appears to be the grey area when it comes to documentation and my own research - can anyone offer more reading material or suggestions for ways to cope with network faults etc for this sort of scenario?

Many thanks in advance,

Novelist answered 16/5, 2017 at 8:58 Comment(0)
N
0

Answering my own question. You should try and re-use IMongoClient wherever possible - ideally, it should be a singleton. IMongoDatabase and IMongoCollection<T> can be created as needed. See the "Re-use" sections of the Mongo C# driver documentation.

The IMongoClient implementation will maintain a long-lived connection to the server and will reconnect in the event of network failure.

Novelist answered 28/5, 2020 at 7:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.