Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.NET best practices for MongoDB connections?

I've been playing with MongoDB recently (It's AMAZINGLY FAST) using the C# driver on GitHub. Everything is working just fine in my little single threaded console app that I'm testing with. I'm able to add 1,000,000 documents (yes, million) in under 8 seconds running single threaded. I only get this performance if I use the connection outside the scope of a for loop. In other words, I'm keeping the connection open for each insert rather than connecting for each insert. Obviously that's contrived.

I thought I'd crank it up a notch to see how it works with multiple threads. I'm doing this because I need to simulate a website with multiple concurrent requests. I'm spinning up between 15 and 50 threads, still inserting a total of 150,000 documents in all cases. If I just let the threads run, each creating a new connection for each insert operation, the performance grinds to a halt.

Obviously I need to find a way to share, lock, or pool the connection. Therein lies the question. What's the best practice in terms of connecting to MongoDB? Should the connection be kept open for the life of the app (there is substantial latency opening and closing the TCP connection for each operation)?

Does anyone have any real world or production experience with MongoDB, and specifically the underlying connection?

Here is my threading sample using a static connection that's locked for insert operations. Please offer suggestions that would maximize performance and reliability in a web context!

private static Mongo _mongo;  private static void RunMongoThreaded() {     _mongo = new Mongo();     _mongo.Connect();      var threadFinishEvents = new List<EventWaitHandle>();      for(var i = 0; i < 50; i++)     {         var threadFinish = new EventWaitHandle(false, EventResetMode.ManualReset);         threadFinishEvents.Add(threadFinish);          var thread = new Thread(delegate()             {                  RunMongoThread();                  threadFinish.Set();             });          thread.Start();     }      WaitHandle.WaitAll(threadFinishEvents.ToArray());     _mongo.Disconnect(); }  private static void RunMongoThread() {     for (var i = 0; i < 3000; i++)     {         var db = _mongo.getDB("Sample");         var collection = db.GetCollection("Users");         var user = GetUser(i);         var document = new Document();         document["FirstName"] = user.FirstName;         document["LastName"] = user.LastName;          lock (_mongo) // Lock the connection - not ideal for threading, but safe and seemingly fast         {             collection.Insert(document);         }     } } 
like image 549
Tyler Brinks Avatar asked Feb 03 '10 17:02

Tyler Brinks


People also ask

Does .NET support MongoDB?

Accessing the MongoDB database from an ASP.NET Core Web API application. The first thing we will do is to add the MongoDB driver NuGet package. This will allow us to access the MongoDB database via friendly APIs. Once this is done, we can proceed to start adding the code.

Can we use MongoDB with .NET core?

Designing a Document Model and Database Service within . NET Core. Before we start designing each of the RESTful API endpoints with . NET Core, we need to create and configure our MongoDB service and define the data model for our API.

How many concurrent connections can MongoDB handle?

MongoDB configuration Even MongoDB itself has an option to limit the maximum number of incoming connections. It defaults to 64k.


2 Answers

Most answers here are outdated and are no longer applicable as the .net driver has matured and had numberless features added.

Looking at the documentation of the new 2.0 driver found here: http://mongodb.github.io/mongo-csharp-driver/2.0/reference/driver/connecting/

The .net driver is now thread safe and handles connection pooling. According to documentation

It is recommended to store a MongoClient instance in a global place, either as a static variable or in an IoC container with a singleton lifetime.

like image 117
runxc1 Bret Ferrier Avatar answered Oct 04 '22 09:10

runxc1 Bret Ferrier


The thing to remember about a static connection is that it's shared among all your threads. What you want is one connection per thread.

like image 36
Joel Coehoorn Avatar answered Oct 04 '22 08:10

Joel Coehoorn