Skip to content

MongoDB Concurrency

prohaska edited this page Mar 20, 2013 · 20 revisions

This wiki describes the concurrency seen by MongoDB applications and how it is implemented.

Observed concurrency

MongoDB allows concurrent queries on any collection in any database

  • Query database(a).collection() || Query database(a).collection()
  • Query database(a).collection() || Query database(b).collection(), a != b

MongoDB does not allow concurrent insertions to the same database

  • Insert database(a).collection() blocks Insert database(a).collection()

MongoDB does allow concurrent insertions to different databases

  • Insert database(a).collection() || Insert database(b).collection(), a != b

MongoDB queries block insertions to the same database

  • Query database(a).collection() blocks Insert database(a).collection()

MongoDB insertions block queries in the same database

  • Insert database(a).collection() blocks Query database(a).collection()

MongoDB allows concurrent insertions and queries in different databases

  • Insert database(a).collection() || Query database(b).collection(), a != b
  • Query database(a).collection() || Insert database(b).collection(), a != b

MongoDB does not allow concurrent queries with dropping the collection

  • Query database(a).collection(b) blocks database(a).collection(b).drop

Examples

Insert

  • Grab a write lock on the database
    • Find the rwlock associated with the database name in the dblocks map
    • Grab a write lock on database's rwlock
    • Grab a little write lock on the qlock
  • Begin txn
  • Lookup the name space details
  • If no name space details
    • Create new name space details. This is implicit collection creation.
  • Insert the documents into the collection
  • Commit txn
  • Release write lock

Queries

  • Grab a read lock on the database
    • Find the rwlock associated with the database name in the dblocks map
    • Grab a read lock on the database's rwlock
    • Grab a little read lock on the qlock
  • Begin txn
  • Find the collection
  • Query the collection
  • Commit txn
  • Release the read lock

Locking mechanism

What is the dblocks map? The dblocks map is a map from database name to a reader writer lock.

How is the dblocks map used?

What is the qlock?

How is the qlock used?

Operations and Locks

  • Create Database
  • Drop Database - Write
  • Create Collection - Write
  • Drop Collection - Write
  • Query - Read
  • Insert - Write
  • Update - Write
  • Remove - Write
  • Create Index
  • Drop Index
  • Others?
Clone this wiki locally