The Lunch Break
I had to move my car during the break so I missed most of it. I suppose I should note now that I wasn’t impressed with the cafeteria food, but since it was the second time I’ve been able to actually eat at Facebook—they grew too big and I grew too impatient all the other times—I suppose I shouldn’t complain. Oh yeah, I’m apparently FourSquare Mayor of all the fast food places in Mission Beach, so that gives you an idea of my palette.
In any case, I used that time to catch up with Haiping. There was some amusement when he thought everyone in the PHP world was as skeptical about HipHop as me, Lucas, and Shire.
PHP at Facebook (before HipHop)
“Move fast and break things.”
—Mark Zuckerberg, CEO of Facebook, talking about Facebook’s core engineering philosophy
(This was given by Haiping.)
HP likes PHP
Put simply, PHP means fast development.
First, it is easy to pick up. It is much easier to hire PHP developers compared to any other language out there. Moreover, you don’t have to hire PHP developers, you can just hire engineers who know other languages and teach them PHP. In Facebook’s case, 50-60% of their engineers do not know PHP coming in, but they pick it up in a few weeks.
Here is “Hello World” in PHP:
Hello World!
Facebook, Palo Alto, California
Olympus E-P2, M.ZUIKO Digital ED 14-42mm 1:3.5-5.6
1/60sec @ ƒ3.5, ISO500, 14mm (24mm)
“You’ve probably visited one of my sites.”
—Paul Reinheimer
Paul is the senior developer at a bunch of large websites that the internet is for. Needless to say, scaling traffic is an issue for them.
Furthermore, PHP is easy to debug. It really fits well with an open source/open development structure. In Facebook’s case, because of PHP, every engineer can own and understand the entire stack. This means code can be modified by anyone else and, since every patch is code reviewed and test plans are written, this can be carried over and the necessary parts can be cobbled together for release (this is the subject of other talks already given by by other Facebook engineers at conferences so I won’t go into this.)
Finally, from Haiping’s perspective (as a C++ coder), there are a number of wins. The language is extremely straightforward with a syntax very similar to C++ and Java. However, you can see that PHP is a thin layer between it and the kernel (he means lower level languages like C), unlike .NET or Java which is highly abstracted. Here traditional weaknesses of PHP (like inconsistent function naming, lack of lexical scoping) become strengths when you are trying to write a cross-compiler. For instance, the reason function naming and parameter ordering is inconsistent in PHP is because that’s the name and ording in the C libraries they are based on—they’re almost straight translations. Another example is that PHP has only one level of scoping (no more), and global variables have to be explicitly imported—this will make a huge difference when parsing for static types as we’ll see later. Given what I’ve written about him in the past, you can almost see Haiping chomping at the bit to tackle this optimization problem.
Four problems of PHP
PHP does present four challenges for Facebook.
The first is High CPU utilization.
Some notes about the benchmarks: The tests were done an a quad-core 64-bit Linux machine, which is a typical type of CPU/OS for a website on Facebook’s scale. The benchmarks, however, are quite artificial, compared to our problem space—scripting languages should do much worse and Python is more optimized for these than the others. Finally, some marks are missing making the geometric mean a little flawed. They do, however, demonstrate a point—while you can’t take the raw numbers as gospel—no, PHP is not 30 times slower than Java on the web—relative rankings should are mostly unchanged.
If we could perform the suite on this set, I’d expect HipHop for PHP to improve the performance conservatively at 3x. This would put it near the performance of Erlang—significantly better than Python, Perl, and Ruby, but similarly significantly slower than C#, Java and native C++.
In general, Facebook claims, that the ratio of users-to-developers on their site means the problem gets amplified with the scale. (I tend to think the ratio is the ratio of user-space-coders-to-core-development is also significant—Yahoo has hundreds of C extensions for PHP; Facebook has around a dozen.) By this he concludes something well-known operationally on consumer facing websites of this scale: small savings in time = big savings in money because hardware costs and power costs take a huge chunk of the overall budget. In Facebook’s case, currently a 20 millisecond per page savings is estimated at a $10 million/year overall savings.
The performance gap between PHP and C++ is emphasized simply by the architecture of Facebook. Facebook is a very monolithic PHP application (unlike Yahoo, which would be many independent separate applications of reasonable size). In their infrastructure currently, include structures may chain as deep as 50 levels, with over a thousand separate file includes in order to render one page.
(This is highly unusual, even for a social network, and normally would have been coded around. That can’t be done here. We’ll see when we investigate other attempts to leave previous attempts to leave PHP.)
The second problem is high memory usage:
Java looks horrible because of its automatic garbage collection.
A more telling feature of the quirky memory usage of PHP is this a completely artificial benchmark, array assignment:
for ($i=0; $i<$max; $i++) {
$a[] = $i;
}
Yields the following memory footprint:
| System | Memory (MB) |
|---|---|
| PHP 5.2, $max=1,000,000M | 150 |
| PHP 5.2, $max=5,000,000M | 700 |
| HipHop, $max=1,000,000M | 17 |
| HipHop, $max=5,000,000M | 47 |
Of course, this is an extreme case, but the point is memory efficiency is bad. (Given some of the patches to PHP core I’ve seen coming out of Facebook, I’d be unsurprised that some code there is assigning an array with a million nodes.)
(This is significant because it sounds like Facebook web servers are running at peak util on both RAM and CPU—a very uncommon situation. This is a sign of bad luck or a highly-optimized, well-utilized PHP installation.)
The third problem is that Facebook has a strong C++ and Python following (as well as other languages). PHP is used in a number of areas: display (to HTML and Ajax), business logic, and data models. All of that is built on infrastructure modules, some of which are also built on PHP. These C++/Python developers would like access to components, some of which is only written in PHP. This means that it is difficult to use PHP logic in other systems.
(Note this isn’t as bad as it sounds. Facebook does have an API layer in the form of Thrift for inter-language communication. But not all stuff is Thrift-enabled and there is the RPC overhead associated with this. Most startups on this scale are less tightly-coupled than Facebook. For instance, at a social network I worked at, all high-performance modules are written in Java, not PHP; no display logic is coded without a corresponding API (both Ajax and binary protocols); and the Java layer “speaks” PHP and directly accessed same memcached objects, databases and filesystem as PHP.)
The fourth problem is that extensions are hard to write for most PHP developers (at least at Facebook). While Yahoo has had this approach to performance, a survey of extension use at Facebook finds only 15 custom PHP extensions written from scratch there—seven of which were written by Haiping.
Other attempts to leave PHP
“Gallium Arsenide is the ‘material of the future’… and will be so in the future.”
—Aphorism in solid state physics
There have been many attempts to migrate away from PHP at Facebook over the years.
Since 2007 alone, Haiping named four failed attempts: to Python (twice), to Java, to C++.
What happened was the same problem that you see in GaAs-technology for semiconductors. A friend of mine is fond of quoting the aphorism above. In materials sciences, as a semiconductor, Moore’s law exists. After a decade of research, a GaAs microprocessor may have matched silicon’s performance numbers only to find out silicon has increased to 32x the processing power during that same time period—chasing and chasing, and only falling further behind.
These attempts fall into this same classic trap of architecture migration. Let’s say you gather a number of five great engineers who are really excited about moving Facebook over to Python. They’re so motivated and so good, that they work late every night to turn out 7500 lines of Python that day. The problem is, in the same day, Facebook has a hundred engineers who maybe are not as motivated, but they’re just as talented, and they’ve turned out 10,000 new lines of PHP—a language with a similar work/line of code as Python. Not only that, how do you keep up the rate as new hires come in and increase the total PHP code output of the company? You’ve lost.
Facebook, Palo Alto, California
Sony DSC-WX1
1/100sec @ ƒ2.4, ISO640, 4.3mm (24mm)
Facebook, like many Web 2.0 companies, has open offices. They use the same model desks that I used at Tagged (Ikea Galant) which is a step down from the ones at Plaxo and Meebo (Ikea Effektiv). The actual size of the desks is slightly smaller than the one I had because we wanted ours to face each other: designed to fit 2 wide = 1 length, but they’re currently laid out in long horizontal lines. These sort of inches add up in a company—you can fit far more engineers in a tiny space this way, then with cubes.
To effect this sort of change, you depend on everything to come to a screeching halt as you migrate the platform. It’s not a language thing—Friendster tried to migrate to PHP—it’s a process thing. And amplified when your CEO is repeating the mantra: “Move fast and break things.”
From my personal experience, I was involved with a re-architecture (without language change) of a social network. At every point the new code added had to be more efficient than the piece it replaced and feature failover was designed to bootstrap the old code if needed. There were only a few “old infrastructure” programmers to compete with. Many of the re-architected libraries allowed the developers to code much more efficiently under the new system. The existing release cycle was slow (seven major releases a year when I started). Even under these relatively benign re-architecture conditions, it took two years to complete and I exited with a modern web release cycle in place—250 releases a year. Facebook has an architecture as variegated as the teams that built the features, has over a hundred “old infrastructure” programmers, an no obvious global efficiencies that haven’t already been worked around through a patch to the PHP engine or APC, and releases multiple times a day!
Improving PHP
Another approach is to improve the Zend core. In fact, many times in Facebook’s history, the growth curve was about to break everything when some improvement to APC, Zend, etc. was done to get them out of the bind. You can get a great idea of some of those from Brian Shire’s PHP Tek talk on it (Facebook blog) and there were ooze other ideas left to try: like warming up interpreter, roll back and restarting…
For some reason or another, engineering management felt these sort of patches would only yield a marginal increase in performance going forward.
Another approach is to migrate Facebook to Apache 2.0. Facebook is on Apache 1.3. This was a little confusing, but what they really mean is upgrade to a threaded Apache 2.0 instead of in Apache in MPM prefork mode. They were further confused in not understanding that the PHP core is actually already thread-safe, but none of the libraries are. PHP is a lot like Linux: when we use the term “Linux” we don’t mean just the kernel, but the entire GNU/Linux software stack that forms the operating system; when we use the term “PHP” it refers to more than Zend and the core—to the entire commonly used extension stack that sits on top of it. Auditing PHP for multithreading would mean auditing every library—not a simple task and definitely with no support from the community.
Well in any case, for better or worse, after about four years of straight successes with this process, it lost out to the HipHop project.












