Skip to content

MongoDB Concurrency

prohaska edited this page Mar 19, 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

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?

Locking examples

Inserts

  • 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

Queries

  • 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
Clone this wiki locally