Locking in MongoDB
- Locking in MongoDB
- Levels of Locking in MongoDB
- Modes of Locking in MongoDB
- Check Locking Status in MongoDB
- Conclusion
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:
-
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. -
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:
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 aShared (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 anExclusive (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 anIntent 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 anR
lock can now be acquired on the collection and document (lower) levels. -
w
: This represents anIntent Exclusive (IX)
lock. It is also a higher-level intent lock.Similar to how an
r
lock works, if aw
lock is acquired on the database level, it means that aW
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:
- For the global level, use this code
db.serverStatus().globalLock
. - For the database level, use this code
db.serverStatus().locks.Database
. - For the collection level, use this code
db.serverStatus().locks.Collection
. - For monitoring locks in
oplog
(operations log), use this codedb.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.
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