4

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?

6

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.

| improve this answer | |
  • how it is going to behave in sharded environment? – Phalguni Mukherjee Sep 11 '13 at 11:40
  • The MongoDB FAW I linked in my answer has also the answer for that question :) (shortly, the instances are independent in this regard) – zagrimsan Sep 11 '13 at 12:14
4

The lock scope has been moving in the various MongoDB versions. They're getting closer to the data over time.

  • Versions before 2.2 had a global lock per MongoDB instance.
    • A lock held on one database on the instance blocked writes to all databases on that instance.
  • As of v2.2, this was changed to be a database lock.
    • A lock on one collection would block writes to the entire database.
  • Collection level locks are on the future feature list but are not yet on the road-map (the JIRA ticket that's tracking this feature).

Mongo is pretty good about lock yielding. The impact of replicating and sharding on the behavior of locks is important if you are building a system that uses those, as the options you choose to implement can affect how much blocking locks affect your system.

| improve this answer | |
  • how it is going to behave in sharded environment? – Phalguni Mukherjee Sep 11 '13 at 11:39
  • @PhalguniMukherjee Simple sharding (no replication) has the lock apply separately to each node's database. Sharding is a way to get around excessive lock-percentages. – sysadmin1138 Sep 11 '13 at 19:08
  • 1
    Currently as of 3.0 version , we have document level locking. – harshavmb Nov 7 '15 at 15:03
2

From MongoDB version 3.0, locks are available at following granularity levels :

  1. Global (MongoD instance) – All databases in it are affected by the lock.
  2. Database – Only the Database on which lock is applied is affected.
  3. Collection – Only the Collection on which lock is applied is affected.
  4. Document – Only the Document on which lock is applied is affected.

Locks for Global, Database and Collection are handled by MongoDB while Document level locking is left to Storage Engine.

References :

  1. https://docs.mongodb.com/manual/faq/concurrency/#what-type-of-locking-does-mongodb-use
  2. MongoDB Tutorial - MongoDB Locks
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.