Skip to content

Why do Mongoose v6 and v7 cause a significant increase in garbage collections and event loop latency compared to v5? #13456

@emilisaksson

Description

@emilisaksson

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the performance issue has not already been reported

Last performant version

5.12.4

Slowed down in version

6.11.1

Node.js version

16.18.0

🦥 Performance issue

Background:
We have been trying to migrate from Mongoose v5 to v6 in our SaaS platform for the past year with no luck. The issues described here also occurs when running Mongoose 7.
Our software have a decent amount of web traffic(about 25 requests per second) and is using a single MongoDB Atlas cluster where each SaaS client is using its own database.
The software is hosted on Heroku.

The issue:
When we deploy a version of our software that is using Mongoose 6 or 7 it runs fine for up to an hour, then we start to see an increase in number of garbage collections, time spent in garbage collection, and event loop latency. This continues until the servers event loop is completely hogged and cannot handle any more incoming requests.
The memory usage is unaffected, so it doesn't seem to be a memory leak.

Steps to Reproduce

The code:

Upon start, we create a database connection with a pool of 300 connections:

global.databaseConnection = await mongoose.createConnection(global.config.dbConnectionString, {
	maxPoolSize: 300,
	minPoolSize: 300
});

When a request for a specific client is received, a connection to that specific clients database is drawn from the connection pool using the useDb-function:

clientDatabaseConnection = global.databaseConnection.useDb(`client-${client.databaseName}`, {
	noListener: true,
	useCache: true
});

The client database connection is then used to do various quesries required by the request, for example:

let clientUserCollection = connection.model<IClientUserModel>("ClientUser", ClientUserSchema, 'users');
let user = await clientUserCollection.findById(userId).exec();

This is basically the structure of the application. What is it that is different in v6 and v7 compared to v5 that could cause these performance implications?

Expected Behavior

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions