Your own Facebook

May 18th, 2010Posted by Steve


The social network phenomenon took the world by storm, now everyone wants their own Facebook or Myspace. So I set about thinking, how hard would it be to reproduce Facebook. The natural place to start is with Facebook itself, it has everything that people want, so all of the functionality is there.Social Facebook also has an extensive API, so there must be a link somewhere between the API and the system itself. Clues to this fact is the existence of FBQL a sort of wrapped up SQL which indicates that the whole site is extensively database driven. So now we have a good starting point the database itself, fortunately Facebook publishes its data definition file which will give us a good idea of the data fields that they are using so go ahead and download it and stick it in a new Visual Studio project. If you have not alread done so, read my article on An easy XML parser, you are going to need the knowledge either way. OK so now you have the DTD file, what about the database?. What you will need is SQL Server 2005 or later, as they support importing XML. (Note this does not work on Express and will probably need to run on Windows 2003 Server) as you need the full Management Interface. Go Ahead and import the DTD file this will create a bunch of tables and fields that represents the Facebook Database. The only thing missing now is the relationships between the Tables which is not described in the DTD file. The relationships become immediately obvious when you look at the primary Id's used in the tables as the database structure appears to be normalized and reasonable well thought out. Create a digram of the new database and add all of the tables and start creating relationships between Id's. When you have finished this you are left with a virtual replica of their database setup.

Now comes the hard part, writing the code. You have two choices here, you can copy the XML of the dtd file and "Paste XML as Types" from the Edit menu into a new class, or you can use the Entity Framework from Microsoft. Either way you will end up with a huge amount of classes (I didn't say this was going to be easy). If you elect the entity framework, then you will need to use the LINQ to SQL Classes, what this does is a similar job to the "paste XML as Types" but adds LINQ commands. If you use "Paste XML as Types" you will need to write the functionality yourself. What I did was create add, edit and delete stored procedures for all of the tables and linked them to the entity framework classes.

So thats about it, you have the database and the engine that controls it, all you need now are web pages that accept the input. As I said it is not easy, but take it from me it works, I have done exactly this and tried it using both methods. One thing to beware of is the Id's which caught Facebook on the hop, the used integers for the fields and quickly realized that there was not enough numbers as their ids are grouped. You can see this from their methods in the API which includes as translator to go from int to bigint. One other interesting thing which comes out of the database diagram is the tables that are not used like "Restaurants" and Apartment and House sales, which gives you an idea of how the whole system started and grew in a completely different direction. Anyone who says that Facebook was planned as a social network from the beginning is deluding themselves, it is obviously a cute story for the press.

Happy coding.