1

I currently use two Azure functions. One to receive webhook requests and add them to a Azure service bus queue. Another to process the queue. The second function reads from and then writes to a MongoDB Atlas database.

My queue processing Azure function app does cache the MongoDB client, so when each function host executes the script, it will reuse the connection if possible. However, presumably Azure function is creating new instances under the load. For reference, here is the caching code:

   const mongodb = require('mongodb');
   const MongoClient = mongodb.MongoClient;
   const uri = process.env["MONGODB_URI"];
   let dbInstance;
   module.exports = async function() {
       if (!dbInstance) {
           client = await MongoClient.connect(uri);
           dbInstance = client.db();
       }
       return dbInstance;
    };

Yesterday, I had an Atlas email notification stating I was nearing the connection limit. Here is the connection spike:

enter image description here

As you can see, it nears my MongoDB Atlas limit of 500 connections.

Is there anyway to terminate these zombie connections, or perhaps reduce the connection TTL?

Alternatively, would it just make more sense to run this queue processor on a traditional server that polls the queue forever? I am currently dealing with ~500 executions a minute, and I simply assumed serverless would be much more scalable. But I am beginning to think a traditional server could handle that load without carrying the risk of overusing DB connections.

ddriver1
  • 723
  • 10
  • 18
  • Are the connection spikes and subsequent drop consistent with your load? Do you see burst loads like the connection count above? Also, based on the number of executions, could you confirm if your function app is scaling out? – PramodValavala Jul 07 '20 at 07:11
  • One quick fix could be to connect and export the `dbInstance` itself instead of an async function, which under load could have concurrent executions despite the conditional. – PramodValavala Jul 07 '20 at 07:12

0 Answers0