How to build LinkedIn kind of Relationship Tree – Neo4J29 December , 2015
After using so many social networking website and application, as an IT expert, sometime, we got question like how they manage data, how they manage performance and so. Here is the answer of one of the query like “How to build LinkedIn kind of Relationship Tree”.
First, everyone will think about to manage in SQL (MySQL, MSSQL etc) or NoSQL (MongoDB, Apache Cassandra Redis etc.) database which are good for single level relationship. But what if we have to display relationship tree like what LinkedIn does (as shown in image), which shows relationship level and chart on which level you are connected with that user and by whom?
Lets think about relational SQL database where we store all the first level friends of the user, and we will get our second level friend from our direct friend’s first level friend but when we will go further with 3rd or 4th level friends then it would lead to poor performance and make our job nearly impossible. So NO to MySQL.
Fortunately, to make to difficult (Nearly Impossible) job to possible, we have Graph DB. Graph DB where we can store users as node with tree kind of relationship as shown below. There are many graph database available like AllegroGraph, ArangoDB, GraphBase, Neo4J and many more. But after lot study, we found Neo4J is very stable and reliable among this.
Neo4j is the graph database , which is capable of storing millions of nodes and relationship between them . This database use Cypher Query for fetching of result and even for returning thousands of nodes it requires only milliseconds. The relationship that we create between nodes can be unidirectional or bidirectional (depending upon requirement) .
Neo4j allows us to traverse the tree structure to any depth and a allow us to find the any friend , friend-of-friend to any level . The cypher query that we write to find the desired node in neo4j is fathomable and has capability to filter , sort, order etc just like any other query.
So according to our scenario we considered all the users as a nodes and if they are related to each other a relationship is created between them.
All the registered user will become a node in our graph . Now suppose user 1 is friend of user 2 then a relation `FRIEND` is created between them.
And now if this user 2 is friend of user 4 then a relation `FRIEND` is created between them.
So you see if this scenario continues then we create a network (graph) of people , and we can traverse to any depth to find how people are related to each other .
Now if we want to know how this user 1 is related to user 4 then MATCH path = shortestPath(n-[:FRIEND]->m ) where n.id=1 and m.id=4
So , this will give us the shortest path between user 1 and 4 also through the length of `path` we will get the level of the friendship (1st level or 2nd or ……. ) .
Through this graph we are capable of finding how they are related (mutual friends) and also know they are at which level of our friendship (which was the main requirement of our project) and there are many other functionality the graph database is capable of .
So you see how the graph made the complex MySQL structure easy and efficient. If you are looking to develop system or application which graph based storage then our expert team is always ready to help you . Please Contact us
How an Online Ordering app will benefits your Restaurant Business?January 29, 2018
Top 7 iOS Application Development Trends for 2018 & BeyondDecember 1, 2017
Amazon Aurora – Highly Scalable, Secure and Reliable Relational DatabaseOctober 16, 2017