16. MongoDB client connection problem
MongoDB is a document-oriented database used to build highly available and scalable applications. MongoDB solution is for high-volume data storage and uses collections and documents, unlike traditional databases. It has been noticed that when a client is connected to MongoDB, multiple connections are created. This document intends to explain why.
16.1. MongoDB in ArmoniK
MongoDB is used in ArmoniK as database to store the metadata of the tasks, resuts, sessions and authentication. Each compute compute must connect to MongoDB database. We have in ArmoniK several connections to MongoDB per pod. The number of connections often varies between 3 and 4 or more per compute pod.
16.2. Mongosh output interpretation
We used compass, the MongoDB GUI, to connect and monitor the client connection to MongoDB. The db.serverStatus().connections command is used in Mongosh to display the status of the client.
connections : {
current : <num>,
available : <num>,
totalCreated : <num>,
active : <num>,
threaded : <num>,
exhaustIsMaster : <num>,
exhaustHello : <num>,
awaitingTopologyChanges : <num>,
loadBalanced : <num>
},
We will not discuss all the output values only relevant ones, the full description of this command can be found in mongosh documentation.
connections.currentThe number of incoming connections from clients to the database server. This number includes the current shell session. Consider the value ofconnections.availableto add more context to this datum.connections.activeThe number of active client connections to the server. Active client connection refers to client connections that currently have operations in progress.
These two parameters are giving the number of current connections to MongoDB and active connections. The current connections are to high compared to the number of clients we are running. For a deployment with a default (3) number of compute pods, the curent connections are about 42 while the active connections is around 10, which is still high since we have only one client running. Althought, our client have multiple services that may open connections, but still below the active connection number shown in mongo shell. The reason of the extra connections are discussed in the next section.
16.3. MongoDB creates multiple connections: Why
The active client connections seen from the value connections.active is not the real connection number to MongoDB. This number is greater than the actual connection number because MongoDB is creating additional connections for future clients that may potentially connect to the database.
An explanation by the Technical Service Engineers for MongoDB of this behaviour is provided in the message forum: So simply put, the connections themselves aren’t client connections that you’re seeing. What you’re seeing is a series of redundant connection pathways that are waiting on more and more clients. As you get more clients the connections will begin to even out and disperse to balance the loads of an ever-increasing number of clients. (Clients = Users/Devices/Instances of your app)
So additional connections are created for potential new clients when they try to connect. The number of available connections to MongoDB is around 800000 connections per mongodb instance. This number can be influenced by clustering, and the size and configuration of the underlying machine. Therefore, this is not a major issue in our case for now. However, it will be nice to have control on these parameters in order to see the actual connection to MongoDB and fix the number of additional connections we desire for our application. The MaxConnecting option, which intends to fix the maximum number of concurrent connections to MongoDB, does not seem to have an effect on those extra connection numbers.