1

I am building a social network app and I am very concerned about next thing.What happens when in mongoDB a lot of users(let's assume millions) try to modify the same document at the same time. Will there be any mismatch ,or ignored queries or any kind of unexpected behaviour?

Practical Example: 2 collections: 'posts' and 'likes' posts will have fields id | name | info | numberOfLikes likes will have fields id | post | fromUser

When assumed millions of users like the post ,like object appears in 'likes' collection and business logic automatically increments numberOfLikes for post. I thought if there could be a conflict when tons of users try to modify that post likes count at the same time.

Cœur
  • 37,241
  • 25
  • 195
  • 267
  • I'm actually reading this as asking for a fair bit more than locking levels and concurreny. It's a very broad issue, particularly in the context of a "social network" type of site. There are various ways to approach different ways to model this. You might consider a browse through the principles of [socialite](https://github.com/10gen-labs/socialite) which is a MongoDB reference project aimed at feeds, user graph, data aggregation and other common concepts. But I think a specific "basic case" or "advanced case" would be a better question that is less broad than this potentially is. – Blakes Seven Jul 27 '15 at 10:51
  • @BlakesSeven my case specifically are 'events' 'activity' collections ( likes , comments, goings , hashtags , mentions etc ). – Grisha Gevorkyan Jul 27 '15 at 10:56

3 Answers3

3

Databases have mechanisms in place to prevent this kind of situation. You can 'lock' on various logical structures, so you can be assured your data is intact - regardless of your transaction count.

See more below:

http://docs.mongodb.org/manual/faq/concurrency/

davsp
  • 2,049
  • 1
  • 14
  • 13
  • this makes sense on a really basic level. Did I get it right that if there database gets 100000 write operations simultaneously on same document it will unite them all into a single write operation? And is it not going to overload the database (I mean simply lag) ? @davsp – Grisha Gevorkyan Jul 27 '15 at 10:54
0

In MongoDB, operations are atomic at the document level. See http://docs.mongodb.org/manual/core/data-modeling-introduction/#atomicity-of-write-operations

jianfyun
  • 71
  • 1
  • 4
0

A couple of things.

You're saying you're building a social app and expect millions of likes and "tons" of them at the same time. Of course it's good to consider performance and scaling at the start of a project, but you're not going to build the next Facebook right now.

Furhtermore, you seem to want to use MongoDB as primary database for this app, and you seem to want to use it as a relational database. Read the somewhat biased titled article Why You Should Never Use MongoDB.

I'd suggest backing your site with a relational database (which may also be better for queries like "What posts did this user like" and "Did this user already like this post") and denormalizing that into MongoDB at regular intervals.

CodeCaster
  • 147,647
  • 23
  • 218
  • 272