Locking in MongoDB

Bilal Shahid Dec 16, 2022
  1. Locking in MongoDB
  2. Levels of Locking in MongoDB
  3. Modes of Locking in MongoDB
  4. Check Locking Status in MongoDB
  5. Conclusion
Locking in MongoDB

In database management systems, the locking mechanism ensures consistency throughout the results.

For example, a read command cannot be executed simultaneously if some write procedure is done on the data. The database resources are locked to prevent inaccuracy in data resulting from such situations.

MongoDB also uses locking to ensure data consistency since multiple clients can access or modify the same data concurrently. In this article, we will be explaining the concept of locking in MongoDB.

Locking in MongoDB

Locking in MongoDB works differently than locking in other relational database management systems. Let us describe to you how locking works in MongoDB.

MongoDB makes use of multi-granularity locking. This means the processes can be locked at the global, database, or collection level or even at the document level for individual storage engines.

These levels of locking will be described further in the article. In the recent versions of MongoDB, starting from v2.2, there exists a reader/writer latch for each database.

Due to these reader/writer latches, concurrent access to the database resources is possible. Let us explain how these work.

Reader/Writer Latch

The reader/writer latch of MongoDB can be best explained as having the following properties:

  1. It is a multiple-reader. This means that any number of readers can access the collections simultaneously without affecting the consistency of the data since the data is not being modified, only read.

  2. It is a single-writer. This means that there can only be one writer at a time to ensure consistency in data.

  3. It is writer-greedy. Being writer greedy means that writers are prioritized over readers.

    Therefore, if a writer requests to use the database, all the incoming readers are temporarily blocked (or locked out) until they complete their job.

    However, the writer waits until the current readers leave. This implementation prevents the writers from being starved indefinitely.

Note: The reader/writer latch is called the reader/writer lock. However, calling it a latch is more appropriate since it is lightweight.

Due to the presence of this latch in MongoDB, any concurrent queries can be executed without major locking conflicts.

Here, the writer latch gets interesting because, in MongoDB, everyone writing operations to a single document is considered atomic. Since the write operation is atomic, the writer latch is held by the MongoDB kernel only for the time it takes to update a single document.

Therefore, if a slow-running write operation is executed (owing to poor schema design or paging a document from disk, for example), it is said to have yielded the latch.

Another thing to note is that, as specified earlier, MongoDB has a separate reader/writer latch for each database. To explain this, suppose we have two databases, A and B.

For a write operation in database A, the writer will acquire a separate latch compared to the write operations in database B.

Similarly, if multiple connections to the database perform write operations concurrently, the latches will be held on a per-database basis. Further, these simultaneous connections will be interleaved.

Note: It may feel like that because of locking, the performance and speed of the database system are affected. In reality, this is not much of a concern since, for the average load, MongoDB saturates the I/O capacity of the disk or SSD before the lock percentage on a database becomes greater than 50%.

Therefore, we say that locking in MongoDB is not per connection. Rather, it is per mongod.

mongod refers to the MongoDB system’s primary background process, which manages data requests, data access, and other background operations.

Levels of Locking in MongoDB

In MongoDB, there are four levels of locking:

  1. Global-level Locking: This is also known as instance-level locking. This implies that all the databases will be locked.
  2. Database-level Locking: Only the specified database will be locked in this type of locking.
  3. Collection-level Locking: In MongoDB, a collection is a group of related documents (similar to a table in traditional RDBMS). Collection-level locking handles lock for individual collections.
  4. Document-level Locking: A document in MongoDB can be referred to as a record having field and value pairs. This type of locking locks the particular document only.

Modes of Locking in MongoDB

In MongoDB, there are four modes of locks, explained below.

  1. R: This represents a Shared (S) lock. This locking mode is used for readers.

    The resources will be shared among the readers, accessing them concurrently in this mode.

  2. W: This represents an Exclusive (X) lock. This locking mode is used for writers.

    In this mode, the resources will not be available for any other concurrent readers or writers.

  3. r: This represents an Intent Shared (IS) lock. It is a higher-level intent lock.

    Such locks are acquired before moving to lower-level locks. For example, if an r lock is acquired on the database level, it means that an R lock can now be acquired on the collection and document (lower) levels.

  4. w: This represents an Intent Exclusive (IX) lock. It is also a higher-level intent lock.

    Similar to how an r lock works, if a w lock is acquired on the database level, it means that a W lock can now be acquired on the collection and document (lower) levels.

Check Locking Status in MongoDB

So, is there a way to check the status of the locks on your mongod instance? Yes, you can do so by using the following method:

db.serverStatus()

You can use this method to monitor the locks on various levels, for example:

  1. For the global level, use this code db.serverStatus().globalLock.
  2. For the database level, use this code db.serverStatus().locks.Database.
  3. For the collection level, use this code db.serverStatus().locks.Collection.
  4. For monitoring locks in oplog (operations log), use this code db.serverStatus().locks.oplog.

You can also use the following method:

db.currentOp()

Conclusion

This article explained the concept of locking in MongoDB, followed by its various levels and modes. We hope you were able to grasp these concepts.

Author: Bilal Shahid
Bilal Shahid avatar Bilal Shahid avatar

Hello, I am Bilal, a research enthusiast who tends to break and make code from scratch. I dwell deep into the latest issues faced by the developer community and provide answers and different solutions. Apart from that, I am just another normal developer with a laptop, a mug of coffee, some biscuits and a thick spectacle!

GitHub