Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Facebook "like" data structure

I've been wondering how facebook manages the database design for all the different things that you can "like". If there is only one thing to like, this is simple, just a foreign key to what you like and a foreign key to who you are.

But there must be hundreds of different tables that you can "like" on facebook. How do they store the likes?

like image 561
Oskar Kjellin Avatar asked Sep 20 '11 15:09

Oskar Kjellin


People also ask

What data structure is used in Facebook?

This is because facebook uses a graph data structure to store its data.

What data structure is used in Instagram?

Instagram mainly uses two backend database systems: PostgreSQL and Cassandra.

Which data structure is used in social media?

Graphs are awesome data structures that you use every day through Google Search, Google Maps, GPS, and social media. They are used to represent elements that share connections. The elements in the graph are called Nodes and the connections between them are called Edges.

How would you design the data structures for a very large social network like Facebook or LinkedIn?

We can construct a graph by treating each person as a node and letting an edge between two nodes indicate that the two users are friends. If we want to find the path between two people, we start with one person and do a simple breadth-first search. Alternatively, we can do bidirectional breadth first search.


2 Answers

If you want to represent this sort of structure in a relational database, then you need to use a hierarchy normally referred to as table inheritance. In table inheritance, you have a single table that defines a parent type, then child tables whose primary keys are also foreign keys back to the parent.

Using the Facebook example, you might have something like this:

User ------------ UserId (PK)  Item ------------- ItemId (PK) ItemType (discriminator column) OwnerId (FK to User)  Status ------------ ItemId (PK, FK to Item) StatusText   RelationshipUpdate ------------------ ItemId (PK, FK to Item) RelationshipStatus RelationTo (FK to User)  Like ------------ OwnerId (FK to User) ItemId (FK to Item) Compound PK of OwnerId, ItemId 

In the interest completeness, it's worth noting that Facebook doesn't use an RDBMS for this sort of thing. They have opted for a NoSQL solution for this sort of storage. However, this is one way of storing such loosely-coupled information within an RDBMS.

like image 180
Adam Robinson Avatar answered Oct 17 '22 23:10

Adam Robinson


Facebook does not have traditional foreign keys and such, as they don't use relational databases for most of their data storage. Simply, they don't cut it for that.

However they use several NoSQL type data stores. The "Like" is most likely attributed based on a service, probably setup in an SOA style manner throughout their infrastructure. This way the "Like" can basically be attributed to anything they want it to be associated with. All this, with vast scalability and no tightly coupled relational issues to deal with. Something that Facebook, can't really afford to deal with at the volume they operate.

They could also be using an AOP (Aspect Oriented Programming) style processing mechanism to "attach" a "Like" to anything that may need one at page rendering time, but I get the notion that it is asynchronous processing via JavaScript against an SOA style web service or other delivery mechanism.

Either way, I'd love to hear how they have this setup from an architecture perspective myself. Considering their volume, even the simple "Like" button becomes a significant implementation of technology.

like image 20
Adron Avatar answered Oct 17 '22 22:10

Adron