HiveBrain v1.2.0
Get Started
← Back to all entries
patternMinor

Write Locks in Mongo DB

Submitted by: @import:stackexchange-dba··
0
Viewed 0 times
mongolockswrite

Problem

I read somewhere that MongoDB holds db level write lock, does this mean that when someone updates any collection others are not allowed to read other collection or how it works. Also when someone writes anything to the db how MongoDB acquires write lock and what is its granularity?

Solution

Your interpretation matches the "How granular are locks in MongoDB?" part in MongoDB FAQ:


How granular are locks in MongoDB?


Beginning with version 2.2, MongoDB implements locks on a per-database
basis for most read and write operations. Some global operations,
typically short lived operations involving multiple databases, still
require a global “instance” wide lock. Before 2.2, there is only one
“global” lock per mongod instance.


For example, if you have six databases and one takes a write lock, the
other five are still available for read and write.

As for the way the locking works:


What type of locking does MongoDB use?


MongoDB uses a readers-writer
[ed: also known as “multi-reader” or “shared exclusive”] lock that allows concurrent
reads access to a database but gives exclusive access to a single write
operation.


When a read lock exists, many read operations may use this lock.
However, when a write lock exists, a single write operation holds the
lock exclusively, and no other read or write operations may share the
lock.


Locks are “writer greedy,” which means writes have preference over
reads. When both a read and write are waiting for a lock, MongoDB
grants the lock to the write.

Context

StackExchange Database Administrators Q#49643, answer score: 7

Revisions (0)

No revisions yet.