1

How does Mongodb handle insert/update/delete merge conflicts in case concurrent distributed writes?

Inserts:

op1: insert( { _id : 'joe', age : 30 } )
op2: insert( { _id : 'joe', age : 33 } )

Deletes:

op1: set( { _id : 'joe', age : 40 } }
op2: delete( { _id : 'joe' } )
op3: set( { _id : 'joe', age : 33 } )

Updates:

p1: update users set age=40 where _id='joe'
op2: update users set state='ca' where _id='joe'
7
  • 1
    objects with same _id will not be inserted !
    – Sikorski
    Mar 11, 2014 at 12:16
  • 2
    When you're wondering about things like this, just try them out on a dummy collection in the shell.
    – JohnnyHK
    Mar 11, 2014 at 12:18
  • 2
    hmm its about the concurrent writes; so I am not sure how I can test that Mar 11, 2014 at 12:20
  • 1
    Neil you are rudest person I have ever met. Are you kind of a Admin of this webpage or u just happen to barge in all the conversations? I come from a free world I like to do things in my way and not get moderated each and single small things Mar 11, 2014 at 12:55
  • These are atomic operations so the amount of time between them doesn't actually matter. Pick an order and test it out.
    – JohnnyHK
    Mar 11, 2014 at 12:56
6

in case concurrent distributed writes

MongoDB doesn't support distributed writes. Writes always go to the primary, so there can't be any conflicts, theoretically.

That isn't entirely true however - if you write to the primary just before it goes down, it's possible that the write is in the primary's journal but didn't make it to the secondary and your client code didn't get a response. In that (rare) case, you will have a conflict. MongoDB handles that by putting a bson file in a special folder named rollback when the server comes back up.

Apart from the intricacies of replica sets, operations are always performed in order (where, again for the replica sets, the order is determined by the primary's clock). As JohnnyHK already pointed out, updates can perform selective updates like $inc or $set, or replace the entire document. What makes sense depends on your application. If you perform a lot of $incs, e.g. for a hit counter, the order doesn't matter, but it does, of course, when you perform a replace. In that case, following from the above, the last write wins. In general, I think one should put some though into object ownership, i.e. who is really allowed to replace a document.

If you want to have MVCC-like (multi-version concurrency control) behavior, e.g. allow different users to write to the same (logical) document with change tracking (something like git), you might want to check out MongoMVCC. That is only relevant if you want concurrency at the application level, so your users can deal with different versions/branches directly.

1
  • 1
    Thanks Christoph, that's exactly the kind of explanation I was looking for. Mar 12, 2014 at 9:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.