Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MongoDB :are reads/writes to database concurrent?

Tags:

mongodb

nosql

What happens when million threads try to read from and write to MongoDB at the same time? does locking happens on a db-level, table-level or row-level?

like image 232
daydreamer Avatar asked Oct 10 '22 17:10

daydreamer


2 Answers

It happens at db-level, however with Mongo 2.0 there are a few methods for concurrency, such as inserting/updating by the _id field.

like image 189
jeffsaracco Avatar answered Oct 13 '22 12:10

jeffsaracco


You might run into concurrency problems, especially if you're working with a single MongoDB instance rather than a sharded cluster. The threads would likely start blocking eachother as they wait for writes and other operations to complete and locks to be released.

Locking in MongoDB happens at the global level of the instance, but some operations since v2.0 will yield their locks (update by _id, remove, long cursor iteration). Collection-level locking will probably be added sometime soon.

If you need to have a large number of threads accessing MongoDB, consider placing a queue in front to absorb the impact of the concurrency contention, then execute the queued operations sequentially from a single thread.

like image 27
Chris Fulstow Avatar answered Oct 13 '22 10:10

Chris Fulstow