Almost everyone has a Facebook account, even people who are not familiar with the Internet. With millions people actively using Facebook, updating their status, replying to messages, uploading photos and so on, how is Facebook's page still loading very fast?
I was told that Facebook was built using only PHP and MySQL, so how can Facebook's performance be so good?
There's no single reason, but a whole lot of reasons:
Heavy usage of caching [1] (APC and memcached [2]), which drastically cuts processing time. Slide 12 compares load time with APC (~130 ms) versus without it - 4050 ms. That's 30x faster!
Usage of HipHop [3], which converts PHP into C++ code (which is then compiled into much more efficient machine code than actual PHP).
Facebook uses PHP and MySQL, but that's not the only thing they use. For example, they use Erlang [4] for their chat, Hadoop clusters [5] for some of their storage. If you go visit their careers page [6], you'll see they are hiring developers with experience in C++, Java, Python, and others.
Facebook has data distributed across many, many servers. In June 2010, FB had 60,000 servers [7]. (think that's too much? Google had half a million... 5 years ago)
Facebook sends as little traffic as possible [8]: they use static CDNs to deliver static content. Gzip to compress data. Cookies, Javascript, HTML - everything is cut back to reduce the number of bytes sent over the network. They use a technology they call "BigPipe", which sends partial content rather than the whole page.
to mention a few...
[1] http://www.scribd.com/doc/3871729/Facebook-Performance-CachingMySQL
. - Josh Smith
Ultimate reason: http://memcached.org/
They claim 98% of everything you see on Facebook is from their massive memcache server cluster.
Check out http://facebook.com/techtalks.
They have some great videos describing many of their various optimizations. For instance, there's a talk on memcached [1] (which helps speed up common key gets) and their front-end optimizations [2] (doing lazy loading of Javascript, etc).
The amazing part is how large everything is at facebook. With millions of users and thousands of servers, even a seemingly small optimization can end up saving them millions of dollars or gigabytes of memory.
[1] http://www.facebook.com/video/video.php?v=631826881803More info at http://highscalability.com/blog/category/facebook
By
This article talks about the inner workings of Facebook: http://royal.pingdom.com/2010/06/18/the-software-behind-facebook/
Facebook was not only using MySql - it started out using Cassandra [1], and is migrating over to HBase [2]. Applications like FB need a highly scalable Database.
[1] http://cassandra.apache.org/Watch this presentation [1] of Aditya Agarwal, Director of Engineering at Facebook, this presentation talks about Facebook’s architecture and its major components (LAMP (PHP, MySQL), Memcache, Thrift, Scribe).
[1] http://www.infoq.com/presentations/Facebook-Software-StackIt's worth looking into how Facebook selectively loads front end JS so that there is very little latency in the responsiveness of the UI.
They have a compiled version of php, in fact. My guess would have to be: insane amounts of crazy hardware, brutally efficient code, and a database structure optimized with caching, denormalization, clustering...
Maybe a small part of the solution overall, but they also optimize for fast client-side rendering. They contracted CSS expert Nicole Sullivan [1] to do some optimization based on her OOCSS techniques [2].
[1] http://www.stubbornella.org/Because they have a lot of money. Hiring smart developers and buying tons of servers every week is quite costly.
XHP is a PHP extension which augments the syntax of the language such that XML document fragments become valid PHP expressions. - GIT checkout [1] and Wiki [2]
[1] http://github.com/facebook/xhp/There is a lot of information about their technology in the Facebook Group Facebook Engineering [1], including discussions of their MySQL cluster, XHP, memcached, etc.
It comes down to having enough money to hire smart staff who write efficient programs running on many many computers.
[1] http://www.facebook.com/Engineering#!/Engineering?v=info