If there's an X lock on document A, does the IX lock on the collection containing A affect what locks can be on document B in the same collection?

When we say an intent exclusive lock on collection A precludes an exclusive lock, does that mean: there can be no exclusive lock on the collection? No exclusive locks on documents in the collection? Neither?? Does a collection or database level intent lock contain a reference to a specific document(s), or is it truly a lock on that collection or database and does not contain a reference to its children? Does the intent lock exist only for optimization purposes, to prevent operations and queries from checking lower level locks?

I’ve been scouring the web, the old google group, stackoverflow etc. for the answers to these questions. Really appreciate any clarification :sweat_smile: thank you


I’ll start with this page as a reference: https://en.wikipedia.org/wiki/Multiple_granularity_locking

MongoDB uses these types of locks (except it does not implement the SIX flavor of lock). MongoDB uses MGL with this hierarchy:

  • Global
  • Database
  • Collection

It does not implement Document level locks in this scheme; instead, the pluggable storage engine is expected to handle concurrency between all readers and writers at this level.

There is one Global lock resource, one Database lock resource per database, and one Collection lock resource per collection. Every access to a document must first lock the resources in the hierarchy order. For a reader in the foo.bar collection, this would be: Global IS, Database foo IS, Collection bar IS. Once all of those locks are granted, in order, the reader is then allowed to enter the storage engine and establish a cursor to read documents in that collection.

You might consider the MGL scheme a form of optimization, but the alternative of having one lock resource per document would make the database operationally impossible, since operations that need exclusive access to a higher level (say, a drop-database operation which needs exclusive access to a Database) would first need to acquire potentially millions of document locks before it could proceed.

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.