callbackWaitsForEmptyEventLoop for Azure?


We are working on server-less solutions in Azure and using a lot of Node.js functions to connect to Atlas.

It’s turned out that we have “connections” problem. Let me explain what is the problem:

  • Every time when a new function instance is fired up by Azure, we need to fire up a connection to Atlas and then function become active, let’s say for 5 min. Then a new a new function instance generates and a new connection is needs to be created.

  • But then in our Test environment we’ve got 14000 connections very quickly, we were very supersized.

By looking at the performance for this solution, we realized it would be much more efficient to create one Atlas connection and use it across all node.js function instance withing one microservice.

We have done some research and it’s turned out that the same problem happened in AWS cloud, when people use Lambda functions.

We have found this article: and as a best practice it’s recommended to use “callbackWaitsForEmptyEventLoop”. But the problem that we can’t find analog in Azure functions.

Can someone please help us?

Hi Dmitry,

For Azure functions, it is recommended to create a static client for MongoDB outside the event handler, to reuse connections across function invocations, instead of recreating the connection each time.

This is one simple example
const MongoClient = require(‘mongodb’).MongoClient;
const uri = “URI from config”;

//Hold client across function invocations
let client = null;

module.exports = async function (context, req) {
// Connect using MongoClient
if (client == null) {
      await MongoClient.connect(uri, function (err, _client) {
      client = _client;
      console.log("(Re)Created Client");
//Read documents in the collection
const collection = client.db("dbName").collection("collectionName");
collection.find({}).toArray(function (err, docs) {
     console.log("Found the following records");

The example provided in the blog post for AWS you referenced will work too.

Azure functions do not support callbackWaitsForEmptyEventLoop , There is an open GitHub issue for that -

Hope this helps.