title: Python mongoengine - is it possible to pool connections across db_aliases, rather than opening a connection per db_alias to prevent many connections?
body: <p>I would be very grateful for your help with the scalability profile of connections to MongoDB atlas.</p>
  <p>I am using mongoengine in a Python Flask web app to read/write data to MongoDB Atlas. We have a growing number of customers and each customer gets their own database, hosted within a single Atlas cluster.</p>
  <p>In the Flask web app, we call the mongoengine connect() method once for each customer, providing the customer name as the db_alias. This gives us a connection to each of the customer's databases.</p>
  <pre><code>from mongoengine import connect
  
  customers = [&quot;customer-1&quot;, &quot;customer-2&quot;]
  host = &quot;connection_string_here&quot;
  for customer in customers:
      connect(alias=customer, host=host)
  </code></pre>
  <p>We also create a set of mongoengine database models for each customer, with the &quot;db_alias&quot; in the meta dict overwritten so that every customer has their own set of models pointing to just their db_alias. The result of this is as follows:</p>
  <p>For Customer 1, their model will be like this:</p>
  <pre><code>class Item(Document):
      name = StringField()
  
      meta = {
          'collection': 'items',
          'db_alias': 'customer-1'
      }
  </code></pre>
  <p>For Customer 2, their model will be like this:</p>
  <pre><code>class Item(Document):
      name = StringField()
  
      meta = {
          'collection': 'items',
          'db_alias': 'customer-2'
      }
  </code></pre>
  <p>The problem is, as the number of customers grows, we have a growing number of connections, as each customer requires its own call to connect() which creates a separate pymongo MongoClient, and an underlying pool of connections per customer.</p>
  <p>For example, 100 customers * 3 connections in that customer's connection pool * 4 worker processes of the Flask web app * 3 VMs running the webapp for load balancing/redundancy.</p>
  <p>It quickly adds up to 3000 - 4000 connections which exceeds the MongoDB Atlas limits and this will continue to get worse with a growing customer base.</p>
  <p>My question is this: Is there a way to maintain a pool of connections that are independent of any single customer's db_alias, but are connected to the MongoDB instance and can be &quot;pointed&quot; to a customer's db_alias on a per-query basis, and then released back to the pool?</p>
  <p>This would allow only 10-50 connections to be kept open per flask process, and no matter the number of customers, they could all share the same 10-50 connections.</p>
  <p>I am not sure if there is something I have misunderstood in the docs or source code, or if connections have to scale with the number of db_aliases in mongoengine.</p>
  <p>Many thanks for your help,
  Robin</p>