Database Reference
In-Depth Information
Understanding database locks
MongoDB uses read/write locks to prevent conflicts between write/read operations and
also makes sure that all clients receive the same dataset with the same query.
MongoDB uses the readers-writer lock to prevent conflicts and control the read/write oper-
ations.
Note
A readers-writer lock is like a mutex, in that, it controls access to a shared resource, allow-
ing concurrent access to multiple threads for reading, but restricting access to a single
thread for writes (or other changes) to the resource. For more information, please visit ht-
tp://en.wikipedia.org/wiki/Readers%E2%80%93writer_lock .
MongoDB uses the shared read-lock, which means that many operations can use the exist-
ing read-lock. On the other hand, a write-lock is limited to a single operation, which means
that no more write operations can use and share the existing lock.
The MongoDB lock mechanism gets better with each version. Before v2.2, MongoDB had
a global lock per mongod instance for most read/write operations. This means that the lock
is shared between all databases on a MongoDB instance and a lock on a database blocks
operations for all other databases.
After v2.2, this limitation changed and the lock was limited to a database and not all data-
bases of a mongod instance. For instance, if you have three databases in a single instance
and one database creates a lock, then the other two databases become available for write/
read operations.
Furthermore, MongoDB locks are "writer greedy", which means that if both read/write op-
erations are waiting for a lock, the write operation gets more priority, and MongoDB gives
the lock to the write operation first.
Note
You can read more about concurrency and read/write locks at http://docs.mongodb.org/
manual/faq/concurrency/ .
Search WWH ::




Custom Search