Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

data model for notification in social network?

Tags:

I build a social network with Neo4j, it includes:

Node labels: User, Post, Comment, Page, Group

Relationships: LIKE, WRITE, HAS, JOIN, FOLLOW,...

It is like Facebook.

example: A user follow B user: when B have a action such as like post, comment, follow another user, follow page, join group, etc. so that action will be sent to A. Similar, C, D, E users that follow B will receive the same notification.

I don't know how to design the data model for this problem and I have some solutions:

  1. create Notification nodes for every user. If a action is executed, create n notification for n follower. Benefit: we can check that this user have seen notification, right? But, number of nodes quickly increase, power of n.
  2. create a query for every call API notification (for client application), this query only get a action list of users are followed in special time (24 hours or a 2, 3 days). But Followers don't check this notification seen or yet, and this query may make server slowly.
  3. create node with limited quantity such as 20, 30 nodes per user.
  4. Create unlimited nodes (include time of action) on 24 hours and those nodes has time of action property > 24 hours will be deleted (expire time maybe is 2, 3 days). Who can help me solve this problem? I should chose which solution or a new way?
like image 255
kien bui Avatar asked May 20 '17 10:05

kien bui


1 Answers

I believe that the best approach is the option 1. As you said, you will be able to know if the follower has read or not the notification. About the number of notification nodes by follower: this problem is called "supernodes" or "dense nodes" - nodes that have too many connections.

The book Learning Neo4j (by Rik Van Bruggen, available for download in the Neo4j's web site) talk about "Dense node" or "Supernode" and says:

"[supernodes] becomes a real problem for graph traversals because the graph database management system will have to evaluate all of the connected relationships to that node in order to determine what the next step will be in the graph traversal."

The book proposes a solution that consists in add meta nodes between the follower and the notification (in your case). This meta node should got at most a hundred of connections. If the current meta node reaches 100 connections a new meta node must be created and added to the hierarchy, according to the example of figure, showing a example with popular artists and your fans:

Dense node

I think you do not worry about it right now. If in the future your followers node becomes a problem then you will be able to refactor your database schema. But at now keep things simple!

In the series of posts called "Building a Twitter clone with Neo4j" Max de Marzi describes the process of building the model. Maybe it can help you to make best decisions about your model!

like image 60
Bruno Peres Avatar answered Sep 22 '22 11:09

Bruno Peres