Whenever the question of designing a social media system comes up, we all are too fast to jump to say that we will use graph database, because Facebook uses it. But does it actually ????
1. First things first, the primary database used by Facebook for storing objects is MySQL database which is a relational database and not a graph database.
2. But, there is no relational database in this world which can match up to the scale of Facebook, especially for celebrities who get millions of likes/requests etc.
3. To solve this issue, they came up with a common solution that every engineer would do. They put a large distributed cache over the primary key-value store. To solve this purpose, Facebook uses Memcache which is a famous distributed cache.
4. But, considering the kind of requests which facebook has to serve- like number of likes on a given post, number of followers of a given page etc, MySQL database was not enough considering large join times.
5. To solve this, Facebook analysed their request patterns and realised some request patterns are very rare and unlikely to happen and hence need not be stored in cache. This improved the performance a lot. But, due to this the code for storing desired data in the cache became quite complex.
6. It is then, when Facebook came up with TAO which uses a graph to represent relationships among objects and caches this graph in RAM/Flash Memory. The objects, though are still persisted in the MySQL storage. But, the cache above the storage stores data in the form of a graph.
7. The data set managed by TAO is partitioned into hundreds of thousands of shards. All objects and associations in the same shard are stored persistently in the same MySQL database, and are cached on the same set of servers in each caching cluster.
This is how Facebook works as it works today and even though it uses graph database, but not as its primary storage and there are many reasons for it. There are many things which relational databases offer which graph databases don’t. So, it is not necessary that the answer to every social media design would be a graph database.