Typically you only create one MongoClient instance for a given cluster and use it across your application. Creating multiple MongoClients will, however, still share the same pool of connections if and only if the connection strings are identical.
Source: http://mongodb.github.io/mongo-csharp-driver/2.0/getting_started/quick_tour/
Our situation is that we have two different "clusters".
We do not work with replication or sharding, but we have two different databases.
It means that some of our API functions will fetch data from one database, whereas other API functions get the data from the second database. This is hard coded.
We created a SelectDatabase method in each class, which then creates a MongoClient with the given settings.
The same class will always access the same database.
I expected that the driver would handle then the number of seconds in an "optimum way".
But it turned out, that each time I refresh the browser, a new Web API Controller call is done and therefore also a new SelectDatabase call. And that resulted in a new socket every time (checked with TCPView).
So of course each API Controller call should cause SelectDatabase, but I think it is an overkill to create a socket each time.
We are creating new MongoClients like this:
//Creating multiple MongoClients will, however, still share the same pool of connections
//if and only if the connection strings are identical.
MongoClientSettings clientSettings1 = new MongoClientSettings()
{
Server = new MongoServerAddress(host, port),
ClusterConfigurator = builder =>
{
builder.ConfigureCluster(settings => settings.With(serverSelectionTimeout: TimeSpan.FromSeconds(serverSelectionTimeoutInSeconds)));
}
};
What is the optimum number of simultaneously opened Sockets (between Web Application and Database), respectively MongoClient instances, in our Project situation?
Web Browser "Socket" Life cycle should only cause the Web application to create 1 socket to the database
The Web Application should use just one socket, each per database, completely independent of how many Web Browser or Sockets are open to the web application.
You should open a new connection each time you have a Web API call
I run some Postman test where I created a few hundred API calls and I saw that it was running out of resources respectively into timeouts. I saw that it run better, when I was choosing approach number 2 instead of 3. But I am concerned that the different tasks created when the Web API gets called so often could interfere somehow then on a Socket level to make errors occur.
Update I made two tests: The first was based on the principle that on each request a new MongoClient is generated. TCPView showed me that a new socket is opened on each request. The second test was with a singleton pattern which allows me to access every time the same two MongoClients. The second test only used two sockets. I also need to say that I changed the asynchronous call so that the methods is blocked until it returns (for simplicity I did not want these Task return types in every method).
I made some trials and it turns out that creating multiple MongoClients will NOT share the same pool of connections if you used a MongoClientSettings object, even if the connection string parameters (host, port) are the same.
MongoDB documentation probably meant "connection strings" literally, that is, only strings.
In short, the following works as intended:
_client = new MongoClient("mongodb://localhost:27017");
But not the following:
MongoClientSettings clientSettings = new MongoClientSettings()
{
Server = new MongoServerAddress(host, port),
ClusterConfigurator = builder =>
{
builder.ConfigureCluster(settings => settings.With(serverSelectionTimeout: TimeSpan.FromSeconds(serverSelectionTimeoutInSeconds)));
}
};
_client = new MongoClient(clientSettings);
Even if you used the very same MongoClientSettings object from a singleton, you still get different connection pools. I also verified that.