Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does MongoDB deal with concurrent updates?

I started to use MongoDB at work so far so good. I was wondering though how does MongoDB deal with concurrent updates ? I've read that there is no locking feature in MongoDB so I was wondering what is the the common practice to deal with this.

Thanks.

like image 951
lollancf37 Avatar asked Aug 09 '11 14:08

lollancf37


People also ask

How does MongoDB handle concurrent request?

MongoDB allows multiple clients to read and write the same data. To ensure consistency, MongoDB uses locking and concurrency control to prevent clients from modifying the same data simultaneously. Writes to a single document occur either in full or not at all, and clients always see consistent data.

How many concurrent requests can MongoDB handle?

On MongoDB, connections are a lot lighter and we set the limit at 5000. That's across the database as a whole, not particular portals. So, with 5000 connections it should be pretty hard to hit the limit and if you do, chances are there's something up with your application.

How does updates work in MongoDB?

MongoDB Update method is used to update the document from the collection, and the update method will update the value of the existing document. We have used a $set operator at the time of updating the document. We can update a single document by using an update or updateOne method.

How many transactions per second can MongoDB handle?

MongoDB supports 250 million ticks per second, at 40x lower cost than the legacy technologies it replaced. Qihoo uses MongoDB to support over 100 applications deployed on over 1,500 nodes, serving 20 billion operations per day.


2 Answers

MongoDB used a process wide write lock to guarantee that only one write operation (update/insert/remove) can be performed at a time. As such it automatically solves concurrency issues since write concurrency simply isn't allowed.

If 4 threads attempt an update operation one of them will take the write lock, do its update and release the lock. After that one of the remaining 3 will grab the lock, do its update, etc.

Concurrency only comes into play if your operation cannot be wrapped in a single write operation. Note that for the most common usecase (find a doc, update it and grab the new version atomically) MongoDB offers the "findAndModify" command which does just that : http://www.mongodb.org/display/DOCS/findAndModify+Command

UPDATE : Locking is more granular these days.

like image 78
Remon van Vliet Avatar answered Oct 23 '22 11:10

Remon van Vliet


Use modifier operations:

$inc $set $unset $push $pushAll $addToSet $pop $pull $pullAll $rename $bit

all of them are atomic.

like image 45
Karoly Horvath Avatar answered Oct 23 '22 13:10

Karoly Horvath