Making the Most of Monitoring MongoDB
When you've got your application up and running and you are ready to let the world enjoy your work, before the launch celebrations begin, don't forget that you'll want to keep an eye on how well your database is performing. Monitoring your database is an essential part of being production-ready and it's easy to forget it in the run-up to a release.
Don't worry, MongoDB has you covered and we'll show you how you can quickly get monitoring your database, whether locally installed or managed in the cloud with MongoDB Atlas. Let's start with what you can get for free.
Free on Atlas
Every MongoDB Atlas cluster comes with monitoring. If you're not using Atlas, you can get a free M0 tier replica set here and investigate for yourself the dashboard we're about to talk about. Here's a snapshot of the metrics dashboard from a free M0 tier replica set.
What we can see here are graphs from the three nodes in the replica set. All the activity in this example is on the first, primary node. Above the metrics are controls for which time range we are viewing with an adjustable granularity. Run your pointer over a graph and you'll get the precise data for that particular time.
The first row of graphs show the total number of operations being performed on each node; that chart can be broken down into the different operation counters by selecting the check box "Display opcounters on separate charts". The row below that displays information about the number of connections over time.
Scrolling down, we see more charts. This time for the size of the deployment (something we only see like this on smaller clusters and replica sets) and the number of network connections.
You can also toggle the rows or nodes in or out of visibility depending on what you are interested in. This is your essential set of metrics and as we'll show later, as you scale up your Atlas clusters, Atlas automatically gathers more data for you.
Free for Local MongoDB
You may, of course, be developing and using MongoDB locally; MongoDB is the database that you can run anywhere. The good news is we have a free monitoring solution for you and better still, you already have it, built into your MongoDB server. By running db.enableFreeMonitoring()
on your local server, you can get your metrics delivers to our free monitoring service where you can browse the last 24 hours of activity in a chart rich environment. The command itself will return you a URL where you can view your metrics:
You'll also see the URL when you use mongo shell to log into your local database, in case you forget it. Keep that URL safe if you want to keep your metrics private.
Here's a screenshot of a local MongoDB as I was importing a dataset of NYC taxi cabs. You can see CPU usage, the time commands are taking to run and disk utilization.
Immediately we can see that we are predominantly writing, and we're steadily filling a drive with a 650MB dataset. Execution times for these writes are also pretty steady. If we scroll down we'll see more metrics:
Now we can see on this system we're inserting around 30,000 documents a second, memory usage, both resident and virtual, and there's been an associated burst of network traffic (on the local interface of course as this instance of MongoDB is only being accessed locally). Scrolling down further, we can see metrics for operations, replication, and query efficiency:
There's no replication going on and we aren't currently querying the database, so the latter two metrics are at 0. With a more typical workload, there's plenty of performance insight to be had there. Another scroll and finally, we get to queues and CPU stats for the system:
Notice how we can get the breakdown for any particular point in time by moving the cursor over that time on the graph.
So, that's plenty of information about your local MongoDB. Unlike Atlas, free monitoring only holds 24 hours of data on your database, so it's really best used for examining the immediate state of performance on your database.
It's also easy to turn off with db.disableFreeMonitoring()
.
Command-line monitoring
Another set of monitoring tools are available, if you're happy with working at the MongoDB command line. The mongostat
and mongotop
commands offer up a range of performance data, displaying it in the terminal. There are also functions in MongoDB to get metric data such as db.ServerStatus(), db.stats() and the replSetGetStatus admin command.
For all of these though, you'll have to gather and manage the data from them yourself, or find a third-party tool, to implement historical monitoring that you can use for visualization. Also, don't forget your logs; MongoDB server logs are packed with information about your system's performance, allowing you to pinpoint the source of slow queries.
Premium on Atlas
We can't move on without mentioning the rest of the available monitoring on MongoDB Atlas. The free tier monitoring on Atlas is the smallest set of essential monitoring metrics. As you move onto paid clusters, so the number of metrics increases. For example, on an M10 cluster you can see all these:
Not only the database metrics, but also a range of metrics for the underlying hardware and operating system, giving you a full picture of the status of your Atlas cluster. This is all built from logged historical data.
Sometimes, you want to get more immediate information. On Atlas you can get access to the Real Time tab on your Cluster view to get the immediate information on the behavior of your cluster. That includes CPU and disk utilization along with which collections are most active, query execution times and number of operations being run. Think of it as a stethoscope for your database's heartbeat.
Both the deep historical metrics and the real time snapshots come with a larger cluster, all automatically as part of deploying.
So, we have free monitoring on MongoDB Atlas, free monitoring and tools and MongoDB Atlas's premium monitoring tools with real time browsing. Which one to pick? We recommend the one that offers you the most information with the least configuration.