- Locking in MongoDB
- Levels of Locking in MongoDB
- Modes of Locking in MongoDB
- Check Locking Status 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.
The reader/writer latch of MongoDB can be best explained as having the following properties:
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.
It is a
single-writer. This means that there can only be one writer at a time to ensure consistency in data.
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.
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 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:
Global-level Locking: This is also known as instance-level locking. This implies that all the databases will be locked.
Database-level Locking: Only the specified database will be locked in this type of locking.
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.
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.
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.
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.
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
rlock is acquired on the database level, it means that an
Rlock can now be acquired on the collection and document (lower) levels.
w: This represents an
Intent Exclusive (IX)lock. It is also a higher-level intent lock.
Similar to how an
rlock works, if a
wlock is acquired on the database level, it means that a
Wlock 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:
You can use this method to monitor the locks on various levels, for example:
- For the global level, use this code
- For the database level, use this code
- For the collection level, use this code
- For monitoring locks in
oplog(operations log), use this code
You can also use the following method:
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.