Next.js Serverless High connection count to mongodb despite following best practices

Hi all,

I have a Next.js serverless app and it consistently uses about 120 connections when interacting with my platform, even though it is just me using it. I have about 10-20 api calls while navigating through my frontend. I am wondering if this is a normal amount?

I followed the best practices listed here: ( Specifically the second example. I also incorporated a cached variable for the DB instance, so that I re-use the cachedDb if it exists.

Here is my code:

import * as client from "./mongodb-client";

// Handler

let cachedDb;

export const connectToDB = async () => {
  if (cachedDb) {
    console.log("connectToDB: using cachedDb");
    return { db: cachedDb, dbClient: client };
  // Get the MongoClient by calling await on the connection promise. Because
  // this is a promise, it will only resolve once.
  const connectedClient = await client.connect();

  cachedDb = connectedClient.db(process.env.DB_NAME);
  // Use the connection to return the name of the connected database.
  return { db: cachedDb, dbClient: connectedClient };

import { MongoClient } from "mongodb";

// Export a module-scoped MongoClient promise. By doing this in a separate

// module, the client can be shared across functions.

const options = {

connectTimeoutMS: 10000,

useUnifiedTopology: true,

useNewUrlParser: true,


const client = new MongoClient(process.env.DATABASE_URL!, options);

module.exports = client;