daydreamer
daydreamer

Reputation: 91949

MongoDB :are reads/writes to database concurrent?

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?

Upvotes: 5

Views: 1598

Answers (2)

Chris Fulstow
Chris Fulstow

Reputation: 41872

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.

Upvotes: 1

jeffsaracco
jeffsaracco

Reputation: 1269

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.

Upvotes: 3

Related Questions