MongoDB Management Service (MMS) tracks dozens of MongoDB-specific metrics. In this post, we’ll dive into a critical statistic – lock percentage.
What is locking in MongoDB?
In order to allow multiple clients to read and write at the same time, MongoDB employs a locking system. This lock system ensures that the clients reading and writing to the MongoDB database each have the same view of the data. It also prevents the clients from making conflicting modifications at the same time.
Prior to version 2.2, MongoDB employed a single reader/writer lock per mongod instance. Since MongoDB version 2.2, concurrency has improved and MongoDB also implements a more granular, database-level lock. In addition, long-running operations may now yield their lock if certain conditions are met. Global lock still exists, however, it is used for widespread maintenance and is used very rarely.
The concurrency FAQ includes more details on how locking works in MongoDB.
Should I track lock percentage?
Lock contention can cause the performance of any database to degrade, and MongoDB is no exception to that rule. A high lock percentage will have impacts across the board: it can starve replication, make application calls slow, timeout, or fail. Lock contention generally needs to be kept within a reasonable threshold for a database to function smoothly.
What is a normal lock percentage for a healthy MongoDB system?
The normal level will vary greatly per database, though it’s difficult to imagine a healthy system with a lock percentage (averaged over a minute) over 80%. Of course, “normal” depends very much on your use case. A write-heavy database might regularly see >60% lock whereas a read-heavy database may never see anything above 10%.
Using MMS, you’ll want to examine trends over time to understand what is normal for your application, establish your baseline, and then track anything that is significantly over that baseline.
How is lock percentage measured in MMS?
On the MMS hosts page, the lock% chart will show you the percentage of time you are waiting for any particular database lock. You can also choose “global” as the lock to inspect. Oddly enough, doing so does not show the percent time waiting for the global lock. Instead, choosing “global” displays a derived metric that adds the percent of time spent in the global lock (typically a very small number) plus the percent of time locked by the hottest database at the time of measurement, where hottest means “most locked.” Because the data is sampled and combined, it is possible to see values over 100%.
What does lock percentage look like in MMS?
Here is an example chart: