Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Efficient way to creating relationships with NoSQL database

Tags:

nosql

I am currently trying to implement Tumblr-like user interactions like reblog, following, followers, commenting, blog posts of people who I currently following etc. Also there is a requirement to display activity for each blog post.

I am stuck with creating proper schema for database. There are several way to achieve this kind of functionality (defining data structures embedded like blog posts and comments, creating an activity document for each action etc.) but I couldn't currently decide which way is the best in terms of performance and scalability.

For instance let's look at implementation of people who I follow. Here is sample User document.

User = { id: Integer, 
         username: String, 
         following: Array of Users,
         followers: Array of Users,
       }

This seems trivial. I can manage following field per user action (follow/unfollow) but what if an user who I currently follow is deleted. Is it effective to update all User records who follows deleted user.

Another problem is creating a view of blog post from people who I follow.

 Post = { id: Integer, 
          author: User, 
          body: Text,
        }

So is it effective query latest posts like;

 db.posts.find( { author: { $in : me.followers} } )
like image 491
Burak Bayer Avatar asked Oct 09 '22 17:10

Burak Bayer


1 Answers

It seems (to me) that you are trying to use a single data store (in this case a document-oriented NoSQL database) to fulfill (at least) two different requirements. The first thing you seem to be trying to do is store data in a document-oriented store. I am going to assume that you have legitimate reasons for doing this.

The second thing you seem to be trying to do is establish relationship(s) between the documents you are storing. Your example shows a FOLLOWS relationship. I would recommend treating this as a different requirement from storing data in a document-oriented NoSQL database and look at storing the relationships in a graph-oriented NoSQL database such as Neo4j. This way, your entities can be stored in the document store and relationships in the graph store using just the document IDs.

My experience has been that it will be difficult (if not impossible) to get a single NoSQL database to meet all functional and non-functional needs of a medium to large sized application. For example, the latest application I am working on uses MongoDB, Redis and Neo4j besides an RDBMS. I spent a lot of time experimenting with technologies and settled on this combination. I have committed myself to using Spring 3, along with the Spring Data project and so far my experience has been great.

like image 58
manish Avatar answered Oct 12 '22 10:10

manish