This is my original tutorial, republished on this website. The examples are preserved. Tool versions and interfaces may have changed.
MongoDB provides monitoring commands and tools to enhance the performance of your database. This document will help you understand various metrics that you can measure to monitor your MongoDB instance and MongoDB Atlas instance.
Why monitor MongoDB?
By adequately monitoring MongoDB, you can quickly spot slowdowns and bottlenecks, understand the root causes of resource limitations, and enhance the performance and scalability of your database. Monitoring MongoDB databases allows you to:
Understand the current capacity of your database
Observe the utilization of resources
Observe the presence of abnormal behavior
Have diagnostic data to solve any identified problems
Ensure data protection for your clients.
How to monitor MongoDB?
Metrics allow us to assess MongoDB databases. But first, to monitor a MongoDB database, you must:
View cluster-related metrics like replica sets, replication lag, sharded cluster, MongoDB processes, disk space, memory usage, and number of connections.
Use the built-in Free Monitoring feature to get information on Operation Execution Time, Memory Usage, CPU Usage, and Operation Counts.
To configure the monitoring of MongoDB Enterprise on Linux, refer to this documentation.
To configure the monitoring of MongoDB Enterprise on Windows, follow this documentation.
Configure and resolve alerts.
View and download MongoDB Logs.
Integrate third-party monitoring tools.
What MongoDB metrics to monitor?
You can still use the following metrics to understand your MongoDB instance.
While monitoring Mongodb metrics, you should look for the following:
Replication lag.
The time it takes to copy data from the primary node to the secondary node is known as Replication lag. If the time it takes is high, we have a high Replication lag. If the time is slow, we have a slow Replication lag. We aim for a low Replication lag.
Disk utilization
Disk utilization refers to disk access. As your client base grows, the number of read and write requests sent to the disk and the required disk space will also increase. Monitoring the disk utilization helps us to understand the need for horizontal and vertical scaling and avoid resource saturation and page faults.
Memory usage
Monitoring memory usage gives us information on how much and how memory is being used. For specific applications, read operations require more memory. For those that store data, write operations require more memory usage. Proper utilization helps us to maintain the correct balance between memory distribution and the frequency of read and write operations. This balance ensures a smooth operating database and application.
Number of connections
This refers to the number of connections the database has open to serve requests as quickly as possible. To ensure port availability, a database should keep connections open for the least amount of time necessary to complete a request. At the same time, we do not want our clients to be waiting for an open connection. So it's important to monitor the number of open connections in the database, verify that they are being closed, and at the same time, keep a certain number of connections available for incoming requests.
Journaling
Journaling ensures the availability and durability of data in the event of failure. WiredTiger (the default MongoDB storage engine starting MongoDB 3.2) records all the data modifications between checkpoints. Refer to WiredTiger Storage Engine for more information.With information from the journal, you can observe various MongoDB instances and decide whether to scale up your database, understand collections with greater throughput of data and dedicate more disk space and memory to it and overall make decisions to reduce the performance issues of the database.
MongoDB performance monitoring tools
Let’s understand the tools to measure these metrics. These are the tools you can run on Mongo shell.
mongostat Command
Mongostat is used to get a quick overview of the status of your MongoDB server instance. It’s best used for watching a single instance for a specific event as it provides a real-time view. This command can monitor basic server statistics such as CPU, Memory, Disk IO, and Network traffic. It retrieves the counts of database operations by type, such as insert, query, update, and delete. Use this tool to derive the value of performance metrics that you require.You can execute the MongoDB command through the following syntax:
mongostat <options> <connection-string> <polling interval in seconds>
Run mongostat from the system command line, not the mongo shell.
mongotop Command
Mongotop tracks how much time a MongoDB instance spends reading and writing data. It gives you primarily an understanding of disk utilization and memory usage. Further metrics can be derived depending on how you use it.You can execute the mongoDB command through the following syntax:
mongotop <options> <connection-string> <polling interval in seconds>
rs.status() Command
Rs.status returns the replica set status. It is done from the member's point of view where the method is run. This method provides a wrapper around the replSetGetStatus command.You can execute the mongoDB command through the following syntax:
db.adminCommand( { replSetGetStatus: 1 } )
serverStatus Command
When you want to have an overview of the database’s state, you use the serverStatus command. It provides you with a document with an overview of the database’s state. Run this command at a regular intervals to collect statistics about the instance.You can execute the mongoDB command through the following syntax:
db.runCommand( { serverStatus: 1 } )
dbStats Command
The dbStats command returns us the storage statistics for a certain collection, such as the value of the amount of storage used and available, the quantity of data contained, index counters, and such.You can execute the mongoDB command through the following syntax:
collStats command is used to collect statistics similar to that provided by dbStats on the collection level. Its output includes a count of the objects in the collection, the size of the collection, the amount of disk space consumed by the collection, and information concerning its indexes for a given collection.You can execute the mongoDB command through the following syntax:
If you want the replica's status set from the server's point of view, use the command replSetGetStatus. Remember, replSetGetStatus must be run against the admin database, and the mongod instance must be a replica set member for replSetGetStatus to return successfully.You can execute the mongoDB command through the following syntax: