Monitoring Tooling / Performance Analysis with db.serverStatus and db.stats

4:36
So you want to unlock the secrets of your database's performance at a more granular level. Let's take a look at some of MongoDB's built in tools to do this. In this video, we'll explore two MongoDB shell methods, server status and stats. These commands provide detailed metrics about any MongoDB deployment. While it's beyond the scope of this video to discuss all the possible metrics to look at, it's worth noting that the methods allow you to extract specific metrics and build custom monitoring solutions. Let's start by taking a look at the overall health of our MongoDB server with the server status method. The server status method provides a wrapper around the server status database command and gives us a real time view of our server's health. It's very important to understand that the server status command output represents all databases and activity, so we can't use it to drill down into the performance of a single database or collection. To use server status, we need to be running the MongoDB shell. Once logged in to the MongoDB shell, we can execute the method as seen here. Woah. That is a lot of useful information, but it's more than we need right now. For more information on all of the metric categories, be sure to check out the MongoDB documentation. Fortunately, we can limit the output to the areas that we want. For now, let's just look at information about operation counts and locks. We'll limit the output by appending the category name to the server status method. Let's start with operation counts. To get the operation counts information, we execute the server status method with op counters appended to the end like this. This tracks the total number of database operations like queries, inserts, updates, and deletes since the last server restart. High op counters for specific operations help us identify which query types to focus our optimization efforts on. To understand the rate of operations and identify potential bottlenecks, we need to compare the output of server status taken at different points in time. For instance, running the method a few times during a period of time and comparing the differences in the counters could give you a sense of the increased operational load. Now let's look at the locks category. It provides information about concurrency, which is your database's ability to handle multiple operations at the same time. We pull the information by appending locks to the end of the server status method. High values and the time acquiring micros field can indicate that multiple operations are competing to acquire the same resource, slowing down performance. Finally, let's zoom out a bit and look at the overall health of our databases. The stats method summarizes key metrics related to storage, document count, and indexing. It's a useful tool for seeing how efficiently your database and indexes are using available disk space. To use stats, we need to be running the MongoDB shell. The method returns a JSON document containing statistics about the currently selected database. This gives us high level statistics about our database's resource consumption, including the number of collections, the total storage size, the number of documents, and details about our indexes. There is a lot of information here, so let's talk about some fields that may be useful for you to monitor. First, we wanna pay close attention to the data size value. This tells us how much disk space our database is currently occupying. An unexpected increase in data size during testing may highlight issues such as redundant or outdated data occupying disk space, which could lead to increased latency or decreased throughput. Next, the objects field shows the total number of documents across all collections in the database. The indexes field tells us how many indexes we have, and the index size indicates the total size of all indexes. Large index size relative to data size suggests over indexing, meaning we should review and potentially consolidate indexes. Check out our skill on indexing design for more on that topic. Lastly, high data size with low objects count might indicate large documents that could benefit from schema redesign or compression. Congratulations. In this video, we learned how to use the server status method to get a real time snapshot of the MongoDB Server, focusing on operation counts and locks to understand server load and concurrency for all databases. Then we use the stats method to get a high level overview of the database, including storage size, document count, and index information to assess overall resource consumption.