By default the WriteConcern of Mongo is set to None, meaning your update is a send and forget and Mongo can do the save after you left, but it also means a read can come in before the write has occurred, and therefore not see the change. You can set the write concern globally in configuration or per write operation.
btw. Are you at USC? I am in Seal Beach and an alumni.
Hi,
I am trying to implement some of the most popular social networking actions i.e. add a friend, delete a friend, accept friendship, view profile and etc using MongoDB.
To handle my queries I created a users collection where every user has an id, username, address and some other attributes.
In addition, every user document has an array type attribute which keeps a track of the id for all of the user's friends.
For example:
user:{"_id":1, "username":"sumita", "friends":[2,3,4]}
user:{"_id":2, "username":"miki", "friends":[1]}
user:{"_id":3, "username":"nisha", "friends":[1]}
user:{"_id":4, "username":"lucky", "friends":[1]}
I have a multi-threaded benchmark for which every thread tries to emulate a user issuing a social networking action. For example thread 1 can emulate user with _id=1 deleting a friend from her friend list say friend with _id=2. And thread 2 can emulate a user i.e. user with _id=2 retrieving her list of friends. The first action will modify the documents to look as follows (as the friendship relationship is symmetric):
user:{"_id":1, "username":"sumita", "friends":[3,4]}
user:{"_id":2, "username":"miki", "friends":[]}
user:{"_id":3, "username":"nisha", "friends":[1]}
user:{"_id":4, "username":"lucky", "friends":[1]}
I have realized that with MongoDB when I increase the level of concurrency (increase the number of threads) or the percentage of update involved actions, there are cases in which the read observes inconsistent data. For example in the previous example, thread 2 may try accessing the list of friend ids for user with _id=2 and get [1] as the result instead of [] which is wrong.
I have read that MongoDB has one global collection lock so to my understanding the read can only access a record once the updates on that record have completed. And this means that I should not see any inconsistent data.
Any kind of help or suggestions is appreciated.
Sumita Barahmand